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 4 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
15 changes: 15 additions & 0 deletions src/components/checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,22 @@ 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 verticalPadding = Math.max(0, (48 - size) / 2);
const horizontalPadding = verticalPadding;

return {
top: verticalPadding,
bottom: verticalPadding,
left: horizontalPadding,
right: horizontalPadding
};
}

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

return (
//@ts-ignore
<TouchableOpacity
Expand All @@ -263,6 +277,7 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
{...others}
style={[this.getBorderStyle(), style, !label && containerStyle]}
onPress={this.onPress}
hitSlop={this.getAccessibleHitSlop()}
>
{
<Animated.View
Expand Down
20 changes: 17 additions & 3 deletions src/components/radioButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,21 @@ 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

const horizontalPadding = verticalPadding;

return {
top: verticalPadding,
bottom: verticalPadding,
left: horizontalPadding,
right: horizontalPadding
};
}

render() {
const {onPress, onValueChange, containerStyle, contentOnLeft, ...others} = this.props;
const {onPress, onValueChange, containerStyle, contentOnLeft, iconOnRight, ...others} = this.props;
const Container = onPress || onValueChange ? TouchableOpacity : View;

return (
Expand All @@ -271,10 +284,11 @@ 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()}
{this.props.iconOnRight ? this.renderIcon() : this.renderLabel()}
{iconOnRight ? this.renderLabel() : this.renderIcon()}
{iconOnRight ? this.renderIcon() : this.renderLabel()}
{contentOnLeft && this.renderButton()}
</Container>
);
Expand Down