UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

37 lines (36 loc) 1.02 kB
"use client"; import { useWindowEvent } from "../use-window-event/use-window-event.mjs"; import { useEffect, useState } from "react"; //#region packages/@mantine/hooks/src/use-window-scroll/use-window-scroll.ts function getScrollPosition() { return typeof window !== "undefined" ? { x: window.scrollX, y: window.scrollY } : { x: 0, y: 0 }; } function scrollTo({ x, y }) { if (typeof window !== "undefined") { const scrollOptions = { behavior: "smooth" }; if (typeof x === "number") scrollOptions.left = x; if (typeof y === "number") scrollOptions.top = y; window.scrollTo(scrollOptions); } } function useWindowScroll() { const [position, setPosition] = useState({ x: 0, y: 0 }); useWindowEvent("scroll", () => setPosition(getScrollPosition())); useWindowEvent("resize", () => setPosition(getScrollPosition())); useEffect(() => { setPosition(getScrollPosition()); }, []); return [position, scrollTo]; } //#endregion export { useWindowScroll }; //# sourceMappingURL=use-window-scroll.mjs.map