Skip to content

Commit 564a842

Browse files
fix: deliver raw touch events past a declining touch responder (#1958)
1 parent 0ecb9c9 commit 564a842

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ with v14.
2121
- `toBeDisabled()` and `*ByRole` queries prefer `disabled` over `aria-disabled` and
2222
`accessibilityState.disabled` on `Text`, as React Native does. A `Text` with
2323
`disabled={false}` and `aria-disabled` is enabled.
24+
- `fireEvent` delivers raw touch events (`touchStart`, `touchMove`, `touchEnd`, `touchCancel`)
25+
to a handler even when the nearest touch responder declines the touch, as a device does. Before,
26+
a child with `onStartShouldSetResponder` returning `false` stopped these events from reaching
27+
the `onTouchStart` of its ancestors. Responder-driven events like `press` still respect the
28+
responder.
2429

2530
## 14.1.0
2631

‎src/events/__tests__/fire-event.test.tsx‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,4 +1812,37 @@ describe('responder system', () => {
18121812
});
18131813
expect(onDrag).toHaveBeenCalled();
18141814
});
1815+
1816+
test.each([
1817+
['touchStart', 'onTouchStart'],
1818+
['touchMove', 'onTouchMove'],
1819+
['touchEnd', 'onTouchEnd'],
1820+
['touchCancel', 'onTouchCancel'],
1821+
])('fires %s on parent when child declines the responder', async (eventName, handlerName) => {
1822+
const onTouch = jest.fn();
1823+
await render(
1824+
<View {...{ [handlerName]: onTouch }}>
1825+
<View
1826+
testID="child"
1827+
onStartShouldSetResponder={() => false}
1828+
onMoveShouldSetResponder={() => false}
1829+
/>
1830+
</View>,
1831+
);
1832+
1833+
await fireEvent(screen.getByTestId('child'), eventName);
1834+
1835+
expect(onTouch).toHaveBeenCalledTimes(1);
1836+
});
1837+
1838+
test('fires touchStart on element that declines the responder', async () => {
1839+
const onTouchStart = jest.fn();
1840+
await render(
1841+
<View testID="view" onTouchStart={onTouchStart} onStartShouldSetResponder={() => false} />,
1842+
);
1843+
1844+
await fireEvent(screen.getByTestId('view'), 'touchStart');
1845+
1846+
expect(onTouchStart).toHaveBeenCalledTimes(1);
1847+
});
18151848
});

‎src/events/is-enabled.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ export function isEventBlockableByPointerEvents(eventType: string): boolean {
4444
*/
4545
const TEXT_INPUT_EVENTS_IGNORING_EDITABLE = ['contentSizeChange', 'layout', 'scroll'];
4646

47+
/**
48+
* List of raw touch events. A device delivers them to every ancestor of the touched view, whether
49+
* or not a touch responder accepts the touch.
50+
*/
51+
const TOUCH_EVENTS_IGNORING_RESPONDER = ['touchStart', 'touchMove', 'touchEnd', 'touchCancel'];
52+
4753
/**
4854
* Checks whether a device would deliver the event to the instance, taking into account
4955
* `pointerEvents`, non-editable `TextInput` and touch responders that decline the touch.
@@ -65,6 +71,10 @@ export function isEventEnabled(
6571
return false;
6672
}
6773

74+
if (TOUCH_EVENTS_IGNORING_RESPONDER.includes(eventType)) {
75+
return true;
76+
}
77+
6878
const touchStart = nearestTouchResponder?.props.onStartShouldSetResponder?.();
6979
const touchMove = nearestTouchResponder?.props.onMoveShouldSetResponder?.();
7080
if (touchStart || touchMove) {

0 commit comments

Comments
 (0)