use-scroll-into-view
Scrolls given element into view
Source
Docs
Package
Usage
The use-scroll-into-view hook handles scroll behavior for any scrollable element. Basic usage works the same way as element.scrollIntoView().
The hook adjusts the scrolling animation with respect to the reduced-motion user preference.
Hello there
API
The hook is configured with a settings object:
onScrollFinish– function that will be called after the scroll animationeasing– custom math easing functionduration- duration of the scroll animation in millisecondsaxis- axis of the scrollcancelable- indicator if the animation may be interrupted by user scrollingoffset- additional distance between the nearest edge and the elementisList- indicator that prevents content jumping in scrolling lists with multiple targets, for example Select, Carousel
The hook returns an object with:
scrollIntoView– function that starts the scroll animationcancel– function that stops the scroll animationtargetRef- ref of the target HTML nodescrollableRef- ref of the scrollable parent HTML element; if not used, the document element will be used
The returned scrollIntoView function accepts a single optional argument alignment - optional target element alignment relative to the parent based on the current axis.
Easing
The hook accept custom easing math function to control the flow of animation.
It takes t argument, which is a number between 0 and 1.
Default easing is easeInOutQuad - more info here.
You can find other popular examples on easings.net
Parent node
Scroll me into view
Scroll X axis
Scroll me into view
Definition
Exported types
UseScrollIntoViewOptions and UseScrollIntoViewReturnValue types are exported from the @mantine/hooks package;
you can import them in your application: