Skip to content

frontend / shared/lib/useHorizontalDragScroll / useHorizontalDragScroll

Function: useHorizontalDragScroll() ​

useHorizontalDragScroll<T>(): object

Defined in: shared/lib/useHorizontalDragScroll.ts:35

Adds drag-to-scroll behaviour for horizontal scrolling of element.

Returns ref which should be attached to scrollable element, pointer down handler for same element, and predicate which tells if last interaction was a drag. Use this predicate inside onClick handlers to avoid action after user just dragged the container.

Only left mouse button starts the drag. Threshold of 3 pixels is used to separate drag from click — if movement is smaller than 3 pixels, drag flag stays false and click works as usual.

Type Parameters ​

T ​

T extends HTMLElement

Type of scrollable element, for example HTMLDivElement.

Returns ​

object

  • elementRef — ref for scrollable element.
  • onPointerDown — handler for same element.
  • wasDragged() — returns true if last pointer sequence moved more than threshold; call it inside onClick to skip the click.

elementRef ​

elementRef: RefObject<T | null>

onPointerDown ​

onPointerDown: (e) => void

Parameters ​

e ​

PointerEvent

Returns ​

void

wasDragged ​

wasDragged: () => boolean

Returns ​

boolean

Example ​

tsx
const { elementRef, onPointerDown, wasDragged } = useHorizontalDragScroll<HTMLDivElement>();
return (
  <div ref={elementRef} onPointerDown={onPointerDown}>
    <button onClick={() => { if (wasDragged()) return; handleClick(); }}>
      Item
    </button>
  </div>
);