2023-03-03 21:37:26 +01:00
|
|
|
import { useMeasure } 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';
|
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,
|
2023-03-03 21:37:26 +01:00
|
|
|
scrollElement,
|
2023-02-10 12:26:57 +01:00
|
|
|
children,
|
|
|
|
}) {
|
2023-03-03 21:37:26 +01:00
|
|
|
const { ref: scrollRef, isSticky } = useSticky({ scrollElement });
|
2023-02-10 12:26:57 +01:00
|
|
|
const { ref: measureRef, dimensions } = useMeasure();
|
2023-03-09 01:37:43 +01:00
|
|
|
const active = enabled && isSticky;
|
2020-08-04 08:20:35 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2023-02-10 12:26:57 +01:00
|
|
|
ref={measureRef}
|
2023-03-09 01:37:43 +01:00
|
|
|
data-sticky={active}
|
|
|
|
style={active ? { height: dimensions.height } : null}
|
2020-08-04 08:20:35 +02:00
|
|
|
>
|
2023-02-10 12:26:57 +01:00
|
|
|
<div
|
|
|
|
ref={scrollRef}
|
2023-03-09 01:37:43 +01:00
|
|
|
className={classNames(className, { [stickyClassName]: active })}
|
|
|
|
style={active ? { ...stickyStyle, width: dimensions.width } : null}
|
2023-02-10 12:26:57 +01:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
2020-08-04 08:20:35 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|