Created
January 31, 2022 02:51
-
-
Save martsie/4f3bf6e3c67cf032ba562745a9c161af to your computer and use it in GitHub Desktop.
useReduceMotion hook for ReactNative
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
import { useEffect, useState } from 'react' | |
import { AccessibilityInfo } from 'react-native' | |
// Adapted from https://github.com/infiniteluke/react-reduce-motion/blob/master/src/targets/native/index.js | |
export const useReduceMotion = (): boolean => { | |
const [shouldReduceMotion, setShouldReduceMotion] = useState(false) | |
useEffect(() => { | |
const handleChange = (isReduceMotionEnabled: boolean): void => { | |
setShouldReduceMotion(isReduceMotionEnabled) | |
} | |
async function init(): Promise<void> { | |
handleChange(await AccessibilityInfo.isReduceMotionEnabled()) | |
} | |
init() | |
AccessibilityInfo.addEventListener('reduceMotionChanged', handleChange) | |
return (): void => { | |
AccessibilityInfo.removeEventListener('reduceMotionChanged', handleChange) | |
} | |
}, []) | |
return shouldReduceMotion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment