import React, { ReactNode, useEffect, useRef, useState } from 'react'; import { useToolbarCollectionContext } from '../ToolbarCollectionContext'; import { observer } from 'mobx-react-lite'; import { ScrollableContentContext, useScrollableContentContext, } from '../../providers'; export interface ToolbarCollectionContentAutoSizerProps { children: ReactNode; } function _ToolbarCollectionContentAutoSizer( props: ToolbarCollectionContentAutoSizerProps, ) { const { children } = props; const { scrollableContentRef } = useScrollableContentContext(); const internalScrollableContentRef = useRef(null); const [scrollableContentContextValue] = useState(() => ({ scrollableContentRef: internalScrollableContentRef, })); const toolbar = useToolbarCollectionContext(); const { internalScroll, collection } = toolbar; useEffect(() => { return toolbar.itemsContentRect.init({ scrollElement: scrollableContentRef.current, }); }, []); let child = (
{ toolbar.itemsContentRect.element = el; if (internalScroll) { internalScrollableContentRef.current = el; collection.scrollableContent = el; } }} > {children}
); if (internalScroll) { child = ( {child} ); } return child; } export const ToolbarCollectionContentAutoSizer = observer( _ToolbarCollectionContentAutoSizer, );