-
Notifications
You must be signed in to change notification settings - Fork 171
fix: auto-scroll when a native drag reaches the edge of a virtual list #387
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
Open
Ye-YiChen
wants to merge
2
commits into
react-component:master
Choose a base branch
from
Ye-YiChen:feat-native-drag-edge-scroll
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+410
โ1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import { raf } from '@rc-component/util'; | ||
| import * as React from 'react'; | ||
| import { smoothScrollOffset } from './useScrollDrag'; | ||
|
|
||
| /** | ||
| * The mouse-drag path in `useScrollDrag` runs on `mousemove`, which the browser | ||
| * does not fire during a native HTML5 drag โ and it deliberately skips | ||
| * draggable targets so it never fights the native drag. So when a consumer | ||
| * (e.g. a draggable Tree) drags a node toward the edge of a virtual list, the | ||
| * container is `overflow: hidden`, the browser's native drag-to-edge autoscroll | ||
| * is disabled, and nothing scrolls. | ||
| * | ||
| * Drive the edge scrolling from the native drag events instead. `dragover` only | ||
| * fires while a drag is in progress, so no extra "is dragging" flag is needed: | ||
| * the listeners can stay mounted and simply do nothing until a drag happens. | ||
| */ | ||
| export default function useDragEdgeScroll( | ||
| inVirtual: boolean, | ||
| componentRef: React.RefObject<HTMLElement>, | ||
| height: number, | ||
| itemHeight: number, | ||
| onScrollOffset: (offset: number) => void, | ||
| ) { | ||
| const onScrollOffsetRef = React.useRef(onScrollOffset); | ||
| onScrollOffsetRef.current = onScrollOffset; | ||
|
|
||
| React.useEffect(() => { | ||
| const ele = componentRef.current; | ||
| if (!inVirtual || !ele || !height) { | ||
| return; | ||
| } | ||
|
|
||
| // `height / 4` caps each band so the top and bottom never overlap on short | ||
| // containers (an idle zone always remains in the middle); `itemHeight * 1.2` | ||
| // keeps a roughly one-row band otherwise. | ||
| const edgeThreshold = Math.min(itemHeight * 1.2, height / 4); | ||
|
|
||
| let rafId: number | null = null; | ||
| let offset = 0; | ||
|
|
||
| const stopScroll = () => { | ||
| if (rafId !== null) { | ||
| raf.cancel(rafId); | ||
| rafId = null; | ||
| } | ||
| offset = 0; | ||
| }; | ||
|
|
||
| const scrollFrame = () => { | ||
| onScrollOffsetRef.current(offset); | ||
| rafId = raf(scrollFrame); | ||
| }; | ||
|
|
||
| const continueScroll = () => { | ||
| // `0` happens when the pointer sits exactly on the band boundary; spinning | ||
| // the loop for a no-op would fire a scroll callback on every frame. | ||
| if (offset === 0) { | ||
| stopScroll(); | ||
| return; | ||
| } | ||
| if (rafId === null) { | ||
| rafId = raf(scrollFrame); | ||
| } | ||
| }; | ||
|
|
||
| const onDragOver = (e: DragEvent) => { | ||
| // Skip if a nested virtual List already handled this event, the same way | ||
| // `useScrollDrag` does on `mousedown`: a nested List's `dragover` bubbles | ||
| // to both holders and only the innermost one should scroll. | ||
| const event = e as DragEvent & { _virtualHandled?: boolean }; | ||
| if (event._virtualHandled) { | ||
| return; | ||
| } | ||
| // `dragover` keeps firing while the drag lasts, so mark each event: the | ||
| // innermost holder sees it first and the outer ones bail out above. | ||
| event._virtualHandled = true; | ||
|
|
||
| const { top, bottom } = ele.getBoundingClientRect(); | ||
| const { clientY } = e; | ||
|
|
||
| if (clientY <= top + edgeThreshold) { | ||
| offset = -smoothScrollOffset(top + edgeThreshold - clientY); | ||
| continueScroll(); | ||
| } else if (clientY >= bottom - edgeThreshold) { | ||
| offset = smoothScrollOffset(clientY - (bottom - edgeThreshold)); | ||
| continueScroll(); | ||
| } else { | ||
| stopScroll(); | ||
| } | ||
| }; | ||
|
|
||
| const onDragLeave = (e: DragEvent) => { | ||
| const related = e.relatedTarget as Node | null; | ||
| if (!related || !ele.contains(related)) { | ||
| stopScroll(); | ||
| } | ||
| }; | ||
|
|
||
| const ownerDocument = ele.ownerDocument; | ||
|
|
||
| // `dragover` / `dragleave` need the container rect, so listen on it. | ||
| // `drop` / `dragend` are listened on the document in the CAPTURE phase: a | ||
| // consumer's node may stop their propagation in the bubble phase (rc-tree's | ||
| // `TreeNode` does), so capture is the only reliable place to see the drag | ||
| // ending. | ||
| ele.addEventListener('dragover', onDragOver); | ||
| ele.addEventListener('dragleave', onDragLeave); | ||
| ownerDocument.addEventListener('drop', stopScroll, true); | ||
| ownerDocument.addEventListener('dragend', stopScroll, true); | ||
|
|
||
| return () => { | ||
| stopScroll(); | ||
| ele.removeEventListener('dragover', onDragOver); | ||
| ele.removeEventListener('dragleave', onDragLeave); | ||
| ownerDocument.removeEventListener('drop', stopScroll, true); | ||
| ownerDocument.removeEventListener('dragend', stopScroll, true); | ||
| }; | ||
| }, [inVirtual, height, itemHeight]); | ||
| } | ||
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
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.