Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usePreferredContrast): new function (#2040)
- Loading branch information
1 parent
73c753f
commit 6db0559
Showing
6 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { defineComponent, reactive } from 'vue-demi' | ||
import { usePreferredContrast } from '@vueuse/core' | ||
|
||
export const UsePreferredContrast = defineComponent({ | ||
name: 'UsePreferredContrast', | ||
setup(props, { slots }) { | ||
const data = reactive({ | ||
contrast: usePreferredContrast(), | ||
}) | ||
|
||
return () => { | ||
if (slots.default) | ||
return slots.default(data) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script setup lang="ts"> | ||
import { usePreferredContrast } from '@vueuse/core' | ||
const contrast = usePreferredContrast() | ||
</script> | ||
|
||
<template> | ||
<note class="mb-2"> | ||
Preferred contrast: | ||
</note> | ||
<code>{{ contrast }}</code> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
category: Browser | ||
--- | ||
|
||
# usePreferredContrast | ||
|
||
Reactive [prefers-contrast](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-contrast) media query. | ||
|
||
## Usage | ||
|
||
```js | ||
import { usePreferredContrast } from '@vueuse/core' | ||
|
||
const preferredContrast = usePreferredContrast() | ||
``` | ||
|
||
## Component Usage | ||
|
||
```html | ||
<UsePreferredContrast v-slot="{ contrast }"> | ||
Preferred Color Scheme: {{ contrast }} | ||
<UsePreferredContrast> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { computed } from 'vue-demi' | ||
import { useMediaQuery } from '../useMediaQuery' | ||
import type { ConfigurableWindow } from '../_configurable' | ||
|
||
export type ContrastType = 'more' | 'less' | 'custom' | 'no-preference' | ||
|
||
/** | ||
* Reactive prefers-contrast media query. | ||
* | ||
* @see https://vueuse.org/usePreferredContrast | ||
* @param [options] | ||
*/ | ||
export function usePreferredContrast(options?: ConfigurableWindow) { | ||
const isMore = useMediaQuery('(prefers-contrast: more)', options) | ||
const isLess = useMediaQuery('(prefers-contrast: less)', options) | ||
const isCustom = useMediaQuery('(prefers-contrast: custom)', options) | ||
|
||
return computed<ContrastType>(() => { | ||
if (isMore.value) | ||
return 'more' | ||
if (isLess.value) | ||
return 'less' | ||
if (isCustom.value) | ||
return 'custom' | ||
return 'no-preference' | ||
}) | ||
} |