Skip to content

fix: guard drag movement without container - #1087

Open
nrps9909 wants to merge 1 commit into
react-component:masterfrom
nrps9909:codex/slider-null-container-1063
Open

fix: guard drag movement without container#1087
nrps9909 wants to merge 1 commit into
react-component:masterfrom
nrps9909:codex/slider-null-container-1063

Conversation

@nrps9909

@nrps9909 nrps9909 commented Aug 19, 2026

Copy link
Copy Markdown

Summary

  • ignore active drag move events after the slider container ref becomes unavailable
  • keep the existing mouseup/touchend cleanup and finish path intact
  • add a hook-level regression test that covers the reported null-ref crash and verifies drag cleanup

Fixes #1063.

Why

The document-level move listener can run after containerRef.current becomes null. It currently dereferences the ref unconditionally and throws while reading getBoundingClientRect(). The listener now snapshots the container and returns only for that move when it is unavailable. The independent end handler still removes all listeners, calls finishChange, and resets drag state.

Validation

  • Exact-base red proof: the new regression fails on 9982e48 with TypeError: Cannot read properties of null (reading getBoundingClientRect)
  • npm test -- tests/useDrag.test.tsx --runInBand — 1/1 passed
  • npm test -- --runInBand — 6 suites, 120/120 tests, 5/5 snapshots passed
  • npm run tsc
  • npm run lint
  • npx eslint src/hooks/useDrag.ts tests/useDrag.test.tsx
  • npx prettier --check src/hooks/useDrag.ts tests/useDrag.test.tsx
  • npm run compile
  • git diff --check

The full suite still prints pre-existing React act(...) warnings in unchanged Range/Slider focus tests; it exits successfully and the new regression is warning-free.

I checked all 34 open PRs and their complete changed-file lists before submission; none touches either target file. The older merged #210 documents the same ref-null failure class in the pre-hook implementation and establishes a regression-test precedent, while no current duplicate patch exists.

AI assistance disclosure: Codex was used to reproduce the exact-base failure, trace the move/end listener ownership, draft the focused guard and regression, audit open PR overlap, and run the validation above. All behavior, test counts, commit state, and duplicate checks are directly verifiable.

Summary by CodeRabbit

  • Bug 修复
    • 修复拖拽过程中容器暂时不可用时可能发生的异常。
    • 在此情况下,拖拽位置不会被错误更新,释放鼠标后仍可正常完成拖拽。
  • 测试
    • 新增相关测试,验证异常场景下的拖拽行为和完成回调处理。

@vercel

vercel Bot commented Aug 19, 2026

Copy link
Copy Markdown

@nrps9909 is attempting to deploy a commit to the afc163's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a4d1c884-4808-4172-955b-acb59ab0700f

📥 Commits

Reviewing files that changed from the base of the PR and between 9982e48 and 4cc2232.

📒 Files selected for processing (2)
  • src/hooks/useDrag.ts
  • tests/useDrag.test.tsx

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


Walkthrough

Changes

useDrag 拖拽稳定性

Layer / File(s) Summary
容器引用保护与拖拽状态验证
src/hooks/useDrag.ts, tests/useDrag.test.tsx
onMouseMove 在容器引用为 null 时直接返回。新增测试验证鼠标移动不会抛错或改变值,mouseUp 只调用一次完成回调,并将拖拽索引重置为 -1

Estimated code review effort: 2 (简单) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 4cc22

This localized change prevents drag-move handling from dereferencing an unavailable container while preserving cleanup behavior; no actionable merge-blocking risk remains after normal checks and review.

Poem

小兔检查引用空,
拖动遇险也从容。
鼠标移动不报错,
释放只响一次钟。
索引归零,胡萝卜香。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 标题准确概括了为拖拽移动增加容器引用保护这一主要变更。
Linked Issues check ✅ Passed 代码在容器引用为空时提前返回,避免调用getBoundingClientRect()并解决问题#1063
Out of Scope Changes check ✅ Passed 所有代码和测试变更均围绕useDrag的空引用保护及拖拽清理行为,未发现无关修改。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

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.

TypeError in useDrag

1 participant