UNPKG

@wordpress/components

Version:
75 lines (74 loc) 1.76 kB
// packages/components/src/slot-fill/bubbles-virtually/slot-fill-provider.tsx import { useState } from "@wordpress/element"; import isShallowEqual from "@wordpress/is-shallow-equal"; import { observableMap } from "@wordpress/compose"; import SlotFillContext from "./slot-fill-context"; import { jsx as _jsx } from "react/jsx-runtime"; function createSlotRegistry() { const slots = observableMap(); const fills = observableMap(); const registerSlot = (name, ref, fillProps) => { slots.set(name, { ref, fillProps }); }; const unregisterSlot = (name, ref) => { const slot = slots.get(name); if (!slot) { return; } if (slot.ref !== ref) { return; } slots.delete(name); }; const updateSlot = (name, ref, fillProps) => { const slot = slots.get(name); if (!slot) { return; } if (slot.ref !== ref) { return; } if (isShallowEqual(slot.fillProps, fillProps)) { return; } slots.set(name, { ref, fillProps }); }; const registerFill = (name, ref) => { fills.set(name, [...fills.get(name) || [], ref]); }; const unregisterFill = (name, ref) => { const fillsForName = fills.get(name); if (!fillsForName) { return; } fills.set(name, fillsForName.filter((fillRef) => fillRef !== ref)); }; return { slots, fills, registerSlot, updateSlot, unregisterSlot, registerFill, unregisterFill }; } function SlotFillProvider({ children }) { const [registry] = useState(createSlotRegistry); return /* @__PURE__ */ _jsx(SlotFillContext.Provider, { value: registry, children }); } export { SlotFillProvider as default }; //# sourceMappingURL=slot-fill-provider.js.map