Skip to content

Commit 84887cc

Browse files
committed
Added hitslop calculation for button
1 parent 3b8c072 commit 84887cc

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/components/button/ButtonConstants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ export const MIN_WIDTH = {
2222
LARGE: 90
2323
};
2424

25+
export const SIZE_TO_VERTICAL_HITSLOP = {
26+
[ButtonSize.xSmall]: 30,
27+
[ButtonSize.small]: 25,
28+
[ButtonSize.medium]: 20,
29+
[ButtonSize.large]: 15
30+
} as const;
31+
2532
export const DEFAULT_SIZE = ButtonSize.large;

src/components/button/index.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
DEFAULT_PROPS,
1717
ButtonSizeProp
1818
} from './types';
19-
import {PADDINGS, HORIZONTAL_PADDINGS, MIN_WIDTH, DEFAULT_SIZE} from './ButtonConstants';
19+
import {PADDINGS, HORIZONTAL_PADDINGS, MIN_WIDTH, DEFAULT_SIZE, SIZE_TO_VERTICAL_HITSLOP} from './ButtonConstants';
2020

2121
export {ButtonSize, ButtonAnimationDirection, ButtonProps};
2222

@@ -35,7 +35,7 @@ class Button extends PureComponent<Props, ButtonState> {
3535
super(props);
3636
}
3737

38-
state = {
38+
state: Record<'size', undefined | number> = {
3939
size: undefined
4040
};
4141
styles = createStyles();
@@ -152,7 +152,17 @@ class Button extends PureComponent<Props, ButtonState> {
152152
const {avoidMinWidth, avoidInnerPadding, round, size: propsSize} = this.props;
153153
const size = propsSize || DEFAULT_SIZE;
154154

155-
const CONTAINER_STYLE_BY_SIZE: Dictionary<any> = {};
155+
const CONTAINER_STYLE_BY_SIZE: Record<
156+
string,
157+
Partial<{
158+
height: number;
159+
width: number;
160+
padding: number;
161+
paddingVertical: number;
162+
paddingHorizontal: number;
163+
minWidth: number;
164+
}>
165+
> = {};
156166
CONTAINER_STYLE_BY_SIZE[Button.sizes.xSmall] = round
157167
? {height: this.state.size, width: this.state.size, padding: PADDINGS.XSMALL}
158168
: {
@@ -339,6 +349,21 @@ class Button extends PureComponent<Props, ButtonState> {
339349
return null;
340350
}
341351

352+
getAccessibleHitSlop() {
353+
const containerStyle = this.getContainerSizeStyle();
354+
const width = (containerStyle.width || containerStyle.minWidth || 0);
355+
const widthWithPadding = width + (containerStyle.paddingHorizontal || containerStyle.padding || 0) * 2;
356+
const horizontalHitslop = Math.max(0, (48 - widthWithPadding) / 2);
357+
const verticalHitslop = SIZE_TO_VERTICAL_HITSLOP[this.props.size || DEFAULT_SIZE] / 2;
358+
359+
return {
360+
top: verticalHitslop,
361+
bottom: verticalHitslop,
362+
left: horizontalHitslop,
363+
right: horizontalHitslop
364+
};
365+
}
366+
342367
render() {
343368
const {onPress, disabled, style, testID, animateLayout, modifiers, forwardedRef, ...others} = this.props;
344369
const shadowStyle = this.getShadowStyle();
@@ -373,6 +398,7 @@ class Button extends PureComponent<Props, ButtonState> {
373398
testID={testID}
374399
{...others}
375400
ref={forwardedRef}
401+
hitSlop={this.getAccessibleHitSlop()}
376402
>
377403
{this.props.children}
378404
{this.props.iconOnRight ? this.renderLabel() : this.renderIcon()}

0 commit comments

Comments
 (0)