UNPKG

@wordpress/components

Version:
70 lines (69 loc) • 3.02 kB
import { forwardRef } from "@wordpress/element"; import warn from "@wordpress/warning"; import { CONNECT_STATIC_NAMESPACE } from "./constants.mjs"; import { getStyledClassNameFromKey } from "./get-styled-class-name-from-key.mjs"; // 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 ? forwardRef(Component) : Component; if (typeof namespace === "undefined") warn("contextConnect: Please provide a namespace"); let mergedNamespace = WrappedComponent[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, { [CONNECT_STATIC_NAMESPACE]: [...new Set(mergedNamespace)], displayName: namespace, selector: `.${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[CONNECT_STATIC_NAMESPACE]) namespaces = Component[CONNECT_STATIC_NAMESPACE]; if (Component.type && Component.type[CONNECT_STATIC_NAMESPACE]) namespaces = Component.type[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; } export { contextConnect, contextConnectWithoutRef, getConnectNamespace, hasConnectNamespace }; //# sourceMappingURL=context-connect.mjs.map