@rtdui/hooks
Version:
React hooks library base on @mantine/hooks
41 lines (38 loc) • 1.17 kB
JavaScript
'use client';
import { useWindowScroll, usePrevious, useIsomorphicEffect } from '@mantine/hooks';
const isFixed = (current, fixedAt) => current <= fixedAt;
const isPinned = (current, previous) => current <= previous;
const isReleased = (current, previous, fixedAt) => !isPinned(current, previous) && !isFixed(current, fixedAt);
function useHeadroom2({
fixedAt = 0,
onPin,
onFix,
onRelease
} = {}) {
const [{ y: scrollPosition }] = useWindowScroll();
const previous = usePrevious(scrollPosition);
useIsomorphicEffect(() => {
if (isPinned(scrollPosition, previous)) {
onPin?.();
}
}, [scrollPosition, onPin]);
useIsomorphicEffect(() => {
if (isFixed(scrollPosition, fixedAt)) {
onFix?.();
}
}, [scrollPosition, fixedAt, onFix]);
useIsomorphicEffect(() => {
if (isReleased(scrollPosition, previous, fixedAt)) {
onRelease?.();
}
}, [scrollPosition, onRelease]);
if (isPinned(scrollPosition, previous)) {
return true;
}
if (isFixed(scrollPosition, fixedAt)) {
return true;
}
return false;
}
export { isFixed, isPinned, isReleased, useHeadroom2 };
//# sourceMappingURL=useHeadRoom2.mjs.map