import React, { FC, useLayoutEffect, useRef } from 'react'; import { useGlobalState } from './state'; interface FixedElementProps { children: React.ReactNode; top?: number; right?: number; left?: number; } export const FixedElement: FC = ({ children, top = 0, left, right, ...props }) => { const fixedElement = useRef(null); const [smoothScrollBar] = useGlobalState('smoothScrollBar'); useLayoutEffect(() => { if (smoothScrollBar) smoothScrollBar.addListener(({ offset }) => { if (fixedElement.current) { fixedElement.current.style.top = top + offset.y + 'px'; if (right !== undefined) fixedElement.current.style.right = right + offset.x + 'px'; if (left !== undefined) fixedElement.current.style.left = left + offset.x + 'px'; } }); return () => {}; }, [left, right, smoothScrollBar, top]); return (
{children}
); };