@wordpress/components
Version:
UI components for WordPress.
100 lines (99 loc) • 4.47 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_element = require("@wordpress/element");
let _wordpress_warning = require("@wordpress/warning");
_wordpress_warning = __toESM(_wordpress_warning);
let __constants_ts = require("./constants.cjs");
let __get_styled_class_name_from_key_ts = require("./get-styled-class-name-from-key.cjs");
// packages/components/src/context/context-connect.ts
/**
* Forwards ref (React.ForwardRef) and "Connects" (or registers) a component
* within the Context system under a specified namespace.
*
* @param Component The component to register into the Context system.
* @param namespace The namespace to register the component under.
* @return The connected WordPressComponent
*/
function contextConnect(Component, namespace) {
return _contextConnect(Component, namespace, { forwardsRef: true });
}
/**
* "Connects" (or registers) a component within the Context system under a specified namespace.
* Does not forward a ref.
*
* @param Component The component to register into the Context system.
* @param namespace The namespace to register the component under.
* @return The connected WordPressComponent
*/
function contextConnectWithoutRef(Component, namespace) {
return _contextConnect(Component, namespace);
}
function _contextConnect(Component, namespace, options) {
const WrappedComponent = options?.forwardsRef ? (0, _wordpress_element.forwardRef)(Component) : Component;
if (typeof namespace === "undefined") (0, _wordpress_warning.default)("contextConnect: Please provide a namespace");
let mergedNamespace = WrappedComponent[__constants_ts.CONNECT_STATIC_NAMESPACE] || [namespace];
/**
* Consolidate (merge) namespaces before attaching it to the WrappedComponent.
*/
if (Array.isArray(namespace)) mergedNamespace = [...mergedNamespace, ...namespace];
if (typeof namespace === "string") mergedNamespace = [...mergedNamespace, namespace];
return Object.assign(WrappedComponent, {
[__constants_ts.CONNECT_STATIC_NAMESPACE]: [...new Set(mergedNamespace)],
displayName: namespace,
selector: `.${(0, __get_styled_class_name_from_key_ts.getStyledClassNameFromKey)(namespace)}`
});
}
/**
* Attempts to retrieve the connected namespace from a component.
*
* @param Component The component to retrieve a namespace from.
* @return The connected namespaces.
*/
function getConnectNamespace(Component) {
if (!Component) return [];
let namespaces = [];
if (Component[__constants_ts.CONNECT_STATIC_NAMESPACE]) namespaces = Component[__constants_ts.CONNECT_STATIC_NAMESPACE];
if (Component.type && Component.type[__constants_ts.CONNECT_STATIC_NAMESPACE]) namespaces = Component.type[__constants_ts.CONNECT_STATIC_NAMESPACE];
return namespaces;
}
/**
* Checks to see if a component is connected within the Context system.
*
* @param Component The component to retrieve a namespace from.
* @param match The namespace to check.
*/
function hasConnectNamespace(Component, match) {
if (!Component) return false;
if (typeof match === "string") return getConnectNamespace(Component).includes(match);
if (Array.isArray(match)) return match.some((result) => getConnectNamespace(Component).includes(result));
return false;
}
exports.contextConnect = contextConnect;
exports.contextConnectWithoutRef = contextConnectWithoutRef;
exports.getConnectNamespace = getConnectNamespace;
exports.hasConnectNamespace = hasConnectNamespace;
//# sourceMappingURL=context-connect.cjs.map