@wordpress/components
Version:
UI components for WordPress.
45 lines (44 loc) • 1.44 kB
JavaScript
import { useObservableValue } from "@wordpress/compose";
import { createPortal, useContext, useLayoutEffect, useRef } from "@wordpress/element";
import SlotFillContext from "./context.mjs";
import StyleProvider from "../style-provider/index.mjs";
import { jsx } from "react/jsx-runtime";
// packages/components/src/slot-fill/fill.tsx
function Fill({ name, children }) {
const registry = useContext(SlotFillContext);
const instanceRef = useRef({});
const childrenRef = useRef(children);
useLayoutEffect(() => {
childrenRef.current = children;
}, [children]);
useLayoutEffect(() => {
const instance = instanceRef.current;
registry.registerFill(name, {
instance,
children: childrenRef.current
});
return () => registry.unregisterFill(name, instance);
}, [registry, name]);
useLayoutEffect(() => {
registry.updateFill(name, {
instance: instanceRef.current,
children
});
}, [
registry,
name,
children
]);
const slot = useObservableValue(registry.slots, name);
if (!slot) return null;
if (slot.type === "children") return null;
const portalEl = slot.ref.current;
if (!portalEl) return null;
const wrappedChildren = typeof children === "function" ? children(slot.fillProps ?? {}) : children;
return createPortal(/* @__PURE__ */ jsx(StyleProvider, {
document: portalEl.ownerDocument,
children: wrappedChildren
}), portalEl);
}
export { Fill as default };
//# sourceMappingURL=fill.mjs.map