Improve a11y

This commit is contained in:
Eric Bailey
2024-01-14 10:43:26 -06:00
parent 541e62514d
commit cb1bfe0e6f
+17 -4
View File
@@ -5,8 +5,10 @@ import {useTheme, atoms as a, web} from '#/alf'
import {Text} from '#/components/Typography' import {Text} from '#/components/Typography'
import {useInteractionState} from '#/components/hooks/useInteractionState' import {useInteractionState} from '#/components/hooks/useInteractionState'
import {StyleProp} from 'react-native' import {StyleProp} from 'react-native'
import {AccessibilityRole} from 'react-native'
type ItemState = { type ItemState = {
id: string
name: string name: string
value: boolean value: boolean
disabled: boolean disabled: boolean
@@ -16,6 +18,7 @@ type ItemState = {
} }
const ItemContext = React.createContext<ItemState>({ const ItemContext = React.createContext<ItemState>({
id: '',
name: '', name: '',
value: false, value: false,
disabled: false, disabled: false,
@@ -49,7 +52,9 @@ function Item({
disabled, disabled,
onChange, onChange,
style, style,
role,
}: ItemProps) { }: ItemProps) {
const labelId = React.useId()
const { const {
state: hovered, state: hovered,
onIn: onHoverIn, onIn: onHoverIn,
@@ -69,6 +74,7 @@ function Item({
const state = React.useMemo( const state = React.useMemo(
() => ({ () => ({
id: labelId,
name, name,
value, value,
disabled: disabled ?? false, disabled: disabled ?? false,
@@ -76,7 +82,7 @@ function Item({
pressed, pressed,
focused, focused,
}), }),
[name, value, disabled, hovered, pressed, focused], [labelId, name, value, disabled, hovered, pressed, focused],
) )
return ( return (
@@ -84,9 +90,14 @@ function Item({
<Pressable <Pressable
disabled={disabled} disabled={disabled}
aria-disabled={disabled ?? false} aria-disabled={disabled ?? false}
role="checkbox"
accessibilityRole="checkbox"
aria-checked={value} aria-checked={value}
aria-labelledby={labelId}
role={role}
accessibilityRole={role as AccessibilityRole}
accessibilityState={{
disabled: disabled ?? false,
selected: value,
}}
onPress={onPress} onPress={onPress}
onHoverIn={onHoverIn} onHoverIn={onHoverIn}
onHoverOut={onHoverOut} onHoverOut={onHoverOut}
@@ -185,6 +196,7 @@ function Group({
{React.cloneElement(child, { {React.cloneElement(child, {
// @ts-ignore TODO figure out children types // @ts-ignore TODO figure out children types
disabled: isDisabled, disabled: isDisabled,
role: role === 'radio' ? 'radio' : 'checkbox',
value: isSelected, value: isSelected,
onChange: itemOnChange, onChange: itemOnChange,
})} })}
@@ -198,9 +210,10 @@ function Group({
function Label({children}: React.PropsWithChildren<{}>) { function Label({children}: React.PropsWithChildren<{}>) {
const t = useTheme() const t = useTheme()
const {disabled} = React.useContext(ItemContext) const {id, disabled} = React.useContext(ItemContext)
return ( return (
<Text <Text
nativeID={id}
style={[ style={[
a.font_bold, a.font_bold,
{ {