Skip to content

Commit 72325c2

Browse files
authored
Chip - Increased hit slop for accessibility (#3746)
1 parent 27d282f commit 72325c2

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

src/components/chip/index.tsx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import _ from 'lodash';
2-
import React, {useCallback} from 'react';
1+
import React, {useCallback, useMemo} from 'react';
32
import {StyleSheet, StyleProp, ViewStyle, ViewProps, ImageStyle, TextStyle, ImageSourcePropType} from 'react-native';
43
import Assets from '../../assets';
54
import {asBaseComponent} from '../../commons/new';
@@ -198,7 +197,7 @@ const Chip = ({
198197
<TouchableOpacity
199198
style={[getMargins('dismiss'), dismissContainerStyle]}
200199
onPress={onDismiss}
201-
hitSlop={{top: 10, bottom: 10, left: 10, right: 10}}
200+
hitSlop={{top: 16, bottom: 16, left: 10, right: 10}}
202201
testID={`${testID}.dismiss`}
203202
>
204203
<Icon
@@ -302,23 +301,37 @@ const Chip = ({
302301
},
303302
[avatarProps, badgeProps, iconSource, rightIconSource, onDismiss]);
304303

305-
const getContainerSize = useCallback(() => {
306-
const width = useSizeAsMinimum ? 'minWidth' : 'width';
307-
const height = useSizeAsMinimum ? 'minHeight' : 'height';
308-
309-
return typeof size === 'object'
310-
? {[width]: _.get(size, 'width'), [height]: _.get(size, 'height')}
311-
: {[width]: size, [height]: size};
304+
const chipSize = useMemo(() => {
305+
const width = typeof size === 'object' ? size.width : size;
306+
const height = typeof size === 'object' ? size.height : size;
307+
return {width, height};
312308
}, [size]);
313309

310+
const containerSizeStyle = useMemo(() => {
311+
const {width, height} = chipSize;
312+
return useSizeAsMinimum ? {minWidth: width, minHeight: height} : {width, height};
313+
}, [chipSize]);
314+
314315
const Container = onPress ? TouchableOpacity : View;
316+
const hitSlop = useMemo(() => {
317+
const {width = 48, height = 48} = chipSize;
318+
const verticalHitSlop = Math.max(0, (48 - height) / 2);
319+
const horizontalHitSlop = Math.max(0, (48 - width) / 2);
320+
return {
321+
top: verticalHitSlop,
322+
bottom: verticalHitSlop,
323+
left: horizontalHitSlop,
324+
right: horizontalHitSlop
325+
};
326+
}, [chipSize]);
315327

316328
return (
317329
<Container
318330
activeOpacity={1}
319331
onPress={onPress}
320-
style={[styles.container, {backgroundColor}, {borderRadius}, containerStyle, getContainerSize()]}
332+
style={[styles.container, {backgroundColor}, {borderRadius}, containerStyle, containerSizeStyle]}
321333
testID={testID}
334+
hitSlop={hitSlop}
322335
{...others}
323336
>
324337
{avatarProps && renderAvatar()}

0 commit comments

Comments
 (0)