-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add debounce functionality to TouchableRipple #4828
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -46,6 +46,11 @@ export type Props = PressableProps & { | |||||
| * Function to execute on long press. | ||||||
| */ | ||||||
| onLongPress?: (e: GestureResponderEvent) => void; | ||||||
| /** | ||||||
| * Debounce time in milliseconds to prevent rapid successive presses. | ||||||
| * When set, subsequent onPress calls within this time window will be ignored. | ||||||
| */ | ||||||
| debounce?: number; | ||||||
| /** | ||||||
| * Function to execute immediately when a touch is engaged, before `onPressOut` and `onPress`. | ||||||
| */ | ||||||
|
|
@@ -93,6 +98,7 @@ export type Props = PressableProps & { | |||||
| * <TouchableRipple | ||||||
| * onPress={() => console.log('Pressed')} | ||||||
| * rippleColor="rgba(0, 0, 0, .32)" | ||||||
| * debounce={300} // Prevent double-clicks within 300ms | ||||||
| * > | ||||||
| * <Text>Press anywhere</Text> | ||||||
| * </TouchableRipple> | ||||||
|
|
@@ -113,6 +119,7 @@ const TouchableRipple = ( | |||||
| underlayColor: _underlayColor, | ||||||
| children, | ||||||
| theme: themeOverrides, | ||||||
| debounce, | ||||||
| ...rest | ||||||
| }: Props, | ||||||
| ref: React.ForwardedRef<View> | ||||||
|
|
@@ -126,6 +133,24 @@ const TouchableRipple = ( | |||||
| const { rippleEffectEnabled } = React.useContext<Settings>(SettingsContext); | ||||||
|
|
||||||
| const { onPress, onLongPress, onPressIn, onPressOut } = rest; | ||||||
| const lastPressTime = React.useRef<number>(0); | ||||||
|
|
||||||
| const debouncedOnPress = React.useCallback( | ||||||
| (e: GestureResponderEvent) => { | ||||||
| if (!onPress) return; | ||||||
|
|
||||||
| if (debounce && debounce > 0) { | ||||||
| const now = Date.now(); | ||||||
| if (now - lastPressTime.current < debounce) { | ||||||
| return; // Ignore this press as it's within the debounce window | ||||||
| } | ||||||
| lastPressTime.current = now; | ||||||
| } | ||||||
|
|
||||||
| onPress(e); | ||||||
| }, | ||||||
| [onPress, debounce] | ||||||
| ); | ||||||
|
|
||||||
| const handlePressIn = React.useCallback( | ||||||
| (e: any) => { | ||||||
|
|
@@ -273,10 +298,11 @@ const TouchableRipple = ( | |||||
| <Pressable | ||||||
| {...rest} | ||||||
| ref={ref} | ||||||
| onPress={debouncedOnPress} | ||||||
| onPressIn={handlePressIn} | ||||||
| onPressOut={handlePressOut} | ||||||
| disabled={disabled} | ||||||
| style={(state) => [ | ||||||
| style={(state: PressableStateCallbackType) => [ | ||||||
|
||||||
| styles.touchable, | ||||||
| borderless && styles.borderless, | ||||||
| // focused state is not ready yet: https://github.com/necolas/react-native-web/issues/1849 | ||||||
|
|
@@ -286,7 +312,7 @@ const TouchableRipple = ( | |||||
| typeof style === 'function' ? style(state) : style, | ||||||
| ]} | ||||||
| > | ||||||
| {(state) => | ||||||
| {(state: PressableStateCallbackType) => | ||||||
|
||||||
| {(state: PressableStateCallbackType) => | |
| {state => |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,54 @@ describe('TouchableRipple', () => { | |
| expect(onPress).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('debounces onPress when debounce prop is provided', () => { | ||
| jest.useFakeTimers(); | ||
| const onPress = jest.fn(); | ||
| const { getByText } = render( | ||
| <TouchableRipple onPress={onPress} debounce={300}> | ||
| <Text>Button</Text> | ||
| </TouchableRipple> | ||
| ); | ||
|
|
||
| const button = getByText('Button'); | ||
|
|
||
| // Press multiple times rapidly | ||
| fireEvent.press(button); | ||
| fireEvent.press(button); | ||
| fireEvent.press(button); | ||
|
|
||
| // Should only be called once due to debouncing | ||
| expect(onPress).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Fast forward time past debounce window | ||
| jest.advanceTimersByTime(400); | ||
|
||
|
|
||
| // Now pressing should work again | ||
| fireEvent.press(button); | ||
| expect(onPress).toHaveBeenCalledTimes(2); | ||
|
|
||
| jest.useRealTimers(); | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Unmocked Time Improves Debounce Testing ReliabilityThe debounce test uses |
||
|
|
||
| it('does not debounce when debounce is not provided', () => { | ||
| const onPress = jest.fn(); | ||
| const { getByText } = render( | ||
| <TouchableRipple onPress={onPress}> | ||
| <Text>Button</Text> | ||
| </TouchableRipple> | ||
| ); | ||
|
|
||
| const button = getByText('Button'); | ||
|
|
||
| // Press multiple times rapidly | ||
| fireEvent.press(button); | ||
| fireEvent.press(button); | ||
| fireEvent.press(button); | ||
|
|
||
| // Should be called for each press | ||
| expect(onPress).toHaveBeenCalledTimes(3); | ||
| }); | ||
|
|
||
| it('disables the button when disabled prop is true', () => { | ||
| const onPress = jest.fn(); | ||
| const { getByText } = render( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Adding inline type annotation for a destructured parameter is unnecessarily verbose. TypeScript can infer this type from the Pressable component's children render prop. Consider removing the type annotation unless there's a specific type inference issue.