Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/components/IconButton/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ColorValue,
FocusEvent,
GestureResponderEvent,
LayoutChangeEvent,
StyleProp,
ViewStyle,
} from 'react-native';
Expand All @@ -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';
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -369,6 +393,7 @@ const IconButton = ({
theme={theme}
onFocus={handleFocus}
onBlur={handleBlur}
onLayout={handleLayout}
aria-label={ariaLabel}
style={[
styles.touchable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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={
[
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -994,6 +998,7 @@ exports[`AppbarAction should be rendered with custom color 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down Expand Up @@ -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={
[
Expand Down Expand Up @@ -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={
[
Expand Down Expand Up @@ -1583,6 +1590,7 @@ exports[`AppbarAction should render AppbarBackAction with custom color 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down
62 changes: 62 additions & 0 deletions src/components/__tests__/IconButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,68 @@ describe('hitSlop', () => {
});
});

it('follows the laid-out size when `style` shrinks the button', async () => {
await render(
<IconButton
icon="menu-down"
iconSize={30}
aria-label="Expand title"
onPress={() => {}}
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(
<IconButton
icon="camera"
onPress={() => {}}
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(
<IconButton
icon="camera"
onPress={() => {}}
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(
<IconButton
Expand Down
7 changes: 6 additions & 1 deletion src/components/__tests__/TextInputAccessibility.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -928,5 +928,10 @@ it('forwards decorative accessory layout callbacks and native IDs', async () =>
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);
});
12 changes: 12 additions & 0 deletions src/components/__tests__/__snapshots__/DataTable.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -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]}
Expand Down
10 changes: 10 additions & 0 deletions src/components/__tests__/__snapshots__/IconButton.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ exports[`applies a static transform from style 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down Expand Up @@ -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]}
Expand Down Expand Up @@ -326,6 +328,7 @@ exports[`renders disabled icon button 1`] = `
onBlur={[Function]}
onClick={[Function]}
onFocus={[Function]}
onLayout={[Function]}
onResponderGrant={[Function]}
onResponderMove={[Function]}
onResponderRelease={[Function]}
Expand Down Expand Up @@ -443,6 +446,7 @@ exports[`renders extra-large icon button 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down Expand Up @@ -554,6 +558,7 @@ exports[`renders filled icon button 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down Expand Up @@ -665,6 +670,7 @@ exports[`renders icon button by default 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down Expand Up @@ -776,6 +782,7 @@ exports[`renders icon button with color 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down Expand Up @@ -887,6 +894,7 @@ exports[`renders icon change animated 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down Expand Up @@ -1036,6 +1044,7 @@ exports[`renders outlined icon button 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down Expand Up @@ -1147,6 +1156,7 @@ exports[`renders tonal icon button 1`] = `
collapsable={false}
onBlur={[Function]}
onFocus={[Function]}
onLayout={[Function]}
role="none"
style={
[
Expand Down
Loading
Loading