Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usePreferredReducedMotion): new function (#2039)
- Loading branch information
1 parent
b6503dc
commit e808b68
Showing
6 changed files
with
74 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 { usePreferredReducedMotion } from '@vueuse/core' | ||
|
||
export const UsePreferredReducedMotion = defineComponent({ | ||
name: 'UsePreferredReducedMotion', | ||
setup(props, { slots }) { | ||
const data = reactive({ | ||
motion: usePreferredReducedMotion(), | ||
}) | ||
|
||
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 { usePreferredReducedMotion } from '@vueuse/core' | ||
const motion = usePreferredReducedMotion() | ||
</script> | ||
|
||
<template> | ||
<note class="mb-2"> | ||
Preferred Motion: | ||
</note> | ||
<code>{{ motion }}</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 | ||
--- | ||
|
||
# usePreferredReducedMotion | ||
|
||
Reactive [prefers-reduced-motion](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion) media query. | ||
|
||
## Usage | ||
|
||
```js | ||
import { usePreferredReducedMotion } from '@vueuse/core' | ||
|
||
const preferredMotion = usePreferredReducedMotion() | ||
``` | ||
|
||
## Component Usage | ||
|
||
```html | ||
<UsePreferredReducedMotion v-slot="{ motion }"> | ||
Preferred Color Scheme: {{ motion }} | ||
<UsePreferredReducedMotion> | ||
``` |
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,21 @@ | ||
import { computed } from 'vue-demi' | ||
import { useMediaQuery } from '../useMediaQuery' | ||
import type { ConfigurableWindow } from '../_configurable' | ||
|
||
export type ReducedMotionType = 'reduce' | 'no-preference' | ||
|
||
/** | ||
* Reactive prefers-reduced-motion media query. | ||
* | ||
* @see https://vueuse.org/usePreferredReducedMotion | ||
* @param [options] | ||
*/ | ||
export function usePreferredReducedMotion(options?: ConfigurableWindow) { | ||
const isReduced = useMediaQuery('(prefers-reduced-motion: reduce)', options) | ||
|
||
return computed<ReducedMotionType>(() => { | ||
if (isReduced.value) | ||
return 'reduce' | ||
return 'no-preference' | ||
}) | ||
} |