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({ export const goToTopAtom = atom({
ok: false, ok: false,
el: undefined as HTMLElement | undefined,
fn: undefined as (() => void) | undefined, fn: undefined as (() => void) | undefined,
}) })

View File

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

View File

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

View File

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