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