From 1d9f0817b340067fe4874a2042aa0ee6b13dd75e Mon Sep 17 00:00:00 2001 From: wanxiankai Date: Mon, 9 Nov 2026 23:19:01 +0800 Subject: [PATCH] fix(useDropArea): preserve over state across child transitions --- docs/useDrop.md | 2 + src/useDropArea.ts | 17 ++- stories/useDropArea.story.tsx | 13 +- tests/useDropArea.test.tsx | 238 ++++++++++++++++++++++++++++++++++ 4 files changed, 263 insertions(+), 7 deletions(-) create mode 100644 tests/useDropArea.test.tsx diff --git a/docs/useDrop.md b/docs/useDrop.md index 5fee420849..12b2c064bb 100644 --- a/docs/useDrop.md +++ b/docs/useDrop.md @@ -5,6 +5,8 @@ Triggers on file, link drop and copy-paste. `useDrop` tracks events for the whole page, `useDropArea` tracks drop events for a specific element. +`useDropArea` keeps `state.over` active while dragging over the element or its +children, and resets it when the drag leaves the area or an item is dropped. ## Usage diff --git a/src/useDropArea.ts b/src/useDropArea.ts index 52ca8fae62..6d294342a0 100644 --- a/src/useDropArea.ts +++ b/src/useDropArea.ts @@ -1,4 +1,4 @@ -import { useMemo, useState } from 'react'; +import { MutableRefObject, useMemo, useRef, useState } from 'react'; import useMountedState from './useMountedState'; import { noop } from './misc/util'; @@ -49,20 +49,25 @@ const createProcess = } }; -const createBond = (process, setOver): DropAreaBond => ({ +const createBond = (process, setOver, dragDepth: MutableRefObject): DropAreaBond => ({ onDragOver: (event) => { event.preventDefault(); }, onDragEnter: (event) => { event.preventDefault(); + dragDepth.current += 1; setOver(true); }, onDragLeave: () => { - setOver(false); + dragDepth.current = Math.max(0, dragDepth.current - 1); + if (dragDepth.current === 0) { + setOver(false); + } }, onDrop: (event) => { event.preventDefault(); event.persist(); + dragDepth.current = 0; setOver(false); process(event.dataTransfer, event); }, @@ -76,8 +81,12 @@ const useDropArea = (options: DropAreaOptions = {}): [DropAreaBond, DropAreaStat const { onFiles, onText, onUri } = options; const isMounted = useMountedState(); const [over, setOver] = useState(false); + const dragDepth = useRef(0); const process = useMemo(() => createProcess(options, isMounted()), [onFiles, onText, onUri]); - const bond: DropAreaBond = useMemo(() => createBond(process, setOver), [process, setOver]); + const bond: DropAreaBond = useMemo( + () => createBond(process, setOver, dragDepth), + [process, setOver] + ); return [bond, { over }]; }; diff --git a/stories/useDropArea.story.tsx b/stories/useDropArea.story.tsx index f99ecd5e42..6385a8f524 100644 --- a/stories/useDropArea.story.tsx +++ b/stories/useDropArea.story.tsx @@ -4,7 +4,7 @@ import * as React from 'react'; import { useDropArea } from '../src'; import ShowDocs from './util/ShowDocs'; -const Demo = () => { +const Demo = ({ children }: { children?: React.ReactNode }) => { const [bond, state] = useDropArea({ onFiles: action('onFiles'), onUri: action('onUri'), @@ -30,7 +30,7 @@ const Demo = () => { return (
- Drop here + {children || 'Drop here'}
    @@ -53,4 +53,11 @@ const Demo = () => { storiesOf('UI/useDropArea', module) .add('Docs', () => ) - .add('Default', () => ); + .add('Default', () => ) + .add('With children', () => ( + +
    + Drag over this child +
    +
    + )); diff --git a/tests/useDropArea.test.tsx b/tests/useDropArea.test.tsx new file mode 100644 index 0000000000..22c67a07e7 --- /dev/null +++ b/tests/useDropArea.test.tsx @@ -0,0 +1,238 @@ +import React from 'react'; +import { createEvent, fireEvent, render } from '@testing-library/react'; +import useDropArea, { DropAreaOptions } from '../src/useDropArea'; + +const DropArea = (options: DropAreaOptions) => { + const [bond, { over }] = useDropArea(options); + + return ( +
    +
    + Drop here +
    +
    Or here
    +
    + ); +}; + +const dataTransfer = { + getData: () => 'https://example.com', +}; + +describe('useDropArea', () => { + it('should enter and leave the drop area', () => { + const { getByTestId } = render(); + const area = getByTestId('area'); + const enter = createEvent.dragEnter(area); + const over = createEvent.dragOver(area); + + expect(area.getAttribute('data-over')).toBe('false'); + + fireEvent(area, enter); + expect(enter.defaultPrevented).toBe(true); + expect(area.getAttribute('data-over')).toBe('true'); + + fireEvent(area, over); + expect(over.defaultPrevented).toBe(true); + + fireEvent.dragLeave(area); + expect(area.getAttribute('data-over')).toBe('false'); + }); + + it('should stay over the area when entering a child without relatedTarget', () => { + const { getByTestId } = render(); + const area = getByTestId('area'); + const child = getByTestId('child'); + + fireEvent.dragEnter(area); + fireEvent.dragEnter(child); + fireEvent.dragLeave(area); + + expect(area.getAttribute('data-over')).toBe('true'); + + fireEvent.dragLeave(child); + expect(area.getAttribute('data-over')).toBe('false'); + }); + + it('should stay over the area when moving between descendants', () => { + const { getByTestId } = render(); + const area = getByTestId('area'); + const child = getByTestId('child'); + const grandchild = getByTestId('grandchild'); + const sibling = getByTestId('sibling'); + + fireEvent.dragEnter(child); + fireEvent.dragEnter(grandchild); + fireEvent.dragLeave(child); + expect(area.getAttribute('data-over')).toBe('true'); + + fireEvent.dragEnter(sibling); + fireEvent.dragLeave(grandchild); + expect(area.getAttribute('data-over')).toBe('true'); + + fireEvent.dragLeave(sibling); + expect(area.getAttribute('data-over')).toBe('false'); + }); + + it('should preserve drag state when the callbacks change during a drag', () => { + const onUri = jest.fn(); + const nextOnUri = jest.fn(); + const { getByTestId, rerender } = render(); + const area = getByTestId('area'); + const child = getByTestId('child'); + + fireEvent.dragEnter(area); + fireEvent.dragEnter(child); + rerender(); + fireEvent.dragLeave(area); + + expect(area.getAttribute('data-over')).toBe('true'); + + fireEvent.drop(child, { dataTransfer }); + expect(area.getAttribute('data-over')).toBe('false'); + expect(onUri).not.toHaveBeenCalled(); + expect(nextOnUri).toHaveBeenCalledWith('https://example.com', expect.any(Object)); + }); + + it('should reset nested drag entries after a drop', () => { + const onUri = jest.fn(); + const { getByTestId } = render(); + const area = getByTestId('area'); + const child = getByTestId('child'); + const drop = createEvent.drop(child, { dataTransfer }); + + fireEvent.dragEnter(area); + fireEvent.dragEnter(child); + fireEvent(child, drop); + + expect(drop.defaultPrevented).toBe(true); + expect(area.getAttribute('data-over')).toBe('false'); + expect(onUri).toHaveBeenCalledTimes(1); + + fireEvent.dragEnter(area); + fireEvent.dragLeave(area); + expect(area.getAttribute('data-over')).toBe('false'); + }); + + it('should tolerate dragleave events after a drop before the next drag', () => { + const { getByTestId } = render(); + const area = getByTestId('area'); + const child = getByTestId('child'); + + fireEvent.dragEnter(child); + fireEvent.drop(child, { dataTransfer }); + fireEvent.dragLeave(child); + + fireEvent.dragEnter(area); + fireEvent.dragEnter(child); + fireEvent.dragLeave(area); + expect(area.getAttribute('data-over')).toBe('true'); + + fireEvent.dragLeave(child); + expect(area.getAttribute('data-over')).toBe('false'); + }); + + it('should preserve an active drag when pasting', () => { + const onUri = jest.fn(); + const { getByTestId } = render(); + const area = getByTestId('area'); + const child = getByTestId('child'); + + fireEvent.dragEnter(area); + fireEvent.dragEnter(child); + fireEvent.paste(area, { clipboardData: dataTransfer }); + fireEvent.dragLeave(area); + + expect(onUri).toHaveBeenCalledTimes(1); + expect(area.getAttribute('data-over')).toBe('true'); + + fireEvent.dragLeave(child); + expect(area.getAttribute('data-over')).toBe('false'); + }); + + it('should track nested drop areas independently', () => { + const NestedAreas = () => { + const [outerBond, outerState] = useDropArea(); + const [innerBond, innerState] = useDropArea(); + + return ( +
    +
    +
    + ); + }; + const { getByTestId } = render(); + const outer = getByTestId('outer'); + const inner = getByTestId('inner'); + + fireEvent.dragEnter(outer); + fireEvent.dragEnter(inner); + fireEvent.dragLeave(outer); + expect(outer.getAttribute('data-over')).toBe('true'); + expect(inner.getAttribute('data-over')).toBe('true'); + + fireEvent.dragEnter(outer); + fireEvent.dragLeave(inner); + expect(outer.getAttribute('data-over')).toBe('true'); + expect(inner.getAttribute('data-over')).toBe('false'); + + fireEvent.dragLeave(outer); + expect(outer.getAttribute('data-over')).toBe('false'); + }); + + it.each(['drop', 'paste'])('should continue to process files on %s', (eventName) => { + const onFiles = jest.fn(); + const onText = jest.fn(); + const file = new File(['contents'], 'example.txt', { type: 'text/plain' }); + const transfer = { getData: () => '', files: [file] }; + const { getByTestId } = render(); + const area = getByTestId('area'); + + if (eventName === 'drop') { + fireEvent.drop(area, { dataTransfer: transfer }); + } else { + fireEvent.paste(area, { clipboardData: transfer }); + } + + expect(onFiles).toHaveBeenCalledWith([file], expect.any(Object)); + expect(onText).not.toHaveBeenCalled(); + }); + + it('should process text with the latest callback after the options change', () => { + const onText = jest.fn(); + const nextOnText = jest.fn(); + const { getByTestId, rerender } = render(); + const area = getByTestId('area'); + + rerender(); + fireEvent.drop(area, { + dataTransfer: { + getData: () => '', + files: [], + items: [{ getAsString: (callback) => callback('Dropped text') }], + }, + }); + + expect(onText).not.toHaveBeenCalled(); + expect(nextOnText).toHaveBeenCalledWith('Dropped text', expect.any(Object)); + }); + + it.each([ + { getData: () => '', files: [new File(['contents'], 'example.txt')] }, + { getData: () => '', items: [{ getAsString: (callback) => callback('Dropped text') }] }, + { getData: () => '' }, + { getData: () => '', files: [], items: [] }, + ])('should accept drops without registered callbacks', (transfer) => { + const { getByTestId, rerender } = render(); + const area = getByTestId('area'); + + fireEvent.dragEnter(area); + fireEvent.drop(area, { dataTransfer: transfer }); + expect(area.getAttribute('data-over')).toBe('false'); + + rerender(); + fireEvent.dragEnter(area); + fireEvent.drop(area, { dataTransfer: transfer }); + expect(area.getAttribute('data-over')).toBe('false'); + }); +});