From 86ffb9c41e033f59599e01b7ad016706b5f62fc8 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Thu, 10 Sep 2020 17:01:55 -0700 Subject: [PATCH] Pressability: Remove Default Press Delay Summary: Removes the default press delay from `Pressability`, which was introduced in 0.63 and affected `Pressable`. Fixes #29376. In a subsequent commit, I will bring it back as an `unstable_pressDelay` prop. Changelog: [General][Changed] - Removed default 130ms delay from Pressability and Pressable. Reviewed By: lunaleaps Differential Revision: D23604582 fbshipit-source-id: c21c72bf8b59fed028f5905ca4f805bb3fa79399 --- Libraries/Pressability/Pressability.js | 12 ++--- .../__tests__/Pressability-test.js | 54 +++++++++---------- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/Libraries/Pressability/Pressability.js b/Libraries/Pressability/Pressability.js index 31d32166804bed..839948d3b25c62 100644 --- a/Libraries/Pressability/Pressability.js +++ b/Libraries/Pressability/Pressability.js @@ -276,8 +276,7 @@ const isPressInSignal = signal => const isTerminalSignal = signal => signal === 'RESPONDER_TERMINATED' || signal === 'RESPONDER_RELEASE'; -const DEFAULT_LONG_PRESS_DELAY_MS = 370; // 500 - 130 -const DEFAULT_PRESS_DELAY_MS = 130; +const DEFAULT_LONG_PRESS_DELAY_MS = 500; const DEFAULT_PRESS_RECT_OFFSETS = { bottom: 30, left: 20, @@ -472,12 +471,7 @@ export default class Pressability { this._touchState = 'NOT_RESPONDER'; this._receiveSignal('RESPONDER_GRANT', event); - const delayPressIn = normalizeDelay( - this._config.delayPressIn, - 0, - DEFAULT_PRESS_DELAY_MS, - ); - + const delayPressIn = normalizeDelay(this._config.delayPressIn); if (delayPressIn > 0) { this._pressDelayTimeout = setTimeout(() => { this._receiveSignal('DELAY', event); @@ -489,7 +483,7 @@ export default class Pressability { const delayLongPress = normalizeDelay( this._config.delayLongPress, 10, - DEFAULT_LONG_PRESS_DELAY_MS, + DEFAULT_LONG_PRESS_DELAY_MS - delayPressIn, ); this._longPressDelayTimeout = setTimeout(() => { this._handleLongPress(event); diff --git a/Libraries/Pressability/__tests__/Pressability-test.js b/Libraries/Pressability/__tests__/Pressability-test.js index 52c35d00c1faae..d584d215b7cc3e 100644 --- a/Libraries/Pressability/__tests__/Pressability-test.js +++ b/Libraries/Pressability/__tests__/Pressability-test.js @@ -355,7 +355,7 @@ describe('Pressability', () => { expect(config.onLongPress).toBeCalled(); }); - it('is called if pressed for 370ms after the press delay', () => { + it('is called if pressed for 500ms after press started', () => { const {config, handlers} = createMockPressability({ delayPressIn: 100, }); @@ -364,7 +364,7 @@ describe('Pressability', () => { handlers.onResponderGrant(createMockPressEvent('onResponderGrant')); handlers.onResponderMove(createMockPressEvent('onResponderMove')); - jest.advanceTimersByTime(469); + jest.advanceTimersByTime(499); expect(config.onLongPress).not.toBeCalled(); jest.advanceTimersByTime(1); expect(config.onLongPress).toBeCalled(); @@ -393,7 +393,7 @@ describe('Pressability', () => { handlers.onResponderGrant(createMockPressEvent('onResponderGrant')); handlers.onResponderMove(createMockPressEvent('onResponderMove')); - jest.advanceTimersByTime(139); + jest.advanceTimersByTime(9); expect(config.onLongPress).not.toBeCalled(); jest.advanceTimersByTime(1); expect(config.onLongPress).toBeCalled(); @@ -460,7 +460,13 @@ describe('Pressability', () => { const {config, handlers} = createMockPressability(); handlers.onStartShouldSetResponder(); - handlers.onResponderGrant(createMockPressEvent('onResponderGrant')); + handlers.onResponderGrant( + createMockPressEvent({ + registrationName: 'onResponderGrant', + pageX: 0, + pageY: 0, + }), + ); handlers.onResponderMove( createMockPressEvent({ registrationName: 'onResponderMove', @@ -475,7 +481,13 @@ describe('Pressability', () => { // Subsequent long touch gesture should not carry over previous state. handlers.onStartShouldSetResponder(); - handlers.onResponderGrant(createMockPressEvent('onResponderGrant')); + handlers.onResponderGrant( + createMockPressEvent({ + registrationName: 'onResponderGrant', + pageX: 7, + pageY: 8, + }), + ); handlers.onResponderMove( // NOTE: Delta from (0, 0) is ~10.6 > 10, but should not matter. createMockPressEvent({ @@ -522,7 +534,7 @@ describe('Pressability', () => { expect(config.onPressIn).toBeCalled(); }); - it('is called after the default delay by default', () => { + it('is called immediately by default', () => { const {config, handlers} = createMockPressability({ delayPressIn: null, }); @@ -531,24 +543,6 @@ describe('Pressability', () => { handlers.onResponderGrant(createMockPressEvent('onResponderGrant')); handlers.onResponderMove(createMockPressEvent('onResponderMove')); - jest.advanceTimersByTime(129); - expect(config.onPressIn).not.toBeCalled(); - jest.advanceTimersByTime(1); - expect(config.onPressIn).toBeCalled(); - }); - - it('falls back to the default delay if `delayPressIn` is omitted', () => { - const {config, handlers} = createMockPressability({ - delayPressIn: null, - }); - - handlers.onStartShouldSetResponder(); - handlers.onResponderGrant(createMockPressEvent('onResponderGrant')); - handlers.onResponderMove(createMockPressEvent('onResponderMove')); - - jest.advanceTimersByTime(129); - expect(config.onPressIn).not.toBeCalled(); - jest.advanceTimersByTime(1); expect(config.onPressIn).toBeCalled(); }); @@ -582,7 +576,9 @@ describe('Pressability', () => { describe('onPressOut', () => { it('is called after `onResponderRelease` before `delayPressIn`', () => { - const {config, handlers} = createMockPressability(); + const {config, handlers} = createMockPressability({ + delayPressIn: Number.EPSILON, + }); handlers.onStartShouldSetResponder(); handlers.onResponderGrant(createMockPressEvent('onResponderGrant')); @@ -596,7 +592,9 @@ describe('Pressability', () => { }); it('is called after `onResponderRelease` after `delayPressIn`', () => { - const {config, handlers} = createMockPressability(); + const {config, handlers} = createMockPressability({ + delayPressIn: Number.EPSILON, + }); handlers.onStartShouldSetResponder(); handlers.onResponderGrant(createMockPressEvent('onResponderGrant')); @@ -611,7 +609,9 @@ describe('Pressability', () => { }); it('is not called after `onResponderTerminate` before `delayPressIn`', () => { - const {config, handlers} = createMockPressability(); + const {config, handlers} = createMockPressability({ + delayPressIn: Number.EPSILON, + }); handlers.onStartShouldSetResponder(); handlers.onResponderGrant(createMockPressEvent('onResponderGrant'));