Skip to content

Commit d6c2579

Browse files
feat(switch): ensure minimum 48x48 hit target for better accessibility (#3517)
* feat(switch): ensure minimum 48x48 hit target for better accessibility Co-Authored-By: nitzany@wix.com <nitzany@wix.com> * refactor: destructure props at top of render function Co-Authored-By: nitzany@wix.com <nitzany@wix.com> * refactor(switch): move hitSlop calculation to dedicated function Co-Authored-By: nitzany@wix.com <nitzany@wix.com> * refactor(switch): rename getHitSlopSize to getAccessibleHitSlop Co-Authored-By: nitzany@wix.com <nitzany@wix.com> * feat(switch): allow hitSlop override while maintaining default accessibility Co-Authored-By: nitzany@wix.com <nitzany@wix.com> * fix(switch): remove redundant hitSlop prop and fix prop spreading order Co-Authored-By: nitzany@wix.com <nitzany@wix.com> * chore(switch): remove unused import Co-Authored-By: nitzany@wix.com <nitzany@wix.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: nitzany@wix.com <nitzany@wix.com>
1 parent cc80598 commit d6c2579

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/components/switch/index.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,28 @@ class Switch extends Component<SwitchProps> {
150150
return <Animated.View style={[this.styles.thumb, thumbPositionStyle, thumbStyle]}/>;
151151
}
152152

153+
getAccessibleHitSlop() {
154+
const {width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT} = this.props;
155+
const verticalPadding = Math.max(0, (48 - height) / 2);
156+
const horizontalPadding = Math.max(0, (48 - width) / 2);
157+
158+
return {
159+
top: verticalPadding,
160+
bottom: verticalPadding,
161+
left: horizontalPadding,
162+
right: horizontalPadding
163+
};
164+
}
165+
153166
render() {
154167
const {...others} = this.props;
168+
155169
return (
156170
// @ts-ignore
157171
<TouchableOpacity
158172
{...this.getAccessibilityProps()}
159173
activeOpacity={1}
174+
hitSlop={this.getAccessibleHitSlop()}
160175
{...others}
161176
style={this.getSwitchStyle()}
162177
onPress={this.onPress}

0 commit comments

Comments
 (0)