Skip to main content

useScroll

React Sensor Hook that tracks scroll position and stat

Usage

Live Editor
function Demo() {
  const elementRef = useRef<HTMLDivElement>(null);
  const [x, y, isScrolling, arrivedState, directions] = useScroll(elementRef);
  const { left, right, top, bottom } = useMemo(
    () => arrivedState,
    [arrivedState],
  );
  const {
    left: toLeft,
    right: toRight,
    top: toTop,
    bottom: toBottom,
  } = useMemo(() => directions, [directions]);

  const absoluteStyle: CSSProperties = {
    paddingTop: "0.25rem",
    paddingBottom: "0.25rem",
    paddingLeft: "0.5rem",
    paddingRight: "0.5rem",
    position: "absolute",
  };
  return (
    <div style={{ display: "flex" }}>
      <div
        ref={elementRef}
        style={{
          width: 300,
          height: 300,
          margin: "auto",
          borderRadius: "0.25rem",
          overflow: "scroll",
        }}
      >
        <div style={{ width: 500, height: 400, position: "relative" }}>
          <div
            style={{
              ...absoluteStyle,
              top: "0rem",
              left: "0rem",
            }}
          >
            TopLeft
          </div>
          <div
            style={{
              ...absoluteStyle,
              bottom: "0rem",
              left: "0rem",
            }}
          >
            BottomLeft
          </div>
          <div
            style={{
              ...absoluteStyle,
              top: "0rem",
              right: "0rem",
            }}
          >
            TopRight
          </div>
          <div
            style={{
              ...absoluteStyle,
              bottom: "0rem",
              right: "0rem",
            }}
          >
            BottomRight
          </div>
          <div
            style={{
              ...absoluteStyle,
              top: "33.33333%",
              left: "33.33333%",
            }}
          >
            Scroll Me
          </div>
        </div>
      </div>
      <div
        style={{
          width: 280,
          margin: "auto",
          paddingLeft: "1rem",
          display: "flex",
          flexDirection: "column",
          gap: 5,
        }}
      >
        <div>
          Position: {x.toFixed(1)}, {y.toFixed(1)}
        </div>
        <div>isScrolling: {JSON.stringify(isScrolling)}</div>
        <div>Top Arrived: {JSON.stringify(top)}</div>
        <div>Right Arrived: {JSON.stringify(right)}</div>
        <div>Bottom Arrived: {JSON.stringify(bottom)}</div>
        <div>Left Arrived: {JSON.stringify(left)}</div>
        <div>Scrolling Up: {JSON.stringify(toTop)}</div>
        <div>Scrolling Right: {JSON.stringify(toRight)}</div>
        <div>Scrolling Down: {JSON.stringify(toBottom)}</div>
        <div>Scrolling Left: {JSON.stringify(toLeft)}</div>
      </div>
    </div>
  );
};

Result
Loading...

API

useScroll

Returns

readonly [number, number, boolean, UseScrollArrivedState, UseScrollDirection]: A tuple with the following elements:

  • The x value.
  • The y value.
  • Whether it is scrolling.
  • Boundary arrival status.
  • Scroll direction.

Arguments

ArgumentDescriptionTypeDefaultValue
targetdom elmentBasicTarget<Element> | Document | Window (Required)-
optionsoptional paramsUseScrollOptions | undefined-

UseScrollOptions

PropertyDescriptionTypeDefaultValue
throttleThrottle time for scroll event, it’s disabled by default.number0
idleThe check time when scrolling ends.This configuration will be setting to (throttle + idle) when the throttle is configured.number-
offsetOffset arrived states by x pixelsUseScrollOffset-
onScrollTrigger it when scrolling.(e: Event) => void-
onStopTrigger it when scrolling ends.(e: Event) => void-
eventListenerOptionsListener options for scroll event.boolean | AddEventListenerOptions{capture: false, passive: true}

UseScrollArrivedState

PropertyDescriptionTypeDefaultValue
leftarrived leftboolean (Required)-
rightarrived rightboolean (Required)-
toparrived topboolean (Required)-
bottomarrived bottomboolean (Required)-

UseScrollDirection

PropertyDescriptionTypeDefaultValue
leftscroll leftboolean (Required)-
rightscroll rightboolean (Required)-
topscroll topboolean (Required)-
bottomscroll bottomboolean (Required)-

BasicTarget

export type BasicTarget<T extends TargetType = Element> = (() => TargetValue<T>) | TargetValue<T> | MutableRefObject<TargetValue<T>>;

TargetValue

type TargetValue<T> = T | undefined | null;

TargetType

type TargetType = HTMLElement | Element | Window | Document | EventTarget;

UseScrollOffset

export interface UseScrollOffset {
left?: number;
right?: number;
top?: number;
bottom?: number;
}