-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Move hitSlop normalization to JS #4388
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
+603
−199
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8091d71
init
coado ab91044
make normalizeHitSlop idempotent
coado 17eb025
reject negative sizes
coado 22b37f3
test on native side wiring
coado c19ebcd
log error in worklet
coado 881869a
fixes
coado a4efeb2
add fallback for updateGestureHandlerConfig
coado 101980a
Merge branch 'main' into @coado/hitslop
coado f7584bc
fix ciągła integracja
coado c4224ef
fix null hitSlop hanlding and optimize uniform hitSlop values transfer
coado File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
packages/react-native-gesture-handler/src/__tests__/hitSlop.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| import type { HitSlop } from '../handlers/gestureHandlerCommon'; | ||
| import { normalizeHitSlop } from '../handlers/hitSlop'; | ||
|
|
||
| describe('normalizeHitSlop', () => { | ||
| test('keeps `undefined` out of the payload and sends `null` as unset slots', () => { | ||
| // `undefined` keeps the property out of partial config updates. `null` has to | ||
| // travel as six unset slots, since the iOS bridge drops null-valued keys. | ||
| expect(normalizeHitSlop(undefined)).toBeUndefined(); | ||
| expect(normalizeHitSlop(null)).toEqual([ | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| ]); | ||
| }); | ||
|
|
||
| test('is idempotent', () => { | ||
| // Normalizing an already normalized value must not empty it out. | ||
| const normalized = normalizeHitSlop({ horizontal: -10, top: -5 }); | ||
|
|
||
| expect(normalizeHitSlop(normalized)).toEqual([ | ||
| -10, | ||
| -5, | ||
| -10, | ||
| null, | ||
| null, | ||
| null, | ||
| ]); | ||
| expect(normalizeHitSlop(normalizeHitSlop(-10))).toBe(-10); | ||
| }); | ||
|
|
||
| test('forwards a uniform hit slop as a plain number', () => { | ||
| // Left as a number so the bridge does not allocate an array wrapper for it; | ||
| // every platform expands it into four equal edges itself. | ||
| expect(normalizeHitSlop(-10)).toBe(-10); | ||
| expect(normalizeHitSlop(0)).toBe(0); | ||
| }); | ||
|
|
||
| test('marks unspecified edges as `null`', () => { | ||
| expect(normalizeHitSlop({})).toEqual([null, null, null, null, null, null]); | ||
| expect(normalizeHitSlop({ left: -10 })).toEqual([ | ||
| -10, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| ]); | ||
| }); | ||
|
|
||
| test('treats an explicitly `undefined` edge as unspecified', () => { | ||
| // Without this, the value would reach the platforms as `0` and shrink the view. | ||
| expect(normalizeHitSlop({ left: undefined, top: -5 })).toEqual([ | ||
| null, | ||
| -5, | ||
| null, | ||
| null, | ||
| null, | ||
| null, | ||
| ]); | ||
| }); | ||
|
|
||
| test('expands `horizontal` and `vertical`', () => { | ||
| expect(normalizeHitSlop({ horizontal: -10 })).toEqual([ | ||
| -10, | ||
| null, | ||
| -10, | ||
| null, | ||
| null, | ||
| null, | ||
| ]); | ||
| expect(normalizeHitSlop({ vertical: -10 })).toEqual([ | ||
| null, | ||
| -10, | ||
| null, | ||
| -10, | ||
| null, | ||
| null, | ||
| ]); | ||
| expect(normalizeHitSlop({ horizontal: -10, vertical: -5 })).toEqual([ | ||
| -10, | ||
| -5, | ||
| -10, | ||
| -5, | ||
| null, | ||
| null, | ||
| ]); | ||
| }); | ||
|
|
||
| test('lets an explicit edge win over the shorthand', () => { | ||
| expect(normalizeHitSlop({ horizontal: -10, left: -20 })).toEqual([ | ||
| -20, | ||
| null, | ||
| -10, | ||
| null, | ||
| null, | ||
| null, | ||
| ]); | ||
| expect(normalizeHitSlop({ vertical: -10, bottom: -20 })).toEqual([ | ||
| null, | ||
| -10, | ||
| null, | ||
| -20, | ||
| null, | ||
| null, | ||
| ]); | ||
| }); | ||
|
|
||
| test('carries `width` and `height` through', () => { | ||
| expect(normalizeHitSlop({ left: 0, width: 20 })).toEqual([ | ||
| 0, | ||
| null, | ||
| null, | ||
| null, | ||
| 20, | ||
| null, | ||
| ]); | ||
| expect(normalizeHitSlop({ bottom: 0, height: 20 })).toEqual([ | ||
| null, | ||
| null, | ||
| null, | ||
| 0, | ||
| null, | ||
| 20, | ||
| ]); | ||
| }); | ||
|
|
||
| test('rejects invalid `width` and `height` combinations', () => { | ||
| expect(() => | ||
| normalizeHitSlop({ left: 0, right: 0, width: 20 } as HitSlop) | ||
| ).toThrow("cannot have all of 'left', 'right' and 'width' defined"); | ||
|
|
||
| expect(() => normalizeHitSlop({ width: 20 } as HitSlop)).toThrow( | ||
| "when 'width' is defined, either 'left' or 'right' has to be defined" | ||
| ); | ||
|
|
||
| expect(() => | ||
| normalizeHitSlop({ top: 0, bottom: 0, height: 20 } as HitSlop) | ||
| ).toThrow("cannot have all of 'top', 'bottom' and 'height' defined"); | ||
|
|
||
| expect(() => normalizeHitSlop({ height: 20 } as HitSlop)).toThrow( | ||
| "when 'height' is defined, either 'top' or 'bottom' has to be defined" | ||
| ); | ||
| }); | ||
|
|
||
| test('rejects a negative `width` or `height`', () => { | ||
| expect(() => normalizeHitSlop({ left: 0, width: -20 } as HitSlop)).toThrow( | ||
| "'width' cannot be negative" | ||
| ); | ||
|
|
||
| expect(() => normalizeHitSlop({ top: 0, height: -20 } as HitSlop)).toThrow( | ||
| "'height' cannot be negative" | ||
| ); | ||
| }); | ||
|
|
||
| test('allows a zero `width` or `height`', () => { | ||
| // An empty hit area is degenerate but coherent, and a hit slop animated | ||
| // from zero upwards passes through it. | ||
| expect(normalizeHitSlop({ left: 0, width: 0 })).toEqual([ | ||
| 0, | ||
| null, | ||
| null, | ||
| null, | ||
| 0, | ||
| null, | ||
| ]); | ||
| }); | ||
|
|
||
| test('counts a shorthand as defining both of its edges', () => { | ||
| // `horizontal` fills in both `left` and `right`, which conflicts with `width`. | ||
| expect(() => | ||
| normalizeHitSlop({ horizontal: -10, width: 20 } as HitSlop) | ||
| ).toThrow("cannot have all of 'left', 'right' and 'width' defined"); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.