nextui/apps/docs/hooks/use-scroll-position.ts
WK 8c2613713a
refactor: migrate eslint to v9 (#5267)
* refactor: migrate eslint to v9

* chore: lint

* chore: update eslint command

* chore: fix lint warnings

* chore: separate lint and lint:fix

* chore: exclude contentlayer generated code

* fix(scripts): add missing await
2025-06-01 13:51:30 -03:00

21 lines
519 B
TypeScript

import * as React from "react";
export function useScrollPosition(ref: React.MutableRefObject<HTMLElement | null>) {
const [scrollPosition, setScrollPosition] = React.useState(0);
React.useEffect(() => {
const handleScroll = () => {
setScrollPosition(ref.current?.scrollTop || 0);
};
ref.current?.addEventListener("scroll", handleScroll);
// Cleanup on unmount
return () => {
ref.current?.removeEventListener("scroll", handleScroll);
};
}, []);
return scrollPosition;
}