@wordpress/components
Version:
UI components for WordPress.
8 lines (7 loc) • 2.24 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/utils/hooks/use-cx.ts"],
"sourcesContent": ["/**\n * External dependencies\n */\nimport { __unsafe_useEmotionCache as useEmotionCache } from '@emotion/react';\nimport { insertStyles } from '@emotion/utils';\n// eslint-disable-next-line no-restricted-imports\nimport { cx as innerCx } from '@emotion/css';\n\n/**\n * WordPress dependencies\n */\nimport { useCallback } from '@wordpress/element';\nconst isSerializedStyles = o => typeof o !== 'undefined' && o !== null && ['name', 'styles'].every(p => typeof o[p] !== 'undefined');\n\n/**\n * Retrieve a `cx` function that knows how to handle `SerializedStyles`\n * returned by the `@emotion/react` `css` function in addition to what\n * `cx` normally knows how to handle. It also hooks into the Emotion\n * Cache, allowing `css` calls to work inside iframes.\n *\n * ```jsx\n * import { css } from '@emotion/react';\n *\n * const styles = css`\n * \tcolor: red\n * `;\n *\n * function RedText( { className, ...props } ) {\n * \tconst cx = useCx();\n *\n * \tconst classes = cx(styles, className);\n *\n * \treturn <span className={classes} {...props} />;\n * }\n * ```\n */\nexport const useCx = () => {\n const cache = useEmotionCache();\n const cx = useCallback((...classNames) => {\n if (cache === null) {\n throw new Error('The `useCx` hook should be only used within a valid Emotion Cache Context');\n }\n return innerCx(...classNames.map(arg => {\n if (isSerializedStyles(arg)) {\n insertStyles(cache, arg, false);\n return `${cache.key}-${arg.name}`;\n }\n return arg;\n }));\n }, [cache]);\n return cx;\n};"],
"mappings": ";AAGA,SAAS,4BAA4B,uBAAuB;AAC5D,SAAS,oBAAoB;AAE7B,SAAS,MAAM,eAAe;AAK9B,SAAS,mBAAmB;AAC5B,IAAM,qBAAqB,OAAK,OAAO,MAAM,eAAe,MAAM,QAAQ,CAAC,QAAQ,QAAQ,EAAE,MAAM,OAAK,OAAO,EAAE,CAAC,MAAM,WAAW;AAwB5H,IAAM,QAAQ,MAAM;AACzB,QAAM,QAAQ,gBAAgB;AAC9B,QAAM,KAAK,YAAY,IAAI,eAAe;AACxC,QAAI,UAAU,MAAM;AAClB,YAAM,IAAI,MAAM,2EAA2E;AAAA,IAC7F;AACA,WAAO,QAAQ,GAAG,WAAW,IAAI,SAAO;AACtC,UAAI,mBAAmB,GAAG,GAAG;AAC3B,qBAAa,OAAO,KAAK,KAAK;AAC9B,eAAO,GAAG,MAAM,GAAG,IAAI,IAAI,IAAI;AAAA,MACjC;AACA,aAAO;AAAA,IACT,CAAC,CAAC;AAAA,EACJ,GAAG,CAAC,KAAK,CAAC;AACV,SAAO;AACT;",
"names": []
}