K

use-window-scroll

Subscribe to window scroll and scroll smoothly to given position
Import

Usage

Hook returns current scroll position and a function to scroll smoothly to given position:

Scroll position x: 0, y: 0
import { useWindowScroll } from '@mantine/hooks';
import { Button, Text, Group } from '@mantine/core';
function Demo() {
const [scroll, scrollTo] = useWindowScroll();
return (
<Group position="center">
<Text>
Scroll position x: {scroll.x}, y: {scroll.y}
</Text>
<Button onClick={() => scrollTo({ y: 0 })}>Scroll to top</Button>
</Group>
);
}

Definition

function useWindowScroll(): readonly [
{ x: number; y: number },
(position: { x?: number; y?: number }) => void
];
Build fully functional accessible web applications with ease
Feedback
Your feedback is most valuable contribution to the project, please share how you use Mantine, what features are missing and what is done good
Leave feedback