UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

26 lines 1.06 kB
import * as React from 'react'; import { createPortal } from 'react-dom'; import { context, usePortal } from 'nice-ui/lib/utils/portal/context'; import { PortalState } from 'nice-ui/lib/utils/portal/PortalState'; export const Portal = ({ children, parent }) => { const parentState = usePortal(); // biome-ignore lint: hook dependency list manually managed const state = React.useMemo(() => { const state = new PortalState(); state.parent = parentState; return state; }, [parent]); const [el] = React.useState(() => document.createElement('div')); // biome-ignore lint: hook dependency list manually managed React.useLayoutEffect(() => { const container = parent || document.body; container.appendChild(el); state.addRoot(el); return () => { state.delRoot(el); container.removeChild(el); }; }, [parent]); return React.createElement(context.Provider, { value: state }, createPortal(children, el)); }; //# sourceMappingURL=Portal.js.map