@wordpress/components
Version:
UI components for WordPress.
8 lines (7 loc) • 3.54 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/context/use-context-system.js"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport warn from '@wordpress/warning';\n\n/**\n * Internal dependencies\n */\nimport { useComponentsContext } from './context-system-provider';\nimport { getNamespace, getConnectedNamespace } from './utils';\nimport { getStyledClassNameFromKey } from './get-styled-class-name-from-key';\nimport { useCx } from '../utils/hooks/use-cx';\n\n/**\n * @template TProps\n * @typedef {TProps & { className: string }} ConnectedProps\n */\n\n/**\n * Custom hook that derives registered props from the Context system.\n * These derived props are then consolidated with incoming component props.\n *\n * @template {{ className?: string }} P\n * @param {P} props Incoming props from the component.\n * @param {string} namespace The namespace to register and to derive context props from.\n * @return {ConnectedProps<P>} The connected props.\n */\nexport function useContextSystem(props, namespace) {\n const contextSystemProps = useComponentsContext();\n if (typeof namespace === 'undefined') {\n globalThis.SCRIPT_DEBUG === true ? warn('useContextSystem: Please provide a namespace') : void 0;\n }\n const contextProps = contextSystemProps?.[namespace] || {};\n\n /** @type {ConnectedProps<P>} */\n // @ts-ignore We fill in the missing properties below\n const finalComponentProps = {\n ...getConnectedNamespace(),\n ...getNamespace(namespace)\n };\n const {\n _overrides: overrideProps,\n ...otherContextProps\n } = contextProps;\n const initialMergedProps = Object.entries(otherContextProps).length ? Object.assign({}, otherContextProps, props) : props;\n const cx = useCx();\n const classes = cx(getStyledClassNameFromKey(namespace), props.className);\n\n // Provides the ability to customize the render of the component.\n const rendered = typeof initialMergedProps.renderChildren === 'function' ? initialMergedProps.renderChildren(initialMergedProps) : initialMergedProps.children;\n for (const key in initialMergedProps) {\n // @ts-ignore filling in missing props\n finalComponentProps[key] = initialMergedProps[key];\n }\n for (const key in overrideProps) {\n // @ts-ignore filling in missing props\n finalComponentProps[key] = overrideProps[key];\n }\n\n // Setting an `undefined` explicitly can cause unintended overwrites\n // when a `cloneElement()` is involved.\n if (rendered !== undefined) {\n // @ts-ignore\n finalComponentProps.children = rendered;\n }\n finalComponentProps.className = classes;\n return finalComponentProps;\n}"],
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,qBAAiB;AAKjB,qCAAqC;AACrC,mBAAoD;AACpD,4CAA0C;AAC1C,oBAAsB;AAgBf,SAAS,iBAAiB,OAAO,WAAW;AACjD,QAAM,yBAAqB,qDAAqB;AAChD,MAAI,OAAO,cAAc,aAAa;AACpC,eAAW,iBAAiB,WAAO,eAAAA,SAAK,8CAA8C,IAAI;AAAA,EAC5F;AACA,QAAM,eAAe,qBAAqB,SAAS,KAAK,CAAC;AAIzD,QAAM,sBAAsB;AAAA,IAC1B,OAAG,oCAAsB;AAAA,IACzB,OAAG,2BAAa,SAAS;AAAA,EAC3B;AACA,QAAM;AAAA,IACJ,YAAY;AAAA,IACZ,GAAG;AAAA,EACL,IAAI;AACJ,QAAM,qBAAqB,OAAO,QAAQ,iBAAiB,EAAE,SAAS,OAAO,OAAO,CAAC,GAAG,mBAAmB,KAAK,IAAI;AACpH,QAAM,SAAK,qBAAM;AACjB,QAAM,UAAU,OAAG,iEAA0B,SAAS,GAAG,MAAM,SAAS;AAGxE,QAAM,WAAW,OAAO,mBAAmB,mBAAmB,aAAa,mBAAmB,eAAe,kBAAkB,IAAI,mBAAmB;AACtJ,aAAW,OAAO,oBAAoB;AAEpC,wBAAoB,GAAG,IAAI,mBAAmB,GAAG;AAAA,EACnD;AACA,aAAW,OAAO,eAAe;AAE/B,wBAAoB,GAAG,IAAI,cAAc,GAAG;AAAA,EAC9C;AAIA,MAAI,aAAa,QAAW;AAE1B,wBAAoB,WAAW;AAAA,EACjC;AACA,sBAAoB,YAAY;AAChC,SAAO;AACT;",
"names": ["warn"]
}