UNPKG

@wordpress/components

Version:
8 lines (7 loc) 5.28 kB
{ "version": 3, "sources": ["../../src/context/context-connect.ts"], "sourcesContent": ["/**\n * External dependencies\n */\n\n/**\n * WordPress dependencies\n */\nimport { forwardRef } from '@wordpress/element';\nimport warn from '@wordpress/warning';\n\n/**\n * Internal dependencies\n */\nimport { CONNECT_STATIC_NAMESPACE } from './constants';\nimport { getStyledClassNameFromKey } from './get-styled-class-name-from-key';\n/**\n * Forwards ref (React.ForwardRef) and \"Connects\" (or registers) a component\n * within the Context system under a specified namespace.\n *\n * @param Component The component to register into the Context system.\n * @param namespace The namespace to register the component under.\n * @return The connected WordPressComponent\n */\nexport function contextConnect(Component, namespace) {\n return _contextConnect(Component, namespace, {\n forwardsRef: true\n });\n}\n\n/**\n * \"Connects\" (or registers) a component within the Context system under a specified namespace.\n * Does not forward a ref.\n *\n * @param Component The component to register into the Context system.\n * @param namespace The namespace to register the component under.\n * @return The connected WordPressComponent\n */\nexport function contextConnectWithoutRef(Component, namespace) {\n return _contextConnect(Component, namespace);\n}\n\n// This is an (experimental) evolution of the initial connect() HOC.\n// The hope is that we can improve render performance by removing functional\n// component wrappers.\nfunction _contextConnect(Component, namespace, options) {\n const WrappedComponent = options?.forwardsRef ? forwardRef(Component) : Component;\n if (typeof namespace === 'undefined') {\n globalThis.SCRIPT_DEBUG === true ? warn('contextConnect: Please provide a namespace') : void 0;\n }\n\n // @ts-expect-error internal property\n let mergedNamespace = WrappedComponent[CONNECT_STATIC_NAMESPACE] || [namespace];\n\n /**\n * Consolidate (merge) namespaces before attaching it to the WrappedComponent.\n */\n if (Array.isArray(namespace)) {\n mergedNamespace = [...mergedNamespace, ...namespace];\n }\n if (typeof namespace === 'string') {\n mergedNamespace = [...mergedNamespace, namespace];\n }\n\n // @ts-expect-error We can't rely on inferred types here because of the\n // `as` prop polymorphism we're handling in https://github.com/WordPress/gutenberg/blob/4f3a11243c365f94892e479bff0b922ccc4ccda3/packages/components/src/context/wordpress-component.ts#L32-L33\n return Object.assign(WrappedComponent, {\n [CONNECT_STATIC_NAMESPACE]: [...new Set(mergedNamespace)],\n displayName: namespace,\n selector: `.${getStyledClassNameFromKey(namespace)}`\n });\n}\n\n/**\n * Attempts to retrieve the connected namespace from a component.\n *\n * @param Component The component to retrieve a namespace from.\n * @return The connected namespaces.\n */\nexport function getConnectNamespace(Component) {\n if (!Component) {\n return [];\n }\n let namespaces = [];\n\n // @ts-ignore internal property\n if (Component[CONNECT_STATIC_NAMESPACE]) {\n // @ts-ignore internal property\n namespaces = Component[CONNECT_STATIC_NAMESPACE];\n }\n\n // @ts-ignore\n if (Component.type && Component.type[CONNECT_STATIC_NAMESPACE]) {\n // @ts-ignore\n namespaces = Component.type[CONNECT_STATIC_NAMESPACE];\n }\n return namespaces;\n}\n\n/**\n * Checks to see if a component is connected within the Context system.\n *\n * @param Component The component to retrieve a namespace from.\n * @param match The namespace to check.\n */\nexport function hasConnectNamespace(Component, match) {\n if (!Component) {\n return false;\n }\n if (typeof match === 'string') {\n return getConnectNamespace(Component).includes(match);\n }\n if (Array.isArray(match)) {\n return match.some(result => getConnectNamespace(Component).includes(result));\n }\n return false;\n}"], "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAOA,qBAA2B;AAC3B,qBAAiB;AAKjB,uBAAyC;AACzC,4CAA0C;AASnC,SAAS,eAAe,WAAW,WAAW;AACnD,SAAO,gBAAgB,WAAW,WAAW;AAAA,IAC3C,aAAa;AAAA,EACf,CAAC;AACH;AAUO,SAAS,yBAAyB,WAAW,WAAW;AAC7D,SAAO,gBAAgB,WAAW,SAAS;AAC7C;AAKA,SAAS,gBAAgB,WAAW,WAAW,SAAS;AACtD,QAAM,mBAAmB,SAAS,kBAAc,2BAAW,SAAS,IAAI;AACxE,MAAI,OAAO,cAAc,aAAa;AACpC,eAAW,iBAAiB,WAAO,eAAAA,SAAK,4CAA4C,IAAI;AAAA,EAC1F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