mirror of
https://github.com/tooot-app/app
synced 2025-06-05 22:19:13 +02:00
Using new text component
Need to use global accessibility checks rather than per text component which is not efficient
This commit is contained in:
76
src/components/Text.tsx
Normal file
76
src/components/Text.tsx
Normal file
@ -0,0 +1,76 @@
|
||||
import { StyleConstants } from '@utils/styles/constants'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { AccessibilityInfo, Text, TextProps, TextStyle } from 'react-native'
|
||||
|
||||
type Props =
|
||||
| {
|
||||
style?: Omit<TextStyle, 'fontSize' | 'lineHeight' | 'fontWeight'>
|
||||
fontStyle?: undefined
|
||||
fontSize?: 'S' | 'M' | 'L'
|
||||
lineHeight?: 'S' | 'M' | 'L'
|
||||
fontWeight?: 'Normal' | 'Bold'
|
||||
}
|
||||
| {
|
||||
style?: Omit<TextStyle, 'fontSize' | 'lineHeight' | 'fontWeight'>
|
||||
fontStyle: 'S' | 'M' | 'L'
|
||||
fontSize?: undefined
|
||||
lineHeight?: undefined
|
||||
fontWeight?: 'Normal' | 'Bold'
|
||||
}
|
||||
|
||||
const CustomText: React.FC<Props & TextProps> = ({
|
||||
children,
|
||||
style,
|
||||
fontStyle,
|
||||
fontSize,
|
||||
fontWeight = 'Normal',
|
||||
lineHeight,
|
||||
...rest
|
||||
}) => {
|
||||
const [isBoldText, setIsBoldText] = useState(false)
|
||||
useEffect(() => {
|
||||
const boldTextChangedSubscription = AccessibilityInfo.addEventListener(
|
||||
'boldTextChanged',
|
||||
boldTextChanged => {
|
||||
setIsBoldText(boldTextChanged)
|
||||
}
|
||||
)
|
||||
|
||||
AccessibilityInfo.isBoldTextEnabled().then(boldTextEnabled => {
|
||||
setIsBoldText(boldTextEnabled)
|
||||
})
|
||||
|
||||
return () => {
|
||||
boldTextChangedSubscription.remove()
|
||||
}
|
||||
}, [])
|
||||
enum BoldMapping {
|
||||
'Normal' = '600',
|
||||
'Bold' = '800'
|
||||
}
|
||||
|
||||
return (
|
||||
<Text
|
||||
style={[
|
||||
style,
|
||||
{ ...(fontStyle && StyleConstants.FontStyle[fontStyle]) },
|
||||
{ ...(fontSize && { fontSize: StyleConstants.Font.Size[fontSize] }) },
|
||||
{
|
||||
...(lineHeight && {
|
||||
lineHeight: StyleConstants.Font.LineHeight[lineHeight]
|
||||
})
|
||||
},
|
||||
{
|
||||
fontWeight: isBoldText
|
||||
? BoldMapping[fontWeight]
|
||||
: StyleConstants.Font.Weight[fontWeight]
|
||||
}
|
||||
]}
|
||||
{...rest}
|
||||
>
|
||||
{children}
|
||||
</Text>
|
||||
)
|
||||
}
|
||||
|
||||
export default CustomText
|
Reference in New Issue
Block a user