@wordpress/components
Version:
UI components for WordPress.
92 lines (89 loc) • 2.41 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SlotFillProvider = SlotFillProvider;
exports.default = void 0;
var _element = require("@wordpress/element");
var _context = _interopRequireDefault(require("./context"));
var _compose = require("@wordpress/compose");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
/**
* Internal dependencies
*/
function createSlotRegistry() {
const slots = (0, _compose.observableMap)();
const fills = (0, _compose.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
};
}
function SlotFillProvider({
children
}) {
const [contextValue] = (0, _element.useState)(createSlotRegistry);
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_context.default.Provider, {
value: contextValue,
children: children
});
}
var _default = exports.default = SlotFillProvider;
//# sourceMappingURL=provider.js.map