Skip to content

feat(switch): ensure minimum 48x48 hit target for better accessibility #3517

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 7 commits into from
Feb 23, 2025
15 changes: 15 additions & 0 deletions src/components/switch/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,28 @@ class Switch extends Component<SwitchProps> {
return <Animated.View style={[this.styles.thumb, thumbPositionStyle, thumbStyle]}/>;
}

getAccessibleHitSlop() {
const {width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT} = this.props;
const verticalPadding = Math.max(0, (48 - height) / 2);
const horizontalPadding = Math.max(0, (48 - width) / 2);

return {
top: verticalPadding,
bottom: verticalPadding,
left: horizontalPadding,
right: horizontalPadding
Comment on lines +159 to +162
Copy link
Collaborator

Choose a reason for hiding this comment

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

The user should be allowed to override the hitSlop

};
}

render() {
const {...others} = this.props;

return (
// @ts-ignore
<TouchableOpacity
{...this.getAccessibilityProps()}
activeOpacity={1}
hitSlop={this.getAccessibleHitSlop()}
{...others}
style={this.getSwitchStyle()}
onPress={this.onPress}
Expand Down