Skip to content

Move hitSlop normalization to JS - #4388

Open
coado wants to merge 9 commits into
mainfrom
@coado/hitslop
Open

Move hitSlop normalization to JS#4388
coado wants to merge 9 commits into
mainfrom
@coado/hitslop

Conversation

@coado

@coado coado commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Description

This moves the parsing and validation of hitSlop into one shared JS module and reduces the platforms to reading a fixed shape.

JS now sends a normalized [left, top, right, bottom, width, height] array, where null marks an unspecified edge. That layout matches what the platforms already stored internally (FloatArray(6) on Android, RNGHHitSlop on Apple), so the native parsers shrink to mapping null → NaN, plus the DIP→px conversion on Android.

Applied at all four producers: filterConfig (v1 and v2), prepareConfigForNativeSide (v3), bindSharedValues (v3's UI-thread path) and the button's web wrapper.

Test plan

Tested on Android, iOS and Web

Copilot AI review requested due to automatic review settings August 5, 2026 16:01
@coderabbitai

coderabbitai Bot commented Aug 5, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 82c92ff0-10f7-4a1f-8aaa-9ceb9e349158

📥 Commits

Reviewing files that changed from the base of the PR and between 101980a and f7584bc.

📒 Files selected for processing (2)
  • packages/react-native-gesture-handler/src/__tests__/hitSlop.test.ts
  • packages/react-native-gesture-handler/src/__tests__/hitSlopSharedValue.test.ts
💤 Files with no reviewable changes (1)
  • packages/react-native-gesture-handler/src/tests/hitSlop.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/react-native-gesture-handler/src/tests/hitSlopSharedValue.test.ts

📝 Walkthrough

Summary by CodeRabbit

  • Improvements

    • Standardized hitSlop handling across native, web, and gesture APIs.
    • Added consistent support for numeric, shorthand, per-edge, width, and height configurations.
    • Preserved omitted, undefined, and explicit null values appropriately.
    • Improved handling of shared and animated configuration updates.
  • Bug Fixes

    • Ensured hit-slop settings apply consistently during setup and subsequent updates.
    • Added validation for invalid dimensions and conflicting constraints with clearer error handling.

Walkthrough

The change normalizes hit-slop inputs into a canonical six-element array. Legacy, v3, web, Android, and Apple paths now consume this format. Tests cover normalization, validation, shared values, wiring, and nullish behavior.

Changes

Hit-slop normalization

Layer / File(s) Summary
Canonical hit-slop contract and validation
packages/react-native-gesture-handler/src/handlers/hitSlop.ts, packages/react-native-gesture-handler/src/handlers/utils.ts, packages/react-native-gesture-handler/src/v3/types/ConfigTypes.ts, packages/react-native-gesture-handler/src/web/interfaces.ts
normalizeHitSlop converts numeric and object inputs into [left, top, right, bottom, width, height]. Configuration types use CanonicalHitSlop.
Configuration and shared-value wiring
packages/react-native-gesture-handler/src/v3/hooks/utils/*, packages/react-native-gesture-handler/src/components/GestureHandlerButton.web.tsx, packages/react-native-gesture-handler/src/__tests__/hitSlopWiring.test.ts, packages/react-native-gesture-handler/src/__tests__/hitSlopSharedValue.test.ts
Configuration paths normalize hit-slop values. Tests cover normalized values, nullish values, and unchanged non-hit-slop values.
Native tuple consumption
packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt, packages/react-native-gesture-handler/apple/RNGestureHandler.mm, packages/react-native-gesture-handler/apple/RNGestureHandlerButtonComponentView.mm
Android and Apple code consume the six-element array. Android converts DIP values to pixels.
Web bounds calculation
packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts, packages/react-native-gesture-handler/src/web/interfaces.ts
The web handler stores canonical tuples and calculates bounds from nullable edges, width, and height values.
Normalization and wiring tests
packages/react-native-gesture-handler/src/__tests__/hitSlop.test.ts, packages/react-native-gesture-handler/src/__tests__/hitSlopWiring.test.ts
Tests cover shorthand expansion, explicit-edge precedence, dimensions, validation, and nullish handling.

Sequence Diagram(s)

sequenceDiagram
  participant GestureConfig
  participant normalizeHitSlop
  participant NativeConfig
  participant GestureHandler
  GestureConfig->>normalizeHitSlop: raw hitSlop value
  normalizeHitSlop-->>GestureConfig: canonical six-element array
  GestureConfig->>NativeConfig: normalized hitSlop
  NativeConfig->>GestureHandler: apply hit-slop tuple
  GestureHandler-->>GestureConfig: updated gesture bounds
Loading

Suggested reviewers: m-bert, j-piasecki

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: moving hitSlop normalization from native code to JavaScript.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR centralizes hitSlop parsing/validation in a shared JS module and changes the native/web consumers to accept a single canonical shape ([left, top, right, bottom, width, height] with null for “unset”), reducing platform-specific parsing logic.

Changes:

  • Introduces normalizeHitSlop + CanonicalHitSlop in JS and updates config producers (v1/v2 filterConfig, v3 native config prep, v3 shared-value UI-thread updates, and the web button wrapper) to emit the canonical array.
  • Simplifies native (Android/iOS) hitSlop parsing to fixed-index reads, plus DIP→px conversion on Android.
  • Updates web handler types and hit-testing logic to consume the canonical array and adds targeted Jest tests to lock the wire contract.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
packages/react-native-gesture-handler/src/web/interfaces.ts Switches web config types from object HitSlop to CanonicalHitSlop.
packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts Updates web handler to store/consume canonical hitSlop arrays; removes local validation.
packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts Normalizes hitSlop when pushed via UI-thread shared value updates.
packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts Normalizes hitSlop in v3 config preparation (including shared values).
packages/react-native-gesture-handler/src/utils.ts Adds isWorkletRuntime helper for worklet-vs-RN runtime detection.
packages/react-native-gesture-handler/src/handlers/utils.ts Normalizes hitSlop in v1/v2 filterConfig.
packages/react-native-gesture-handler/src/handlers/hitSlop.ts Adds shared canonical hitSlop type + normalization/validation logic (worklet-safe).
packages/react-native-gesture-handler/src/components/GestureHandlerButton.web.tsx Normalizes button hitSlop before passing to the web module.
packages/react-native-gesture-handler/src/tests/hitSlopWiring.test.ts Adds wiring tests for normalization at multiple producers.
packages/react-native-gesture-handler/src/tests/hitSlopSharedValue.test.ts Adds test ensuring UI-thread shared-value path normalizes hitSlop.
packages/react-native-gesture-handler/src/tests/hitSlop.test.ts Adds unit tests for normalizeHitSlop, including worklet-runtime behavior.
packages/react-native-gesture-handler/apple/RNGestureHandlerButtonComponentView.mm Updates button hitSlop config emission to the canonical array shape.
packages/react-native-gesture-handler/apple/RNGestureHandler.mm Updates iOS handler hitSlop parsing to read canonical array indices.
packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt Updates Android hitSlop parsing to read canonical array indices and convert DIP→px.
Suppressed comments (1)

packages/react-native-gesture-handler/src/tests/hitSlop.test.ts:205

  • These assignments also rely on globalThis.__RUNTIME_KIND / _WORKLET typings. Once a local g is introduced, use it consistently here to avoid TS errors.
    test('reports instead of throwing', () => {
      // 2 is the UI runtime.
      globalThis.__RUNTIME_KIND = 2;
      expectReported();
    });

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/react-native-gesture-handler/src/handlers/hitSlop.ts Outdated
Comment thread packages/react-native-gesture-handler/src/utils.ts Outdated
Comment thread packages/react-native-gesture-handler/src/__tests__/hitSlop.test.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (1)
packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts (1)

110-117: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Reuse maybeUnpackValue for the shared-value unwrapping.

Line 102 in the same function already unwraps through maybeUnpackValue. The inline Reanimated?.isSharedValue(value) ? value.value : value duplicates that rule. Use the helper so both call sites stay in sync.

♻️ Proposed refactor
-      const unpackedValue = Reanimated?.isSharedValue(value)
-        ? value.value
-        : value;
+      const unpackedValue = maybeUnpackValue(value);
 
       (filteredConfig as Record<string, unknown>)[key] =
         key === 'hitSlop'
           ? normalizeHitSlop(unpackedValue as HitSlop)
           : unpackedValue;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts`
around lines 110 - 117, In the config filtering logic, replace the inline
Reanimated shared-value check and value access before the hitSlop handling with
the existing maybeUnpackValue helper already used earlier in the same function.
Keep the subsequent normalizeHitSlop and filteredConfig assignment behavior
unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt`:
- Around line 988-1003: Update the hitSlop reader around the local edge function
and handler.setHitSlop call to validate each tuple entry before accessing it:
treat out-of-bounds or non-numeric values as GestureHandler.HIT_SLOP_NONE,
matching Apple behavior for malformed arrays. Use ReadableArray bounds/type
checks before getDouble so malformed hitSlop configuration cannot crash native
updates.

In `@packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts`:
- Around line 879-915: Add web hit-slop coverage in GestureHandler.test.ts for
zero-valued edges, explicitly null edges, and combinations of
left/right/top/bottom with width/height sizing. Ensure the tests validate the
bounds computed by the hit-slop logic in GestureHandler, then run yarn lint:js,
yarn format:js, yarn ts-check, and yarn test GestureHandler.test.ts with
dependencies installed.

---

Nitpick comments:
In `@packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts`:
- Around line 110-117: In the config filtering logic, replace the inline
Reanimated shared-value check and value access before the hitSlop handling with
the existing maybeUnpackValue helper already used earlier in the same function.
Keep the subsequent normalizeHitSlop and filteredConfig assignment behavior
unchanged.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 68c6aa8c-5054-4c13-bc42-508a04972fe0

📥 Commits

Reviewing files that changed from the base of the PR and between e58c43a and c19ebcd.

📒 Files selected for processing (14)
  • packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt
  • packages/react-native-gesture-handler/apple/RNGestureHandler.mm
  • packages/react-native-gesture-handler/apple/RNGestureHandlerButtonComponentView.mm
  • packages/react-native-gesture-handler/src/__tests__/hitSlop.test.ts
  • packages/react-native-gesture-handler/src/__tests__/hitSlopSharedValue.test.ts
  • packages/react-native-gesture-handler/src/__tests__/hitSlopWiring.test.ts
  • packages/react-native-gesture-handler/src/components/GestureHandlerButton.web.tsx
  • packages/react-native-gesture-handler/src/handlers/hitSlop.ts
  • packages/react-native-gesture-handler/src/handlers/utils.ts
  • packages/react-native-gesture-handler/src/utils.ts
  • packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts
  • packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts
  • packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts
  • packages/react-native-gesture-handler/src/web/interfaces.ts

@coado
coado marked this pull request as ready for review August 7, 2026 12:02
Comment thread packages/react-native-gesture-handler/apple/RNGestureHandler.mm Outdated
Comment thread packages/react-native-gesture-handler/apple/RNGestureHandler.mm Outdated
Comment thread packages/react-native-gesture-handler/src/handlers/hitSlop.ts Outdated
Comment thread packages/react-native-gesture-handler/src/handlers/hitSlop.ts Outdated
Comment thread packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts Outdated
Comment thread packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts Outdated
Comment thread packages/react-native-gesture-handler/src/utils.ts Outdated

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/react-native-gesture-handler/src/v3/types/ConfigTypes.ts`:
- Line 77: Resolve the failing Jest cases in hitSlop.test.ts and
hitSlopSharedValue.test.ts by tracing the hitSlop type and runtime handling from
ConfigTypes.ts, then update the implementation or tests so both regular and
shared-value hitSlop behavior passes. Verify the package test suite succeeds
without regressing the existing lint, formatting, or TypeScript checks.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: efb67868-57be-440a-be3e-8b1de90b58f9

📥 Commits

Reviewing files that changed from the base of the PR and between c19ebcd and 881869a.

📒 Files selected for processing (5)
  • packages/react-native-gesture-handler/apple/RNGestureHandler.mm
  • packages/react-native-gesture-handler/src/handlers/hitSlop.ts
  • packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts
  • packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts
  • packages/react-native-gesture-handler/src/v3/types/ConfigTypes.ts
💤 Files with no reviewable changes (1)
  • packages/react-native-gesture-handler/apple/RNGestureHandler.mm
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts
  • packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts
  • packages/react-native-gesture-handler/src/handlers/hitSlop.ts

enabled?: boolean | undefined;
shouldCancelWhenOutside?: boolean | undefined;
hitSlop?: HitSlop | undefined;
hitSlop?: HitSlop | CanonicalHitSlop | undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/usr/bin/env bash
set -euo pipefail

yarn lint:js
yarn format:js
yarn ts-check
(cd packages/react-native-gesture-handler && yarn test)

Repository: software-mansion/react-native-gesture-handler

Length of output: 50401


🏁 Script executed:

#!/usr/bin/env bash
set -u

printf '%s\n' '--- root scripts ---'
node - <<'JS'
const p = require('./package.json');
for (const [k, v] of Object.entries(p.scripts || {})) {
  if (/^(lint:js|format:js|ts-check|test)/.test(k)) console.log(`${k}: ${v}`);
}
JS

printf '%s\n' '--- package scripts ---'
node - <<'JS'
const p = require('./packages/react-native-gesture-handler/package.json');
for (const [k, v] of Object.entries(p.scripts || {})) {
  if (/^(lint:js|format:js|ts-check|test)/.test(k)) console.log(`${k}: ${v}`);
}
JS

printf '%s\n' '--- combined-command tail ---'
log=/tmp/coderabbit-shell-logs/shell-output-WkHJhD
if [ -f "$log" ]; then
  tail -n 120 "$log"
else
  printf '%s\n' 'Log not available'
fi

printf '%s\n' '--- repository status summary ---'
git status --short
git diff --stat

Repository: software-mansion/react-native-gesture-handler

Length of output: 10604


🏁 Script executed:

#!/usr/bin/env bash
set -u

printf '%s\n' '--- root scripts and workspace configuration ---'
node - <<'JS'
const p = require('./package.json');
console.log(JSON.stringify({
  scripts: p.scripts || {},
  packageManager: p.packageManager,
  workspaces: p.workspaces
}, null, 2));
JS

printf '%s\n' '--- target type and related symbols ---'
cat -n packages/react-native-gesture-handler/src/v3/types/ConfigTypes.ts | sed -n '1,105p'
rg -n "normalizeHitSlop|expectReported|bindSharedValues|mockUpdateGestureHandlerConfig|HitSlop" \
  packages/react-native-gesture-handler/src/__tests__/hitSlop.test.ts \
  packages/react-native-gesture-handler/src/__tests__/hitSlopSharedValue.test.ts \
  packages/react-native-gesture-handler/src/handlers/hitSlop.ts

printf '%s\n' '--- recent commit summary ---'
git log --oneline -3
git show --stat --oneline HEAD
git diff HEAD^ HEAD -- packages/react-native-gesture-handler/src/v3/types/ConfigTypes.ts \
  packages/react-native-gesture-handler/src/handlers/hitSlop.ts \
  packages/react-native-gesture-handler/src/__tests__/hitSlop.test.ts \
  packages/react-native-gesture-handler/src/__tests__/hitSlopSharedValue.test.ts

printf '%s\n' '--- validation-stage markers from the prior log ---'
log=/tmp/coderabbit-shell-logs/shell-output-WkHJhD
if [ -f "$log" ]; then
  rg -n "tsc|ts-check|Test Suites:|Tests:|FAIL |PASS |Done in|error TS|error Command|Exit code|Command failed" "$log" | tail -n 100
fi

Repository: software-mansion/react-native-gesture-handler

Length of output: 13910


Resolve the failing Jest tests before merge.

yarn lint:js, yarn format:js, and yarn ts-check pass. Package yarn test fails in hitSlop.test.ts and hitSlopSharedValue.test.ts.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/react-native-gesture-handler/src/v3/types/ConfigTypes.ts` at line
77, Resolve the failing Jest cases in hitSlop.test.ts and
hitSlopSharedValue.test.ts by tracing the hitSlop type and runtime handling from
ConfigTypes.ts, then update the implementation or tests so both regular and
shared-value hitSlop behavior passes. Verify the package test suite succeeds
without regressing the existing lint, formatting, or TypeScript checks.

Source: Coding guidelines


prop = config[@"hitSlop"];
if ([prop isKindOfClass:[NSNumber class]]) {
_hitSlop.left = _hitSlop.right = _hitSlop.top = _hitSlop.bottom = [prop doubleValue];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave the single-number path in place on both platforms? Handling this is a matter of building a rect using the number, and passing it is much more efficient than the wrappers RN is using for objects (at least on Android).

if (!isnan(_hitSlop.height) && !isnan(_hitSlop.top) && !isnan(_hitSlop.bottom)) {
RCTLogError(@"Cannot have all of top, bottom and height defined");
}
// An explicit `null` clears the hit slop; a missing key leaves the previous value alone.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't explicit null be translated to nil?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants