-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
111 additions
and
40 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,65 @@ | ||
import React from 'react' | ||
import {AccessibilityInfo} from 'react-native' | ||
import {isReducedMotion} from 'react-native-reanimated' | ||
|
||
import {isWeb} from '#/platform/detection' | ||
|
||
const Context = React.createContext({ | ||
reduceMotionEnabled: false, | ||
screenReaderEnabled: false, | ||
}) | ||
|
||
export function useA11y() { | ||
return React.useContext(Context) | ||
} | ||
|
||
export function Provider({children}: React.PropsWithChildren<{}>) { | ||
const [reduceMotionEnabled, setReduceMotionEnabled] = React.useState(() => | ||
isReducedMotion(), | ||
) | ||
const [screenReaderEnabled, setScreenReaderEnabled] = React.useState(false) | ||
|
||
React.useEffect(() => { | ||
const reduceMotionChangedSubscription = AccessibilityInfo.addEventListener( | ||
'reduceMotionChanged', | ||
enabled => { | ||
setReduceMotionEnabled(enabled) | ||
}, | ||
) | ||
const screenReaderChangedSubscription = AccessibilityInfo.addEventListener( | ||
'screenReaderChanged', | ||
enabled => { | ||
setScreenReaderEnabled(enabled) | ||
}, | ||
) | ||
|
||
;(async () => { | ||
const [_reduceMotionEnabled, _screenReaderEnabled] = await Promise.all([ | ||
AccessibilityInfo.isReduceMotionEnabled(), | ||
AccessibilityInfo.isScreenReaderEnabled(), | ||
]) | ||
setReduceMotionEnabled(_reduceMotionEnabled) | ||
setScreenReaderEnabled(_screenReaderEnabled) | ||
})() | ||
|
||
return () => { | ||
reduceMotionChangedSubscription.remove() | ||
screenReaderChangedSubscription.remove() | ||
} | ||
}, []) | ||
|
||
const ctx = React.useMemo(() => { | ||
return { | ||
reduceMotionEnabled, | ||
/** | ||
* Always returns true on web. For now, we're using this for mobile a11y, | ||
* so we reset to false on web. | ||
* | ||
* @see https://github.com/necolas/react-native-web/discussions/2072 | ||
*/ | ||
screenReaderEnabled: isWeb ? false : screenReaderEnabled, | ||
} | ||
}, [reduceMotionEnabled, screenReaderEnabled]) | ||
|
||
return <Context.Provider value={ctx}>{children}</Context.Provider> | ||
} |