umami/components/helpers/StickyHeader.js

32 lines
816 B
JavaScript
Raw Normal View History

import { useMeasure } 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
export default function StickyHeader({
className,
stickyClassName,
stickyStyle,
enabled = true,
scrollElement,
2023-02-10 12:26:57 +01:00
children,
}) {
const { ref: scrollRef, isSticky } = useSticky({ scrollElement });
2023-02-10 12:26:57 +01:00
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>
);
}