Skip to content

Fix Pressable requiring dimensionsAfterResize #3606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 11, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import {
AccessibilityProps,
ColorValue,
LayoutChangeEvent,
StyleProp,
ViewStyle,
} from 'react-native';
Expand Down Expand Up @@ -56,6 +57,11 @@ export interface RawButtonProps
*/
style?: StyleProp<ViewStyle>;

/**
* Invoked on mount and layout changes.
*/
onLayout?: (event: LayoutChangeEvent) => void;

/**
* Used for testing-library compatibility, not passed to the native component.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
Expand All @@ -15,9 +14,9 @@ import {
} from './PressableProps';
import {
Insets,
LayoutChangeEvent,
Platform,
StyleProp,
View,
ViewStyle,
processColor,
} from 'react-native';
Expand Down Expand Up @@ -46,7 +45,6 @@ let IS_FABRIC: null | boolean = null;

const Pressable = (props: PressableProps) => {
const {
ref,
testOnly_pressed,
hitSlop,
pressRetentionOffset,
Expand All @@ -60,6 +58,7 @@ const Pressable = (props: PressableProps) => {
onPressIn,
onPressOut,
onLongPress,
onLayout,
style,
children,
android_disableSound,
Expand All @@ -69,7 +68,6 @@ const Pressable = (props: PressableProps) => {
simultaneousWithExternalGesture,
requireExternalGestureToFail,
blocksExternalGesture,
dimensionsAfterResize,
...remainingProps
} = props;

Expand All @@ -79,9 +77,6 @@ const Pressable = (props: PressableProps) => {
blocksExternalGesture,
};

// used only if `ref` is undefined
const fallbackRef = useRef<View>(null);

const [pressedState, setPressedState] = useState(testOnly_pressed ?? false);

const longPressTimeoutRef = useRef<number | null>(null);
Expand Down Expand Up @@ -109,21 +104,6 @@ const Pressable = (props: PressableProps) => {
normalizedPressRetentionOffset
);

useLayoutEffect(() => {
if (dimensionsAfterResize) {
dimensions.current = dimensionsAfterResize;
} else {
requestAnimationFrame(() => {
(ref ?? fallbackRef).current?.measure((_x, _y, width, height) => {
dimensions.current = {
width,
height,
};
});
});
}
}, [dimensionsAfterResize, ref]);

const cancelLongPress = useCallback(() => {
if (longPressTimeoutRef.current) {
clearTimeout(longPressTimeoutRef.current);
Expand Down Expand Up @@ -377,11 +357,19 @@ const Pressable = (props: PressableProps) => {
: processColor(unprocessedRippleColor);
}, [android_ripple]);

const setDimensions = useCallback(
(event: LayoutChangeEvent) => {
onLayout?.(event);
dimensions.current = event.nativeEvent.layout;
},
[onLayout]
);

return (
<GestureDetector gesture={gesture}>
<NativeButton
{...remainingProps}
ref={ref ?? fallbackRef}
onLayout={setDimensions}
accessible={accessible !== false}
hitSlop={appliedHitSlop}
enabled={isPressableEnabled}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface PressableProps
/**
* A reference to the pressable element.
*/
ref?: React.RefObject<View>;
ref?: React.Ref<View>;

/**
* Either children or a render prop that receives a boolean reflecting whether
Expand Down Expand Up @@ -168,9 +168,7 @@ export interface PressableProps
blocksExternalGesture?: RelationPropType;

/**
* Defines the dimensions of the Pressable after it's been resized.
* This property does not affect Pressable's physical appearance.
* Required when the Pressable is resized **and** uses pressRetentionOffset.
* @deprecated This property is no longer used, and will be removed in the future.
*/
dimensionsAfterResize?: PressableDimensions;
}
Loading