@wordpress/components
Version:
UI components for WordPress.
72 lines (71 loc) • 3.11 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_element = require("@wordpress/element");
let __context_ts = require("./context.cjs");
__context_ts = __toESM(__context_ts);
let react_jsx_runtime = require("react/jsx-runtime");
// packages/components/src/slot-fill/slot.tsx
/**
* Whether the argument is a function.
*
* @param maybeFunc The argument to check.
* @return True if the argument is a function, false otherwise.
*/
function isFunction(maybeFunc) {
return typeof maybeFunc === "function";
}
function addKeysToChildren(children) {
return _wordpress_element.Children.map(children, (child, childIndex) => {
if (!child || typeof child === "string") return child;
let childKey = childIndex;
if (typeof child === "object" && "key" in child && child?.key) childKey = child.key;
return (0, _wordpress_element.cloneElement)(child, { key: childKey });
});
}
function Slot(props) {
const { name, children, fillProps = {} } = props;
const registry = (0, _wordpress_element.useContext)(__context_ts.default);
const instanceRef = (0, _wordpress_element.useRef)({});
(0, _wordpress_element.useLayoutEffect)(() => {
const instance = instanceRef.current;
registry.registerSlot(name, {
type: "children",
instance
});
return () => registry.unregisterSlot(name, instance);
}, [registry, name]);
let fills = (0, _wordpress_compose.useObservableValue)(registry.fills, name) ?? [];
const currentSlot = (0, _wordpress_compose.useObservableValue)(registry.slots, name);
if (!currentSlot || currentSlot.instance !== instanceRef.current) fills = [];
const renderedFills = fills.map((fill) => {
return addKeysToChildren(isFunction(fill.children) ? fill.children(fillProps) : fill.children);
}).filter((element) => !(0, _wordpress_element.isEmptyElement)(element));
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: isFunction(children) ? children(renderedFills) : renderedFills });
}
exports.default = Slot;
//# sourceMappingURL=slot.cjs.map