useScrollIntoView

React sensor hook works like element.scrollIntoView()

useScrollIntoView provides a smooth, animated scroll to a target element, similar to Element.scrollIntoView() but with more control. It returns an object with scrollIntoView (accepting alignment options like "start", "end", "center") and cancel functions. You can configure the scroll axis, duration, easing function, offset, and whether the user can interrupt the animation by scrolling.

When to Use

  • Scrolling to anchored sections with smooth, customizable animation (e.g., table of contents navigation)
  • Focusing on newly added content, error messages, or form fields after validation
  • Building scrollable lists where selecting an item scrolls it into the visible area

Notes

  • SSR-safe: Returns no-op functions during server-side rendering. No DOM scrolling occurs on the server.
  • Cancelable: Set cancelable: true (default) to allow the user to interrupt the scroll animation by manually scrolling. Set isList: true to prevent content jumping in lists.
  • Related hooks: See also useScroll for tracking scroll position and state, and useScrollLock for preventing scroll entirely.

Usage

Live Editor
function Demo() {
  const targetRef = useRef<HTMLParagraphElement>(null);
  const { scrollIntoView } = useScrollIntoView(targetRef, {
    offset: 60,
  });

  return (
    <div>
      <button onClick={() => scrollIntoView({ alignment: "center" })}>
        Scroll to target
      </button>
      <div style={{ height: "50vh" }} />
      <p ref={targetRef}>Hello there</p>
    </div>
  );
};
Result

API

useScrollIntoView

Returns

{ scrollIntoView: (animation?: UseScrollIntoViewAnimation | undefined) => void; cancel: () => void; }: A object with the following elements:

  • scrollIntoView: scroll target element into viewport
  • cancel: cancel scroll function

Arguments

ArgumentDescriptionTypeDefaultValue
targetElementdom elementBasicTarget<HTMLElement> (Required)-
paramsoptional paramsUseScrollIntoViewParams | undefined-
scrollElementscroll containerBasicTarget<HTMLElement>-

UseScrollIntoViewAnimation

PropertyDescriptionTypeDefaultValue
alignmenttarget element alignment relatively to parent based on current axis'start' | 'end' | 'center'-

UseScrollIntoViewParams

PropertyDescriptionTypeDefaultValue
onScrollFinishcallback fired after scroll() => void-
durationduration of scroll in millisecondsnumber1250
axisaxis of scroll'x' | 'y'y
easingcustom mathematical easing function(t: number) => number(t: number) => t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t
offsetadditional distance between nearest edge and elementnumber0
cancelableindicator if animation may be interrupted by user scrollingbooleantrue
isListprevents content jumping in scrolling lists with multiple targetsboolean-

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;