Skip to content
dnd-grid

useEdgeScroll

Auto-scroll scroll containers while dragging near edges.

useEdgeScroll powers the autoScroll behavior in DndGrid. Use it directly when you need auto-scroll for custom drag interactions.

Example

import { useRef } from "react";
import { AutoScrollActivator, useEdgeScroll } from "@dnd-grid/react";

export function EdgeScrollCard() {
  const cardRef = useRef<HTMLDivElement | null>(null);
  const { handleDragStart, handleDrag, handleDragEnd, isScrolling } =
    useEdgeScroll({
      activator: AutoScrollActivator.Pointer,
      threshold: { x: 0.15, y: 0.15 },
    });

  return (
    <div
      ref={cardRef}
      onPointerDown={(event) => {
        handleDragStart(event, cardRef.current);
        event.currentTarget.setPointerCapture(event.pointerId);
      }}
      onPointerMove={(event) => handleDrag(event, cardRef.current)}
      onPointerUp={() => handleDragEnd()}
      style={{ cursor: "grab", userSelect: "none" }}
    >
      {isScrolling ? "Scrolling..." : "Drag me near the edges"}
    </div>
  );
}

Options

OptionTypeDescription
enabledbooleanEnable or disable auto-scroll.
threshold{ x: number; y: number }Percent of the container edge used as the trigger zone.
accelerationnumberBase scroll speed multiplier.
intervalnumberScroll interval in ms.
activatorAutoScrollActivatorUse pointer coordinates or the dragged element rect.
orderTraversalOrderScroll ancestor order (TreeOrder or ReversedTreeOrder).
canScroll(element) => booleanOptional predicate to skip specific scroll containers.
layoutShiftCompensationboolean or { x: boolean; y: boolean }Compensate when layout shifts during drag.

Returns

ValueDescription
handleDragStartCall on drag start with the event, node, and optional { left, top } position.
handleDragCall on drag move with the event, node, and optional { left, top } position.
handleDragEndCall on drag end.
isScrollingtrue while auto-scroll is active.