Use global detection

This commit is contained in:
Zhiyuan Zheng 2022-05-07 13:23:08 +02:00
parent 7c48c61c99
commit ea0b85365e
2 changed files with 21 additions and 24 deletions

View File

@ -1,6 +1,6 @@
import { useAccessibility } from '@utils/accessibility/AccessibilityManager'
import { StyleConstants } from '@utils/styles/constants' import { StyleConstants } from '@utils/styles/constants'
import { useEffect, useState } from 'react' import { Text, TextProps, TextStyle } from 'react-native'
import { AccessibilityInfo, Text, TextProps, TextStyle } from 'react-native'
type Props = type Props =
| { | {
@ -27,23 +27,8 @@ const CustomText: React.FC<Props & TextProps> = ({
lineHeight, lineHeight,
...rest ...rest
}) => { }) => {
const [isBoldText, setIsBoldText] = useState(false) const { boldTextEnabled } = useAccessibility()
useEffect(() => {
const boldTextChangedSubscription = AccessibilityInfo.addEventListener(
'boldTextChanged',
boldTextChanged => {
setIsBoldText(boldTextChanged)
}
)
AccessibilityInfo.isBoldTextEnabled().then(boldTextEnabled => {
setIsBoldText(boldTextEnabled)
})
return () => {
boldTextChangedSubscription.remove()
}
}, [])
enum BoldMapping { enum BoldMapping {
'Normal' = '600', 'Normal' = '600',
'Bold' = '800' 'Bold' = '800'
@ -61,15 +46,14 @@ const CustomText: React.FC<Props & TextProps> = ({
}) })
}, },
{ {
fontWeight: isBoldText fontWeight: boldTextEnabled
? BoldMapping[fontWeight] ? BoldMapping[fontWeight]
: StyleConstants.Font.Weight[fontWeight] : StyleConstants.Font.Weight[fontWeight]
} }
]} ]}
{...rest} {...rest}
> children={children}
{children} />
</Text>
) )
} }

View File

@ -4,11 +4,13 @@ import { AccessibilityInfo } from 'react-native'
type ContextType = { type ContextType = {
reduceMotionEnabled: boolean reduceMotionEnabled: boolean
screenReaderEnabled: boolean screenReaderEnabled: boolean
boldTextEnabled: boolean
} }
const AccessibilityContext = createContext<ContextType>({ const AccessibilityContext = createContext<ContextType>({
reduceMotionEnabled: false, reduceMotionEnabled: false,
screenReaderEnabled: false screenReaderEnabled: false,
boldTextEnabled: false
}) })
export const useAccessibility = () => useContext(AccessibilityContext) export const useAccessibility = () => useContext(AccessibilityContext)
@ -16,6 +18,7 @@ export const useAccessibility = () => useContext(AccessibilityContext)
const AccessibilityManager: React.FC = ({ children }) => { const AccessibilityManager: React.FC = ({ children }) => {
const [reduceMotionEnabled, setReduceMotionEnabled] = useState(false) const [reduceMotionEnabled, setReduceMotionEnabled] = useState(false)
const [screenReaderEnabled, setScreenReaderEnabled] = useState(false) const [screenReaderEnabled, setScreenReaderEnabled] = useState(false)
const [boldTextEnabled, setBoldTextEnabled] = useState(false)
const handleReduceMotionChanged = (reduceMotionEnabled: boolean) => const handleReduceMotionChanged = (reduceMotionEnabled: boolean) =>
setReduceMotionEnabled(reduceMotionEnabled) setReduceMotionEnabled(reduceMotionEnabled)
@ -23,11 +26,16 @@ const AccessibilityManager: React.FC = ({ children }) => {
const handleScreenReaderEnabled = (screenReaderEnabled: boolean) => const handleScreenReaderEnabled = (screenReaderEnabled: boolean) =>
setScreenReaderEnabled(screenReaderEnabled) setScreenReaderEnabled(screenReaderEnabled)
const handleBoldTextEnabled = (boldTextEnabled: boolean) =>
setBoldTextEnabled(boldTextEnabled)
const loadAccessibilityInfo = async () => { const loadAccessibilityInfo = async () => {
const reduceMotion = await AccessibilityInfo.isReduceMotionEnabled() const reduceMotion = await AccessibilityInfo.isReduceMotionEnabled()
const screenReader = await AccessibilityInfo.isScreenReaderEnabled() const screenReader = await AccessibilityInfo.isScreenReaderEnabled()
const boldText = await AccessibilityInfo.isBoldTextEnabled()
setReduceMotionEnabled(reduceMotion) setReduceMotionEnabled(reduceMotion)
setScreenReaderEnabled(screenReader) setScreenReaderEnabled(screenReader)
setBoldTextEnabled(boldText)
} }
useEffect(() => { useEffect(() => {
@ -41,16 +49,21 @@ const AccessibilityManager: React.FC = ({ children }) => {
'screenReaderChanged', 'screenReaderChanged',
handleScreenReaderEnabled handleScreenReaderEnabled
) )
const boldTextSubscription = AccessibilityInfo.addEventListener(
'boldTextChanged',
handleBoldTextEnabled
)
return () => { return () => {
reduceMotionSubscription.remove() reduceMotionSubscription.remove()
screenReaderSubscription.remove() screenReaderSubscription.remove()
boldTextSubscription.remove()
} }
}, []) }, [])
return ( return (
<AccessibilityContext.Provider <AccessibilityContext.Provider
value={{ reduceMotionEnabled, screenReaderEnabled }} value={{ reduceMotionEnabled, screenReaderEnabled, boldTextEnabled }}
> >
{children} {children}
</AccessibilityContext.Provider> </AccessibilityContext.Provider>