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';
|
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';
|
2023-02-10 12:26:57 +01:00
|
|
|
import { UI_LAYOUT_BODY } from 'lib/constants';
|
2020-08-04 08:20:35 +02:00
|
|
|
|
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();
|
2020-08-04 08:20:35 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2023-02-10 12:26:57 +01:00
|
|
|
ref={measureRef}
|
|
|
|
data-sticky={enabled && isSticky}
|
|
|
|
style={enabled && isSticky ? { height: dimensions.height } : null}
|
2020-08-04 08:20:35 +02:00
|
|
|
>
|
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>
|
2020-08-04 08:20:35 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|