|
1 | 1 | import {useEffect, useState} from 'react'
|
2 |
| -import { |
3 |
| - AccessibilityInfo, |
4 |
| - AccessibilityChangeEvent, |
5 |
| - AccessibilityEvent, |
6 |
| -} from 'react-native' |
| 2 | +import {AccessibilityInfo, AccessibilityEvent} from 'react-native' |
7 | 3 |
|
8 |
| -export function useAccessibilityInfo() { |
9 |
| - const [reduceMotionEnabled, setReduceMotionEnabled] = useState(false) |
10 |
| - const [screenReaderEnabled, setScreenReaderEnabled] = useState(false) |
| 4 | +type AccessibilityEventName = |
| 5 | + | 'boldTextChanged' // iOS-only Event |
| 6 | + | 'grayscaleChanged' // iOS-only Event |
| 7 | + | 'invertColorsChanged' // iOS-only Event |
| 8 | + | 'reduceMotionChanged' |
| 9 | + | 'screenReaderChanged' |
| 10 | + | 'reduceTransparencyChanged' // iOS-only Event |
| 11 | + |
| 12 | +type AccessibilityInfoStaticInitializers = |
| 13 | + | 'isBoldTextEnabled' |
| 14 | + | 'isScreenReaderEnabled' |
| 15 | + | 'isGrayscaleEnabled' |
| 16 | + | 'isInvertColorsEnabled' |
| 17 | + | 'isReduceMotionEnabled' |
| 18 | + | 'isReduceTransparencyEnabled' |
11 | 19 |
|
12 |
| - const handleReduceMotionChanged = (enabled: AccessibilityChangeEvent) => |
13 |
| - setReduceMotionEnabled(enabled) |
14 |
| - const handleScreenReaderChanged = (enabled: AccessibilityChangeEvent) => |
15 |
| - setScreenReaderEnabled(enabled) |
| 20 | +type AccessibilityEventToInfoStaticKeyMap = { |
| 21 | + [K in AccessibilityEventName]?: AccessibilityInfoStaticInitializers |
| 22 | +} |
| 23 | + |
| 24 | +const EVENT_NAME_TO_INITIALIZER: AccessibilityEventToInfoStaticKeyMap = { |
| 25 | + boldTextChanged: 'isBoldTextEnabled', |
| 26 | + screenReaderChanged: 'isScreenReaderEnabled', |
| 27 | + grayscaleChanged: 'isGrayscaleEnabled', |
| 28 | + invertColorsChanged: 'isInvertColorsEnabled', |
| 29 | + reduceMotionChanged: 'isReduceMotionEnabled', |
| 30 | + reduceTransparencyChanged: 'isReduceTransparencyEnabled', |
| 31 | +} |
| 32 | + |
| 33 | +type AccessibilityInfoChangeEventHandler = (event: AccessibilityEvent) => void |
| 34 | + |
| 35 | +function useAccessibilityStateListener( |
| 36 | + eventName: AccessibilityEventName, |
| 37 | +): boolean { |
| 38 | + const [isEnabled, setIsEnabled] = useState(false) |
16 | 39 |
|
17 | 40 | useEffect(() => {
|
18 |
| - AccessibilityInfo.isReduceMotionEnabled().then(handleReduceMotionChanged) |
19 |
| - AccessibilityInfo.isScreenReaderEnabled().then(handleScreenReaderChanged) |
| 41 | + const initializerKey = EVENT_NAME_TO_INITIALIZER[eventName] |
20 | 42 |
|
| 43 | + if (!initializerKey) { |
| 44 | + return |
| 45 | + } |
| 46 | + |
| 47 | + AccessibilityInfo[initializerKey]().then(setIsEnabled) |
21 | 48 | AccessibilityInfo.addEventListener(
|
22 |
| - 'reduceMotionChanged', |
23 |
| - handleReduceMotionChanged as (event: AccessibilityEvent) => void, |
24 |
| - ) |
25 |
| - AccessibilityInfo.addEventListener( |
26 |
| - 'screenReaderChanged', |
27 |
| - handleScreenReaderChanged as (event: AccessibilityEvent) => void, |
| 49 | + eventName, |
| 50 | + <AccessibilityInfoChangeEventHandler>setIsEnabled, |
28 | 51 | )
|
29 | 52 |
|
30 |
| - return () => { |
| 53 | + return () => |
31 | 54 | AccessibilityInfo.removeEventListener(
|
32 |
| - 'reduceMotionChanged', |
33 |
| - handleReduceMotionChanged as (event: AccessibilityEvent) => void, |
| 55 | + eventName, |
| 56 | + <AccessibilityInfoChangeEventHandler>setIsEnabled, |
34 | 57 | )
|
35 |
| - AccessibilityInfo.removeEventListener( |
36 |
| - 'screenReaderChanged', |
37 |
| - handleScreenReaderChanged as (event: AccessibilityEvent) => void, |
38 |
| - ) |
39 |
| - } |
40 |
| - }, []) |
| 58 | + }, [eventName]) |
| 59 | + |
| 60 | + return isEnabled |
| 61 | +} |
| 62 | + |
| 63 | +export function useAccessibilityInfo() { |
| 64 | + const screenReaderEnabled = useAccessibilityStateListener( |
| 65 | + 'screenReaderChanged', |
| 66 | + ) |
| 67 | + const greyScaleEnabled = useAccessibilityStateListener('grayscaleChanged') |
| 68 | + const boldTextEnabled = useAccessibilityStateListener('boldTextChanged') |
| 69 | + const invertColorsEnabled = useAccessibilityStateListener( |
| 70 | + 'invertColorsChanged', |
| 71 | + ) |
| 72 | + const reduceMotionEnabled = useAccessibilityStateListener( |
| 73 | + 'reduceMotionChanged', |
| 74 | + ) |
| 75 | + const reduceTransparencyEnabled = useAccessibilityStateListener( |
| 76 | + 'reduceTransparencyChanged', |
| 77 | + ) |
41 | 78 |
|
42 |
| - return {reduceMotionEnabled, screenReaderEnabled} |
| 79 | + return { |
| 80 | + screenReaderEnabled, |
| 81 | + greyScaleEnabled, |
| 82 | + invertColorsEnabled, |
| 83 | + reduceMotionEnabled, |
| 84 | + reduceTransparencyEnabled, |
| 85 | + boldTextEnabled, |
| 86 | + } |
43 | 87 | }
|
0 commit comments