Only adjust the drag hoverCount on an actual hover transition - #4977
Merged
Conversation
useDragLeave shares a module-level hoverCount across every drop target, and debounces a clear of state.hoveringPath when it reaches zero. The effect treated every run as a hover transition: any run that was not a false→true change of isDeepHovering fell into the `else` branch and decremented the shared count. That branch is reached on mount, so any thought mounting mid-drag decremented the count — dropping it to zero and blanking the drop indicator while a target was still hovered. Its cleanup was commented "Cleanup on unmount" but listed four dependencies, so React ran it before every re-run rather than only on unmount. It also never decremented, so a drop target unmounted mid-drag leaked its count and hoveringPath was never cleared. Guard the count on `isDeepHovering !== isCountedRef.current` so only a real enter or leave adjusts it, and move the release into its own effect with empty deps, where it decrements this target's contribution. The empty deps are what make it an unmount handler. Adds hook tests for the two reproduced failures (an unrelated thought mounting mid-hover; a hovered target unmounting), a guard that the count still reaches zero on leave, and documents the counting rule in docs/drag-and-drop.md. Co-Authored-By: Claude Opus 5 (unknown context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Second finding from the audit prompted by #4969 (a cleanup that latched state because React re-runs cleanups on every dependency change, not only on unmount). Independent of #4975.
Problem
useDragLeaveshares a module-levelhoverCountacross every drop target and debounces a clear ofstate.hoveringPathwhen it reaches zero. Two defects:1. Every effect run was treated as a hover transition. Any run that was not a
false → truechange ofisDeepHoveringfell into theelsebranch and decremented the shared count:The effect runs on mount, so any thought mounting mid-drag decrements the count. With one target genuinely hovered, the count goes to zero and
hoveringPathis cleared — the blue drop indicator disappears while the cursor is still over the target.2. The cleanup claimed to be an unmount handler but wasn't. It was commented
// Cleanup on unmountwhile the effect listed four dependencies, so React ran it before every re-run. It also never decremented, so a drop target unmounted mid-drag leaked its count andhoveringPathwas then never cleared.Both are reproduced by the new tests, which fail on
main:Solution
isDeepHovering !== isCountedRef.current, so only a real enter or leave adjusts it. Mounts andcanDropThoughtchanges no longer touch the shared count.Verification
yarn lint:tsc,eslinton the changed filesdocs/drag-and-drop.mdupdated with the counting rule.Not verified: I could not run the Puppeteer drag-and-drop suite locally, so the change is verified against the hook's semantics and unit tests rather than a real drag. Worth a manual drag before merging, particularly nested drop zones (
ThoughtanduseDragAndDropSubThoughteach register auseDragLeave).🤖 Generated with Claude Code