Skip to content

Commit aae880e

Browse files
adids1221M-i-k-e-l
andauthored
Carousel - page control hit slop fix a11y (#3763)
* Add accessible hit slop calculation to PageControl component * getAccessibleHitSlop moved to StyleUtils * Refactor hit slop calculation in PageControl component for improved accessibility * Refactor hit slop calculation for consistency and improved accessibility --------- Co-authored-by: Miki Leib <[email protected]>
1 parent d3a3e0e commit aae880e

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

src/components/checkbox/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ImageStyle
1111
} from 'react-native';
1212
import {Colors, Spacings} from '../../style';
13+
import {StyleUtils} from '../../utils';
1314
//@ts-ignore
1415
import Assets from '../../assets';
1516
import {asBaseComponent} from '../../commons/new';
@@ -251,9 +252,7 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
251252
};
252253
};
253254

254-
getAccessibleHitSlop(size: number) {
255-
return Math.max(0, (48 - size) / 2);
256-
}
255+
257256

258257
renderCheckbox() {
259258
const {selectedIcon, label, testID, style, containerStyle, indeterminate, ...others} = this.props;
@@ -267,7 +266,7 @@ class Checkbox extends Component<CheckboxProps, CheckboxState> {
267266
{...others}
268267
style={[this.getBorderStyle(), style, !label && containerStyle]}
269268
onPress={this.onPress}
270-
hitSlop={this.getAccessibleHitSlop(this.props.size || DEFAULT_SIZE)}
269+
hitSlop={StyleUtils.getAccessibleHitSlop(this.props.size || DEFAULT_SIZE)}
271270
>
272271
{
273272
<Animated.View

src/components/pageControl/index.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, {PureComponent} from 'react';
33
import {StyleSheet, LayoutAnimation, StyleProp, ViewStyle} from 'react-native';
44
import {asBaseComponent} from '../../commons/new';
55
import {Colors} from '../../style';
6+
import {StyleUtils} from '../../utils';
67
import TouchableOpacity, {TouchableOpacityProps} from '../touchableOpacity';
78
import View from '../view';
89

@@ -114,14 +115,12 @@ class PageControl extends PureComponent<PageControlProps, State> {
114115

115116
if (Array.isArray(props.size)) {
116117
if (props.size[0] >= props.size[1] || props.size[1] >= props.size[2]) {
117-
console.warn(
118-
'It is recommended that largeSize > mediumSize > smallSize, currently: smallSize=',
118+
console.warn('It is recommended that largeSize > mediumSize > smallSize, currently: smallSize=',
119119
props.size[0],
120120
'mediumSize=',
121121
props.size[1],
122122
'largeSize=',
123-
props.size[2]
124-
);
123+
props.size[2]);
125124
}
126125
}
127126
}
@@ -190,6 +189,8 @@ class PageControl extends PureComponent<PageControlProps, State> {
190189

191190
renderIndicator(index: number, size: number, enlargeActive?: boolean) {
192191
const {currentPage, color, inactiveColor, onPagePress, spacing = PageControl.DEFAULT_SPACING} = this.props;
192+
const hitSlopValue = StyleUtils.getAccessibleHitSlop(size);
193+
193194
return (
194195
<TouchableOpacity
195196
customValue={index}
@@ -201,6 +202,10 @@ class PageControl extends PureComponent<PageControlProps, State> {
201202
getColorStyle(index === currentPage, color, inactiveColor),
202203
getSizeStyle(size, index, currentPage, enlargeActive)
203204
]}
205+
hitSlop={{
206+
top: hitSlopValue,
207+
bottom: hitSlopValue
208+
}}
204209
/>
205210
);
206211
}

src/components/radioButton/index.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ViewStyle
1111
} from 'react-native';
1212
import {Colors} from '../../style';
13+
import {StyleUtils} from '../../utils';
1314
import {asBaseComponent, forwardRef, BaseComponentInjectedProps, ForwardRefInjectedProps} from '../../commons/new';
1415
import TouchableOpacity from '../touchableOpacity';
1516
import View, {ViewProps} from '../view';
@@ -257,18 +258,12 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
257258
);
258259
}
259260

260-
getAccessibleHitSlop(size: number) {
261-
const verticalPadding = Math.max(0, (48 - size) / 2);
262-
263-
return {
264-
top: verticalPadding,
265-
bottom: verticalPadding
266-
};
267-
}
261+
268262

269263
render() {
270264
const {onPress, onValueChange, containerStyle, contentOnLeft, ...others} = this.props;
271265
const Container = onPress || onValueChange ? TouchableOpacity : View;
266+
const hitSlopValue = StyleUtils.getAccessibleHitSlop(this.props.size || DEFAULT_SIZE);
272267

273268
return (
274269
// @ts-ignore
@@ -280,7 +275,10 @@ class RadioButton extends PureComponent<Props, RadioButtonState> {
280275
style={containerStyle}
281276
onPress={this.onPress}
282277
{...this.getAccessibilityProps()}
283-
hitSlop={this.getAccessibleHitSlop(this.props.size || DEFAULT_SIZE)}
278+
hitSlop={{
279+
top: hitSlopValue,
280+
bottom: hitSlopValue
281+
}}
284282
>
285283
{!contentOnLeft && this.renderButton()}
286284
{this.props.iconOnRight ? this.renderLabel() : this.renderIcon()}

src/utils/styleUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ export function unpackStyle(style?: StyleProp<ViewStyle>, options: UnpackStyleOp
99
return JSON.parse(JSON.stringify(options.flatten ? StyleSheet.flatten(style) : style));
1010
}
1111
}
12+
13+
export function getAccessibleHitSlop(size: number) {
14+
return Math.max(0, (48 - size) / 2);
15+
}

0 commit comments

Comments
 (0)