-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from adkenyon/useAccessibilityInfo-update
useAccessibilityInfo update
- Loading branch information
Showing
4 changed files
with
78 additions
and
48 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 |
---|---|---|
@@ -1,43 +1,66 @@ | ||
import {useEffect, useState} from 'react' | ||
import { | ||
AccessibilityInfo, | ||
AccessibilityChangeEvent, | ||
AccessibilityEvent, | ||
} from 'react-native' | ||
import {AccessibilityInfo, AccessibilityChangeEventName} from 'react-native' | ||
|
||
export function useAccessibilityInfo() { | ||
const [reduceMotionEnabled, setReduceMotionEnabled] = useState(false) | ||
const [screenReaderEnabled, setScreenReaderEnabled] = useState(false) | ||
type AccessibilityInfoStaticInitializers = | ||
| 'isBoldTextEnabled' | ||
| 'isScreenReaderEnabled' | ||
| 'isGrayscaleEnabled' | ||
| 'isInvertColorsEnabled' | ||
| 'isReduceMotionEnabled' | ||
| 'isReduceTransparencyEnabled' | ||
|
||
const handleReduceMotionChanged = (enabled: AccessibilityChangeEvent) => | ||
setReduceMotionEnabled(enabled) | ||
const handleScreenReaderChanged = (enabled: AccessibilityChangeEvent) => | ||
setScreenReaderEnabled(enabled) | ||
function useAccessibilityStateListener( | ||
eventName: AccessibilityChangeEventName, | ||
initializerName: AccessibilityInfoStaticInitializers, | ||
) { | ||
const [isEnabled, setIsEnabled] = useState<boolean | undefined>(undefined) | ||
|
||
useEffect(() => { | ||
AccessibilityInfo.isReduceMotionEnabled().then(handleReduceMotionChanged) | ||
AccessibilityInfo.isScreenReaderEnabled().then(handleScreenReaderChanged) | ||
|
||
AccessibilityInfo.addEventListener( | ||
'reduceMotionChanged', | ||
handleReduceMotionChanged as (event: AccessibilityEvent) => void, | ||
) | ||
AccessibilityInfo.addEventListener( | ||
'screenReaderChanged', | ||
handleScreenReaderChanged as (event: AccessibilityEvent) => void, | ||
) | ||
|
||
return () => { | ||
AccessibilityInfo.removeEventListener( | ||
'reduceMotionChanged', | ||
handleReduceMotionChanged as (event: AccessibilityEvent) => void, | ||
) | ||
AccessibilityInfo.removeEventListener( | ||
'screenReaderChanged', | ||
handleScreenReaderChanged as (event: AccessibilityEvent) => void, | ||
) | ||
if (!AccessibilityInfo[initializerName]) { | ||
return | ||
} | ||
}, []) | ||
|
||
return {reduceMotionEnabled, screenReaderEnabled} | ||
AccessibilityInfo[initializerName]().then(setIsEnabled) | ||
AccessibilityInfo.addEventListener(eventName, setIsEnabled) | ||
|
||
return () => AccessibilityInfo.removeEventListener(eventName, setIsEnabled) | ||
}, [eventName, initializerName]) | ||
|
||
return isEnabled | ||
} | ||
|
||
export function useAccessibilityInfo() { | ||
const boldTextEnabled = useAccessibilityStateListener( | ||
'boldTextChanged', | ||
'isBoldTextEnabled', | ||
) | ||
const grayscaleEnabled = useAccessibilityStateListener( | ||
'grayscaleChanged', | ||
'isGrayscaleEnabled', | ||
) | ||
const invertColorsEnabled = useAccessibilityStateListener( | ||
'invertColorsChanged', | ||
'isInvertColorsEnabled', | ||
) | ||
const reduceMotionEnabled = useAccessibilityStateListener( | ||
'reduceMotionChanged', | ||
'isReduceMotionEnabled', | ||
) | ||
const reduceTransparencyEnabled = useAccessibilityStateListener( | ||
'reduceTransparencyChanged', | ||
'isReduceTransparencyEnabled', | ||
) | ||
const screenReaderEnabled = useAccessibilityStateListener( | ||
'screenReaderChanged', | ||
'isScreenReaderEnabled', | ||
) | ||
|
||
return { | ||
screenReaderEnabled, | ||
grayscaleEnabled, | ||
invertColorsEnabled, | ||
reduceMotionEnabled, | ||
reduceTransparencyEnabled, | ||
boldTextEnabled, | ||
} | ||
} |
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