UNPKG

@wordpress/components

Version:
138 lines (106 loc) 4.35 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.contextConnect = contextConnect; exports.contextConnectWithoutRef = contextConnectWithoutRef; exports.getConnectNamespace = getConnectNamespace; exports.hasConnectNamespace = hasConnectNamespace; var _element = require("@wordpress/element"); var _warning = _interopRequireDefault(require("@wordpress/warning")); var _constants = require("./constants"); var _getStyledClassNameFromKey = require("./get-styled-class-name-from-key"); /** * External dependencies */ /** * WordPress dependencies */ /** * Internal dependencies */ /** * 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); } // This is an (experimental) evolution of the initial connect() HOC. // The hope is that we can improve render performance by removing functional // component wrappers. function _contextConnect(Component, namespace, options) { const WrappedComponent = options !== null && options !== void 0 && options.forwardsRef ? (0, _element.forwardRef)(Component) : Component; if (typeof namespace === 'undefined') { typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? (0, _warning.default)('contextConnect: Please provide a namespace') : void 0; } // @ts-expect-error internal property let mergedNamespace = WrappedComponent[_constants.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]; } // @ts-expect-error We can't rely on inferred types here because of the // `as` prop polymorphism we're handling in https://github.com/WordPress/gutenberg/blob/9620bae6fef4fde7cc2b7833f416e240207cda29/packages/components/src/ui/context/wordpress-component.ts#L32-L33 return Object.assign(WrappedComponent, { [_constants.CONNECT_STATIC_NAMESPACE]: [...new Set(mergedNamespace)], displayName: namespace, selector: `.${(0, _getStyledClassNameFromKey.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 = []; // @ts-ignore internal property if (Component[_constants.CONNECT_STATIC_NAMESPACE]) { // @ts-ignore internal property namespaces = Component[_constants.CONNECT_STATIC_NAMESPACE]; } // @ts-ignore if (Component.type && Component.type[_constants.CONNECT_STATIC_NAMESPACE]) { // @ts-ignore namespaces = Component.type[_constants.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; } //# sourceMappingURL=context-connect.js.map