Skip to content

feat(checkbox/radioButton): ensure minimum 48x48 hit target for better accessibility #3518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/components/checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,16 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
};
};

getAccessibleHitSlop() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it a pure function that gets the size and returns the hitslop

const {size = DEFAULT_SIZE} = this.props;
const hitTargetPadding = Math.max(0, (48 - size) / 2);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return Math.max(0, (48 - size) / 2);


return hitTargetPadding;
}

renderCheckbox() {
const {selectedIcon, label, testID, style, containerStyle, indeterminate, ...others} = this.props;

return (
//@ts-ignore
<TouchableOpacity
Expand All @@ -263,6 +271,7 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
{...others}
style={[this.getBorderStyle(), style, !label && containerStyle]}
onPress={this.onPress}
hitSlop={this.getAccessibleHitSlop()}
>
{
<Animated.View
Expand Down
11 changes: 11 additions & 0 deletions src/components/radioButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
);
}

getAccessibleHitSlop() {
const {size = DEFAULT_SIZE} = this.props;
const verticalPadding = Math.max(0, (48 - size) / 2);
Copy link
Collaborator

@Inbal-Tish Inbal-Tish Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only vertical?

Copy link
Collaborator

@nitzanyiz nitzanyiz Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Horizontally the CheckBox takes as much room as it can so it depends on the parent


return {
top: verticalPadding,
bottom: verticalPadding
};
}

render() {
const {onPress, onValueChange, containerStyle, contentOnLeft, ...others} = this.props;
const Container = onPress || onValueChange ? TouchableOpacity : View;
Expand All @@ -271,6 +281,7 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
style={containerStyle}
onPress={this.onPress}
{...this.getAccessibilityProps()}
hitSlop={Container === TouchableOpacity ? this.getAccessibleHitSlop() : undefined}
>
{!contentOnLeft && this.renderButton()}
{this.props.iconOnRight ? this.renderLabel() : this.renderIcon()}
Expand Down