@wordpress/components
Version:
UI components for WordPress.
83 lines (82 loc) • 2.09 kB
JavaScript
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import SlotFillContext from './context';
import { observableMap } from '@wordpress/compose';
import { jsx as _jsx } from "react/jsx-runtime";
function createSlotRegistry() {
const slots = observableMap();
const fills = observableMap();
function registerSlot(name, instance) {
slots.set(name, instance);
}
function unregisterSlot(name, instance) {
// If a previous instance of a Slot by this name unmounts, do nothing,
// as the slot and its fills should only be removed for the current
// known instance.
if (slots.get(name) !== instance) {
return;
}
slots.delete(name);
}
function registerFill(name, instance, children) {
fills.set(name, [...(fills.get(name) || []), {
instance,
children
}]);
}
function unregisterFill(name, instance) {
const fillsForName = fills.get(name);
if (!fillsForName) {
return;
}
fills.set(name, fillsForName.filter(fill => fill.instance !== instance));
}
function updateFill(name, instance, children) {
const fillsForName = fills.get(name);
if (!fillsForName) {
return;
}
const fillForInstance = fillsForName.find(f => f.instance === instance);
if (!fillForInstance) {
return;
}
if (fillForInstance.children === children) {
return;
}
fills.set(name, fillsForName.map(f => {
if (f.instance === instance) {
// Replace with new record with updated `children`.
return {
instance,
children
};
}
return f;
}));
}
return {
slots,
fills,
registerSlot,
unregisterSlot,
registerFill,
unregisterFill,
updateFill
};
}
export function SlotFillProvider({
children
}) {
const [contextValue] = useState(createSlotRegistry);
return /*#__PURE__*/_jsx(SlotFillContext.Provider, {
value: contextValue,
children: children
});
}
export default SlotFillProvider;
//# sourceMappingURL=provider.js.map