Skip to content

Commit 8bc9999

Browse files
authored
Merge pull request #174 from adkenyon/useAccessibilityInfo-update
useAccessibilityInfo update
2 parents 1bd09df + b719ae3 commit 8bc9999

File tree

4 files changed

+78
-48
lines changed

4 files changed

+78
-48
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ yarn add @react-native-community/hooks
3737
```js
3838
import { useAccessibilityInfo } from '@react-native-community/hooks'
3939

40-
const { reduceMotionEnabled, screenReaderEnabled } = useAccessibilityInfo()
40+
const {
41+
boldTextEnabled,
42+
screenReaderEnabled,
43+
reduceMotionEnabled, // requires RN60 or newer
44+
grayscaleEnabled, // requires RN60 or newer
45+
invertColorsEnabled, // requires RN60 or newer
46+
reduceTransparencyEnabled // requires RN60 or newer
47+
} = useAccessibilityInfo()
4148
```
4249

4350
### `useAppState`

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@testing-library/react-native": "5.0.3",
2727
"@types/jest": "25.2.3",
2828
"@types/react": "16.9.34",
29-
"@types/react-native": "0.62.8",
29+
"@types/react-native": "0.62.9",
3030
"all-contributors-cli": "6.15.0",
3131
"auto": "9.26.8",
3232
"eslint": "7.0.0",

src/useAccessibilityInfo.ts

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,66 @@
11
import {useEffect, useState} from 'react'
2-
import {
3-
AccessibilityInfo,
4-
AccessibilityChangeEvent,
5-
AccessibilityEvent,
6-
} from 'react-native'
2+
import {AccessibilityInfo, AccessibilityChangeEventName} from 'react-native'
73

8-
export function useAccessibilityInfo() {
9-
const [reduceMotionEnabled, setReduceMotionEnabled] = useState(false)
10-
const [screenReaderEnabled, setScreenReaderEnabled] = useState(false)
4+
type AccessibilityInfoStaticInitializers =
5+
| 'isBoldTextEnabled'
6+
| 'isScreenReaderEnabled'
7+
| 'isGrayscaleEnabled'
8+
| 'isInvertColorsEnabled'
9+
| 'isReduceMotionEnabled'
10+
| 'isReduceTransparencyEnabled'
1111

12-
const handleReduceMotionChanged = (enabled: AccessibilityChangeEvent) =>
13-
setReduceMotionEnabled(enabled)
14-
const handleScreenReaderChanged = (enabled: AccessibilityChangeEvent) =>
15-
setScreenReaderEnabled(enabled)
12+
function useAccessibilityStateListener(
13+
eventName: AccessibilityChangeEventName,
14+
initializerName: AccessibilityInfoStaticInitializers,
15+
) {
16+
const [isEnabled, setIsEnabled] = useState<boolean | undefined>(undefined)
1617

1718
useEffect(() => {
18-
AccessibilityInfo.isReduceMotionEnabled().then(handleReduceMotionChanged)
19-
AccessibilityInfo.isScreenReaderEnabled().then(handleScreenReaderChanged)
20-
21-
AccessibilityInfo.addEventListener(
22-
'reduceMotionChanged',
23-
handleReduceMotionChanged as (event: AccessibilityEvent) => void,
24-
)
25-
AccessibilityInfo.addEventListener(
26-
'screenReaderChanged',
27-
handleScreenReaderChanged as (event: AccessibilityEvent) => void,
28-
)
29-
30-
return () => {
31-
AccessibilityInfo.removeEventListener(
32-
'reduceMotionChanged',
33-
handleReduceMotionChanged as (event: AccessibilityEvent) => void,
34-
)
35-
AccessibilityInfo.removeEventListener(
36-
'screenReaderChanged',
37-
handleScreenReaderChanged as (event: AccessibilityEvent) => void,
38-
)
19+
if (!AccessibilityInfo[initializerName]) {
20+
return
3921
}
40-
}, [])
4122

42-
return {reduceMotionEnabled, screenReaderEnabled}
23+
AccessibilityInfo[initializerName]().then(setIsEnabled)
24+
AccessibilityInfo.addEventListener(eventName, setIsEnabled)
25+
26+
return () => AccessibilityInfo.removeEventListener(eventName, setIsEnabled)
27+
}, [eventName, initializerName])
28+
29+
return isEnabled
30+
}
31+
32+
export function useAccessibilityInfo() {
33+
const boldTextEnabled = useAccessibilityStateListener(
34+
'boldTextChanged',
35+
'isBoldTextEnabled',
36+
)
37+
const grayscaleEnabled = useAccessibilityStateListener(
38+
'grayscaleChanged',
39+
'isGrayscaleEnabled',
40+
)
41+
const invertColorsEnabled = useAccessibilityStateListener(
42+
'invertColorsChanged',
43+
'isInvertColorsEnabled',
44+
)
45+
const reduceMotionEnabled = useAccessibilityStateListener(
46+
'reduceMotionChanged',
47+
'isReduceMotionEnabled',
48+
)
49+
const reduceTransparencyEnabled = useAccessibilityStateListener(
50+
'reduceTransparencyChanged',
51+
'isReduceTransparencyEnabled',
52+
)
53+
const screenReaderEnabled = useAccessibilityStateListener(
54+
'screenReaderChanged',
55+
'isScreenReaderEnabled',
56+
)
57+
58+
return {
59+
screenReaderEnabled,
60+
grayscaleEnabled,
61+
invertColorsEnabled,
62+
reduceMotionEnabled,
63+
reduceTransparencyEnabled,
64+
boldTextEnabled,
65+
}
4366
}

