Skip to content

Commit 016510f

Browse files
committed
fix: don't run gesture recognizers after dispose()
1 parent df04ef3 commit 016510f

4 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎AGENTS.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ no `userEvent.setup()` either, since there's no state or option to keep.
187187
- `tap` runs the recognizers a touch
188188
would reach (`dispatch-gesture.ts`, with hit testing in `hit-test.ts`): from the `gestureRecognizers` prop, or attached
189189
with `setGestureRecognizers()` through a ref (`helpers/native-view.ts`
190-
records them). The deepest accepting recognizer wins. Disabled ones and ones
190+
records them). The deepest accepting recognizer wins. Disposed ones count as
191+
detached, as on a device (`dispose()` detaches them). Disabled ones and ones
191192
whose `gestureRecognizerShouldBegin` returns `false` are skipped, and
192193
`isUserInteractionEnabled={false}` sends the touch to the view below. It
193194
throws when no recognizer for the gesture exists at all, since that's almost

‎src/__tests__/user-event-gestures.test.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ describe('tap', () => {
9595
await userEvent.tap(screen.getByText('Disabled'))
9696
})
9797

98+
test('skips disposed recognizers, which are detached from their view', async () => {
99+
const log: string[] = []
100+
const disposed = createTap(() => log.push('disposed'))
101+
disposed.dispose()
102+
await render(
103+
<View gestureRecognizers={[createTap(() => log.push('outer'))]}>
104+
<Text gestureRecognizers={[disposed]}>Tap me</Text>
105+
</View>,
106+
)
107+
108+
await userEvent.tap(screen.getByText('Tap me'))
109+
expect(log).toEqual(['outer'])
110+
})
111+
112+
test('throws when the only recognizer is disposed', async () => {
113+
const tap = createTap(() => {})
114+
tap.dispose()
115+
await render(<Text gestureRecognizers={[tap]}>Disposed</Text>)
116+
117+
await expect(userEvent.tap(screen.getByText('Disposed'))).rejects.toThrow(
118+
'tap() found no TapGestureRecognizer it could trigger on the "Text" element or its ' +
119+
'ancestors. Some were disposed, which detaches them.',
120+
)
121+
})
122+
98123
test('isUserInteractionEnabled={false} blocks the view and its subtree', async () => {
99124
const log: string[] = []
100125
await render(

‎src/helpers/gestures.ts‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,19 @@ class GestureRecognizer {
2121
isEnabled = true
2222
numberOfTouches = 0
2323
state: number = GestureState.Possible
24+
#disposed = false
2425

25-
dispose() {}
26+
/**
27+
* On a device, `dispose()` detaches the recognizer from its view for good.
28+
* Not part of NitroUI's API: lets hit testing treat it as detached.
29+
*/
30+
get isDisposed(): boolean {
31+
return this.#disposed
32+
}
33+
34+
dispose() {
35+
this.#disposed = true
36+
}
2637
}
2738

2839
class TapGestureRecognizer extends GestureRecognizer {

‎src/user-event/dispatch-gesture.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ type Recognizer = InstanceType<typeof FakeGestureRecognizer> & {
3232
* `isUserInteractionEnabled={false}`: then it lands on the parent of the
3333
* highest such view, so only recognizers above it can see it.
3434
* - Recognizers that `accepts` the gesture are tried from the deepest view
35-
* up. Disabled ones are skipped, and so are ones whose
35+
* up. Disposed ones are detached on a device, so they're ignored. Disabled
36+
* ones are skipped, and so are ones whose
3637
* `gestureRecognizerShouldBegin` returns `false`. The first one left
3738
* runs.
3839
*
@@ -50,9 +51,14 @@ export async function dispatchGesture<R extends Recognizer>(
5051
const reachable = chain.slice(blockedAt + 1)
5152

5253
let found = false
54+
let foundDisposed = false
5355
for (const host of reachable) {
5456
for (const recognizer of getGestureRecognizers(host)) {
5557
if (!accepts(recognizer)) continue
58+
if (recognizer.isDisposed) {
59+
foundDisposed = true
60+
continue
61+
}
5662
found = true
5763
if (!recognizer.isEnabled) continue
5864

@@ -73,7 +79,8 @@ export async function dispatchGesture<R extends Recognizer>(
7379
if (!found && blockedAt === -1) {
7480
throw new Error(
7581
`${caller} found no ${recognizerName} it could trigger on the "${element.type}" ` +
76-
'element or its ancestors.',
82+
'element or its ancestors.' +
83+
(foundDisposed ? ' Some were disposed, which detaches them.' : ''),
7784
)
7885
}
7986
}

0 commit comments

Comments
 (0)