@linaria/core
Version:
Blazing fast zero-runtime CSS in JS library
1 lines • 3.63 kB
Source Map (JSON)
{"version":3,"sources":["../src/css.ts","../src/cx.ts"],"sourcesContent":["import type { CSSProperties } from './CSSProperties';\nimport type { LinariaClassName } from './cx';\n\ntype WYWEvalMeta = { __wyw_meta: unknown }; // simplified version of WYWEvalMeta from @wyw-in-js/shared\n\ntype CSS = (\n strings: TemplateStringsArray,\n ...exprs: Array<string | number | CSSProperties | WYWEvalMeta>\n) => LinariaClassName;\n\nlet idx = 0;\n\nconst css: CSS = () => {\n if (process.env.NODE_ENV === 'test') {\n // eslint-disable-next-line no-plusplus\n return `mocked-css-${idx++}` as LinariaClassName;\n }\n\n throw new Error(\n 'Using the \"css\" tag in runtime is not supported. Make sure you have set up the Babel plugin correctly.'\n );\n};\n\nexport default css;\n","export type LinariaClassName = string & { __linariaClassName: true };\n\nexport type ClassName<T = string> = T | false | void | null | 0 | '';\n\ninterface ICX {\n (...classNames: ClassName<LinariaClassName>[]): LinariaClassName;\n (...classNames: ClassName[]): string;\n}\n/**\n * Takes a list of class names and filters for truthy ones, joining them into a single class name for convenience.\n * eg.\n * ```js\n * cx('red', isBig && 'big') // returns 'red big' if `isBig` is true, otherwise returns 'red'\n * ```\n * If space separated atomic styles are provided, they are deduplicated according to the first hashed valued:\n *\n * ```js\n * cx('atm_a_class1 atm_b_class2', 'atm_a_class3') // returns `atm_a_class3 atm_b_class2`\n * ```\n *\n * @returns the combined, space separated class names that can be applied directly to the class attribute\n */\nconst cx: ICX = function cx() {\n const presentClassNames: (ClassName | ClassName<LinariaClassName>)[] =\n Array.prototype.slice\n // eslint-disable-next-line prefer-rest-params\n .call(arguments)\n .filter(Boolean);\n\n const atomicClasses: { [k: string]: string } = {};\n const nonAtomicClasses: string[] = [];\n presentClassNames.forEach((arg) => {\n // className could be the output of a previous cx call, so split by ' ' first\n const individualClassNames = arg ? arg.split(' ') : [];\n\n individualClassNames.forEach((className) => {\n if (className.startsWith('atm_')) {\n const [, keyHash] = className.split('_');\n atomicClasses[keyHash] = className;\n } else {\n nonAtomicClasses.push(className);\n }\n });\n });\n\n const result: string[] = [];\n\n // eslint-disable-next-line no-restricted-syntax\n for (const keyHash in atomicClasses) {\n if (Object.prototype.hasOwnProperty.call(atomicClasses, keyHash)) {\n result.push(atomicClasses[keyHash]);\n }\n }\n\n result.push(...nonAtomicClasses);\n\n return result.join(' ') as LinariaClassName;\n};\n\nexport default cx;\n"],"mappings":";AAUA,IAAI,MAAM;AAEV,IAAM,MAAW,MAAM;AACrB,MAAI,QAAQ,IAAI,aAAa,QAAQ;AAEnC,WAAO,cAAc,KAAK;AAAA,EAC5B;AAEA,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;AAEA,IAAO,cAAQ;;;ACDf,IAAM,KAAU,SAASA,MAAK;AAC5B,QAAM,oBACJ,MAAM,UAAU,MAEb,KAAK,SAAS,EACd,OAAO,OAAO;AAEnB,QAAM,gBAAyC,CAAC;AAChD,QAAM,mBAA6B,CAAC;AACpC,oBAAkB,QAAQ,CAAC,QAAQ;AAEjC,UAAM,uBAAuB,MAAM,IAAI,MAAM,GAAG,IAAI,CAAC;AAErD,yBAAqB,QAAQ,CAAC,cAAc;AAC1C,UAAI,UAAU,WAAW,MAAM,GAAG;AAChC,cAAM,CAAC,EAAE,OAAO,IAAI,UAAU,MAAM,GAAG;AACvC,sBAAc,OAAO,IAAI;AAAA,MAC3B,OAAO;AACL,yBAAiB,KAAK,SAAS;AAAA,MACjC;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AAED,QAAM,SAAmB,CAAC;AAG1B,aAAW,WAAW,eAAe;AACnC,QAAI,OAAO,UAAU,eAAe,KAAK,eAAe,OAAO,GAAG;AAChE,aAAO,KAAK,cAAc,OAAO,CAAC;AAAA,IACpC;AAAA,EACF;AAEA,SAAO,KAAK,GAAG,gBAAgB;AAE/B,SAAO,OAAO,KAAK,GAAG;AACxB;AAEA,IAAO,aAAQ;","names":["cx"]}