mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-19 03:09:14 +08:00
fix: auto scroll when drag to outside
This commit is contained in:
parent
47808a8c7f
commit
02f6f008fa
@ -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,
|
||||||
})
|
})
|
||||||
|
@ -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>
|
||||||
)
|
)
|
||||||
|
@ -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}
|
||||||
|
@ -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" }),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user