fix: auto scroll when drag to outside

This commit is contained in:
Ou 2024-11-11 00:27:48 +08:00
parent 47808a8c7f
commit 02f6f008fa
4 changed files with 13 additions and 3 deletions

View File

@ -34,5 +34,6 @@ export const currentSourcesAtom = atom((get) => {
export const goToTopAtom = atom({
ok: false,
el: undefined as HTMLElement | undefined,
fn: undefined as (() => void) | undefined,
})

View File

@ -94,8 +94,12 @@ function DndWrapper({ items, setItems, children }: PropsWithChildren<{
trailing: true,
wait: AnimationDuration,
})
const { el } = useAtomValue(goToTopAtom)
useEffect(() => {
console.log(el)
}, [el])
return (
<DndContext onDropTargetChange={run}>
<DndContext onDropTargetChange={run} autoscroll={el ? { element: el } : undefined}>
{children}
</DndContext>
)

View File

@ -1,12 +1,15 @@
import { monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/element/adapter"
import { combine } from "@atlaskit/pragmatic-drag-and-drop/combine"
import { autoScrollForElements } from "@atlaskit/pragmatic-drag-and-drop-auto-scroll/element"
import type { PropsWithChildren } from "react"
import type { AllEvents, ElementDragType } from "@atlaskit/pragmatic-drag-and-drop/dist/types/internal-types"
import type { ElementAutoScrollArgs } from "@atlaskit/pragmatic-drag-and-drop-auto-scroll/dist/types/internal-types"
import { InstanceIdContext } from "./useSortable"
interface ContextProps extends Partial<AllEvents<ElementDragType>> {
autoscroll?: ElementAutoScrollArgs<ElementDragType>
}
export function DndContext({ children, ...callback }: PropsWithChildren<ContextProps>) {
export function DndContext({ children, autoscroll, ...callback }: PropsWithChildren<ContextProps>) {
const [instanceId] = useState<string>(randomUUID())
useEffect(() => {
return (
@ -17,9 +20,10 @@ export function DndContext({ children, ...callback }: PropsWithChildren<ContextP
},
...callback,
}),
autoscroll ? autoScrollForElements(autoscroll) : () => { },
)
)
}, [callback, instanceId])
}, [callback, instanceId, autoscroll])
return (
<InstanceIdContext.Provider value={instanceId}>
{children}

View File

@ -71,6 +71,7 @@ export function GlobalOverlayScrollbar({ children, className, ...props }: PropsW
const el = e.target as HTMLElement
setGoToTop({
ok: el.scrollTop > 100,
el,
fn: () => el.scrollTo({ top: 0, behavior: "smooth" }),
})
},