File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change 1
1
export * from "./useColorSchemePreference" ;
2
2
export * from "./useFloating" ;
3
3
export * from "./useFormValues" ;
4
+ export * from "./useReducedMotionPreference" ;
4
5
export * from "./useWindowSize" ;
Original file line number Diff line number Diff line change
1
+ import { useEffect , useRef , useState } from "react" ;
2
+
3
+ /** Provides the user's reduced motion preference and subscribes to changes. */
4
+ export const useReducedMotionPreference = ( ) : boolean => {
5
+ const match = useRef < MediaQueryList > (
6
+ window . matchMedia ( "(prefers-reduced-motion)" ) ,
7
+ ) ;
8
+ const [ isReducedPreferred , setIsReducedPreferred ] = useState (
9
+ ! ! match . current . matches ,
10
+ ) ;
11
+
12
+ useEffect ( ( ) => {
13
+ const currentMatch = match . current ;
14
+ const onChange = ( ) => {
15
+ setIsReducedPreferred ( ! ! currentMatch . matches ) ;
16
+ } ;
17
+
18
+ currentMatch . addEventListener ( "change" , onChange ) ;
19
+ return ( ) => currentMatch . removeEventListener ( "change" , onChange ) ;
20
+ } , [ match ] ) ;
21
+
22
+ return isReducedPreferred ;
23
+ } ;
You can’t perform that action at this time.
0 commit comments