2023-02-09 17:22:36 +01:00
|
|
|
import { useEffect, useRef } from 'react';
|
2020-08-04 08:20:35 +02:00
|
|
|
import classNames from 'classnames';
|
2023-02-09 08:14:11 +01:00
|
|
|
import useSticky from 'hooks/useSticky';
|
2020-08-04 08:20:35 +02:00
|
|
|
|
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]);
|
2020-08-04 08:20:35 +02:00
|
|
|
|
|
|
|
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}
|
2020-08-04 08:20:35 +02:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|