From 05b6d6f07b31a785d18a8abfe3af152a76ab1253 Mon Sep 17 00:00:00 2001 From: Priyanshu0007 Date: Tue, 24 Nov 2026 23:35:11 +0530 Subject: [PATCH] fix(icon-button): size the hit slop from the laid-out size Closes #5310 --- src/components/IconButton/IconButton.tsx | 29 ++++++++- .../Appbar/__snapshots__/Appbar.test.tsx.snap | 8 +++ src/components/__tests__/IconButton.test.tsx | 62 +++++++++++++++++++ .../__tests__/TextInputAccessibility.test.tsx | 7 ++- .../__snapshots__/DataTable.test.tsx.snap | 12 ++++ .../__snapshots__/IconButton.test.tsx.snap | 10 +++ .../__snapshots__/Searchbar.test.tsx.snap | 7 +++ .../__snapshots__/TextInput.test.tsx.snap | 10 +++ 8 files changed, 142 insertions(+), 3 deletions(-) diff --git a/src/components/IconButton/IconButton.tsx b/src/components/IconButton/IconButton.tsx index a3553503c1..4e0420301d 100644 --- a/src/components/IconButton/IconButton.tsx +++ b/src/components/IconButton/IconButton.tsx @@ -5,6 +5,7 @@ import type { ColorValue, FocusEvent, GestureResponderEvent, + LayoutChangeEvent, StyleProp, ViewStyle, } from 'react-native'; @@ -20,6 +21,7 @@ import getMinInteractiveSizeHitSlop from '../../utils/getMinInteractiveSizeHitSl import hasTouchHandler, { carriesToggleState, } from '../../utils/hasTouchHandler'; +import { roundLayoutSize } from '../../utils/roundLayoutSize'; import { useFocusRing } from '../../utils/useFocusRing'; import { useShapeMorph } from '../../utils/useShapeMorph'; import ActivityIndicator from '../ActivityIndicator'; @@ -179,11 +181,19 @@ const IconButton = ({ onPressOut: onPressOutProp, onFocus: onFocusProp, onBlur: onBlurProp, + onLayout: onLayoutProp, ...rest }: Props) => { const theme = useInternalTheme(themeOverrides); const reduceMotion = useReduceMotion(); const [pressed, setPressed] = React.useState(false); + // Laid-out size of the touchable. `style` and `contentStyle` can resize the + // button away from its named `size`, so the hit slop is read from this once + // it is known. + const [touchableLayout, setTouchableLayout] = React.useState<{ + width: number; + height: number; + } | null>(null); const IconComponent = animated ? CrossFadeIcon : Icon; @@ -297,9 +307,23 @@ const IconButton = ({ ? { 'aria-pressed': selected } : { 'aria-checked': selected }; + const handleLayout = (e: LayoutChangeEvent) => { + onLayoutProp?.(e); + const width = roundLayoutSize(e.nativeEvent.layout.width); + const height = roundLayoutSize(e.nativeEvent.layout.height); + setTouchableLayout((current) => + current?.width === width && current.height === height + ? current + : { width, height } + ); + }; + // The touchable sits inside the outline, so it is smaller than the container. - const touchableWidth = dimensions.width - 2 * borderWidth; - const touchableHeight = dimensions.height - 2 * borderWidth; + // Until it is laid out, the size comes from the `size` tokens. + const touchableWidth = + touchableLayout?.width ?? dimensions.width - 2 * borderWidth; + const touchableHeight = + touchableLayout?.height ?? dimensions.height - 2 * borderWidth; // Disabled and handler-less buttons get no extra target. const defaultHitSlop = canBePressed @@ -369,6 +393,7 @@ const IconButton = ({ theme={theme} onFocus={handleFocus} onBlur={handleBlur} + onLayout={handleLayout} aria-label={ariaLabel} style={[ styles.touchable, diff --git a/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap b/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap index c13c51a6cd..1066a225e7 100644 --- a/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap +++ b/src/components/__tests__/Appbar/__snapshots__/Appbar.test.tsx.snap @@ -181,6 +181,7 @@ exports[`Appbar does not pass any additional props to Searchbar 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -364,6 +365,7 @@ exports[`Appbar does not pass any additional props to Searchbar 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -556,6 +558,7 @@ exports[`Appbar passes additional props to AppbarBackAction, AppbarContent and A onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -793,6 +796,7 @@ exports[`Appbar passes additional props to AppbarBackAction, AppbarContent and A onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -994,6 +998,7 @@ exports[`AppbarAction should be rendered with custom color 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1190,6 +1195,7 @@ exports[`AppbarAction should be rendered with default theme color 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1386,6 +1392,7 @@ exports[`AppbarAction should be rendered with specific theme color if is leading collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1583,6 +1590,7 @@ exports[`AppbarAction should render AppbarBackAction with custom color 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ diff --git a/src/components/__tests__/IconButton.test.tsx b/src/components/__tests__/IconButton.test.tsx index 574dbb8598..05551cdc4b 100644 --- a/src/components/__tests__/IconButton.test.tsx +++ b/src/components/__tests__/IconButton.test.tsx @@ -687,6 +687,68 @@ describe('hitSlop', () => { }); }); + it('follows the laid-out size when `style` shrinks the button', async () => { + await render( + {}} + style={{ width: 30, height: 30 }} + testID="icon-button" + /> + ); + + const touchable = screen.getByTestId('icon-button'); + await fireEvent(touchable, 'layout', { + nativeEvent: { layout: { x: 0, y: 0, width: 30, height: 30 } }, + }); + + expect(touchable).toHaveProp('hitSlop', { + top: 9, + bottom: 9, + left: 9, + right: 9, + }); + }); + + it('drops the default once the laid-out size reaches 48dp', async () => { + await render( + {}} + style={{ width: 56, height: 56 }} + testID="icon-button" + /> + ); + + const touchable = screen.getByTestId('icon-button'); + await fireEvent(touchable, 'layout', { + nativeEvent: { layout: { x: 0, y: 0, width: 56, height: 56 } }, + }); + + expect(touchable).not.toHaveProp('hitSlop'); + }); + + it('still calls a forwarded onLayout', async () => { + const onLayout = jest.fn(); + await render( + {}} + onLayout={onLayout} + testID="icon-button" + /> + ); + + const event = { + nativeEvent: { layout: { x: 0, y: 0, width: 40, height: 40 } }, + }; + await fireEvent(screen.getByTestId('icon-button'), 'layout', event); + + expect(onLayout).toHaveBeenCalledWith(event); + }); + it('takes `0` as an opt out', async () => { await render( includeHiddenElements: true, }); expect(icon).toHaveProp('nativeID', 'search-icon'); - expect(icon).toHaveProp('onLayout', onLayout); + + const event = { + nativeEvent: { layout: { x: 0, y: 0, width: 40, height: 40 } }, + }; + await fireEvent(icon, 'layout', event); + expect(onLayout).toHaveBeenCalledWith(event); }); diff --git a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap index 09c5c963f1..c01d483dc4 100644 --- a/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DataTable.test.tsx.snap @@ -613,6 +613,7 @@ exports[`DataTable.Pagination renders data table pagination 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -754,6 +755,7 @@ exports[`DataTable.Pagination renders data table pagination 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -956,6 +958,7 @@ exports[`DataTable.Pagination renders data table pagination with fast-forward bu onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -1097,6 +1100,7 @@ exports[`DataTable.Pagination renders data table pagination with fast-forward bu onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -1238,6 +1242,7 @@ exports[`DataTable.Pagination renders data table pagination with fast-forward bu onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -1379,6 +1384,7 @@ exports[`DataTable.Pagination renders data table pagination with fast-forward bu onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -1581,6 +1587,7 @@ exports[`DataTable.Pagination renders data table pagination with label 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -1722,6 +1729,7 @@ exports[`DataTable.Pagination renders data table pagination with label 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -2238,6 +2246,7 @@ exports[`DataTable.Pagination renders data table pagination with options select onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -2379,6 +2388,7 @@ exports[`DataTable.Pagination renders data table pagination with options select onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -2520,6 +2530,7 @@ exports[`DataTable.Pagination renders data table pagination with options select onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -2661,6 +2672,7 @@ exports[`DataTable.Pagination renders data table pagination with options select onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} diff --git a/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap b/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap index 0ff39c064b..755a442ba1 100644 --- a/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/IconButton.test.tsx.snap @@ -52,6 +52,7 @@ exports[`applies a static transform from style 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -190,6 +191,7 @@ exports[`renders a square icon button 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -326,6 +328,7 @@ exports[`renders disabled icon button 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -443,6 +446,7 @@ exports[`renders extra-large icon button 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -554,6 +558,7 @@ exports[`renders filled icon button 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -665,6 +670,7 @@ exports[`renders icon button by default 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -776,6 +782,7 @@ exports[`renders icon button with color 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -887,6 +894,7 @@ exports[`renders icon change animated 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1036,6 +1044,7 @@ exports[`renders outlined icon button 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1147,6 +1156,7 @@ exports[`renders tonal icon button 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ diff --git a/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap b/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap index a8d788e309..f48961ec34 100644 --- a/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/Searchbar.test.tsx.snap @@ -136,6 +136,7 @@ exports[`activity indicator snapshot test 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -566,6 +567,7 @@ exports[`renders searchbar in "view" mode 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -749,6 +751,7 @@ exports[`renders searchbar in "view" mode 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -983,6 +986,7 @@ exports[`renders with placeholder 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1166,6 +1170,7 @@ exports[`renders with placeholder 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -1375,6 +1380,7 @@ exports[`renders with text 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1554,6 +1560,7 @@ exports[`renders with text 1`] = ` onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} diff --git a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap index a815b58ef0..cf66d37137 100644 --- a/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/TextInput.test.tsx.snap @@ -230,6 +230,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -433,6 +434,7 @@ exports[`dims a disabled TextInput with TextInput.Icon accessories exactly once onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -718,6 +720,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -894,6 +897,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1173,6 +1177,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -1376,6 +1381,7 @@ exports[`renders filled TextInput with TextInput.Icon accessories when error is onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]} @@ -1832,6 +1838,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -2008,6 +2015,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories 1`] = ` collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -2267,6 +2275,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i collapsable={false} onBlur={[Function]} onFocus={[Function]} + onLayout={[Function]} role="none" style={ [ @@ -2470,6 +2479,7 @@ exports[`renders outlined TextInput with TextInput.Icon accessories when error i onBlur={[Function]} onClick={[Function]} onFocus={[Function]} + onLayout={[Function]} onResponderGrant={[Function]} onResponderMove={[Function]} onResponderRelease={[Function]}