Updated sticky header logic.

This commit is contained in:
Mike Cao 2023-02-09 08:22:36 -08:00
parent 45c13da262
commit f062cdbed2
8 changed files with 31 additions and 24 deletions

View file

@ -1,15 +1,21 @@
import { useEffect, useRef } from 'react';
import classNames from 'classnames';
import useSticky from 'hooks/useSticky';
export default function StickyHeader({ className, stickyClassName, stickyStyle, children }) {
const { ref, isSticky } = useSticky();
const initialWidth = useRef(0);
useEffect(() => {
initialWidth.current = ref.current.clientWidth;
}, [ref]);
return (
<div
ref={ref}
data-sticky={isSticky}
className={classNames(className, { [stickyClassName]: isSticky })}
style={isSticky ? { ...stickyStyle, width: ref?.current?.clientWidth } : null}
style={isSticky ? { ...stickyStyle, width: initialWidth.current } : null}
>
{children}
</div>