yarn.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,13 +1614,13 @@
16141614

16151615
"@types/prop-types@*":
16161616
version "15.7.3"
1617-
resolved "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7"
1617+
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7"
16181618
integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw==
16191619

1620-
"@types/[email protected].8":
1621-
version "0.62.8"
1622-
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.62.8.tgz#224602561f75b838ed6e3b5ea37093bb84cffd74"
1623-
integrity sha512-YEf0tH3xYJpQB12Vvzoy7cqPY3mvbbulTnG3G7ToKOuJuqigAN3K9NNAaxNAAm1zCj+UtObhzcaJtPRwX1z6Fw==
1620+
"@types/[email protected].9":
1621+
version "0.62.9"
1622+
resolved "https://registry.yarnpkg.com/@types/react-native/-/react-native-0.62.9.tgz#f75d4a8879e68ed3857d6f2f73dc0752a0505362"
1623+
integrity sha512-OcoE7SKz1PsvTGJK5fIwJu6kWdDFN+hH1vMI4GVTEBYhV5FAM5vKVUFCaSiEPJScyNyIEWAeQwFvI3a01+Grzg==
16241624
dependencies:
16251625
"@types/react" "*"
16261626

@@ -1632,9 +1632,9 @@
16321632
"@types/react" "*"
16331633

16341634
"@types/react@*":
1635-
version "16.9.25"
1636-
resolved "https://registry.npmjs.org/@types/react/-/react-16.9.25.tgz#6ae2159b40138c792058a23c3c04fd3db49e929e"
1637-
integrity sha512-Dlj2V72cfYLPNscIG3/SMUOzhzj7GK3bpSrfefwt2YT9GLynvLCCZjbhyF6VsT0q0+aRACRX03TDJGb7cA0cqg==
1635+
version "16.9.35"
1636+
resolved "https://registry.yarnpkg.com/@types/react/-/react-16.9.35.tgz#a0830d172e8aadd9bd41709ba2281a3124bbd368"
1637+
integrity sha512-q0n0SsWcGc8nDqH2GJfWQWUOmZSJhXV64CjVN5SvcNti3TdEaA3AH0D8DwNmMdzjMAC/78tB8nAZIlV8yTz+zQ==
16381638
dependencies:
16391639
"@types/prop-types" "*"
16401640
csstype "^2.2.0"
@@ -2823,9 +2823,9 @@ cssstyle@^2.2.0:
28232823
cssom "~0.3.6"
28242824

28252825
csstype@^2.2.0:
2826-
version "2.6.9"
2827-
resolved "https://registry.npmjs.org/csstype/-/csstype-2.6.9.tgz#05141d0cd557a56b8891394c1911c40c8a98d098"
2828-
integrity sha512-xz39Sb4+OaTsULgUERcCk+TJj8ylkL4aSVDQiX/ksxbELSqwkgt4d4RD7fovIdgJGSuNYqwZEiVjYY5l0ask+Q==
2826+
version "2.6.10"
2827+
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.10.tgz#e63af50e66d7c266edb6b32909cfd0aabe03928b"
2828+
integrity sha512-D34BqZU4cIlMCY93rZHbrq9pjTAQJ3U8S8rfBqjwHxkGPThWFjzZDQpgMJY0QViLxth6ZKYiwFBo14RdN44U/w==
28292829

28302830
dashdash@^1.12.0:
28312831
version "1.14.1"

0 commit comments

Comments
 (0)