umami/components/helpers/StickyHeader.js

33 lines
925 B
JavaScript
Raw Normal View History

2023-02-09 17:22:36 +01:00
import { useEffect, useRef } from 'react';
2023-02-10 12:26:57 +01:00
import { useMeasure, useCombinedRefs } from 'react-basics';
import classNames from 'classnames';
2023-02-09 08:14:11 +01:00
import useSticky from 'hooks/useSticky';
2023-02-10 12:26:57 +01:00
import { UI_LAYOUT_BODY } from 'lib/constants';
2023-02-10 12:26:57 +01:00
export default function StickyHeader({
className,
stickyClassName,
stickyStyle,
enabled = true,
children,
}) {
const { ref: scrollRef, isSticky } = useSticky({ scrollElementId: UI_LAYOUT_BODY });
const { ref: measureRef, dimensions } = useMeasure();
return (
<div
2023-02-10 12:26:57 +01:00
ref={measureRef}
data-sticky={enabled && isSticky}
style={enabled && isSticky ? { height: dimensions.height } : null}
>
2023-02-10 12:26:57 +01:00
<div
ref={scrollRef}
className={classNames(className, { [stickyClassName]: enabled && isSticky })}
style={enabled && isSticky ? { ...stickyStyle, width: dimensions.width } : null}
>
{children}
</div>
</div>
);
}