umami/components/helpers/StickyHeader.js

24 lines
637 B
JavaScript
Raw Normal View History

2023-02-09 17:22:36 +01:00
import { useEffect, useRef } from 'react';
import classNames from 'classnames';
2023-02-09 08:14:11 +01:00
import useSticky from 'hooks/useSticky';
2023-02-09 08:14:11 +01:00
export default function StickyHeader({ className, stickyClassName, stickyStyle, children }) {
const { ref, isSticky } = useSticky();
2023-02-09 17:22:36 +01:00
const initialWidth = useRef(0);
useEffect(() => {
initialWidth.current = ref.current.clientWidth;
}, [ref]);
return (
<div
ref={ref}
2023-02-09 08:14:11 +01:00
data-sticky={isSticky}
className={classNames(className, { [stickyClassName]: isSticky })}
2023-02-09 17:22:36 +01:00
style={isSticky ? { ...stickyStyle, width: initialWidth.current } : null}
>
{children}
</div>
);
}