(this: MacroContext | void, content: string, layer = '_.a')
| 1101 | * @returns The generated class name that applies the styles. |
| 1102 | */ |
| 1103 | export function css(this: MacroContext | void, content: string, layer = '_.a'): string { |
| 1104 | // Check if `this` is undefined, which means style was not called as a macro but as a normal function. |
| 1105 | // We also check if this is globalThis, which happens in non-strict mode bundles. |
| 1106 | // Also allow style to be called as a normal function in tests. |
| 1107 | // @ts-ignore |
| 1108 | |
| 1109 | if ((this == null || this === globalThis) && process.env.NODE_ENV !== 'test') { |
| 1110 | throw new Error('The css macro must be imported with {type: "macro"}.'); |
| 1111 | } |
| 1112 | let className = generateArbitraryValueSelector(content, true); |
| 1113 | content = `@layer ${layer} { |
| 1114 | .${className} { |
| 1115 | ${content} |
| 1116 | } |
| 1117 | }`; |
| 1118 | |
| 1119 | // Ensure layer is always declared after the _ layer used by style macro. |
| 1120 | if (!layer.startsWith('_.')) { |
| 1121 | content = `@layer _, ${layer};\n` + content; |
| 1122 | } |
| 1123 | |
| 1124 | if (this && typeof this.addAsset === 'function') { |
| 1125 | this.addAsset({ |
| 1126 | type: 'css', |
| 1127 | content |
| 1128 | }); |
| 1129 | } |
| 1130 | return className; |
| 1131 | } |
| 1132 | |
| 1133 | export function keyframes(this: MacroContext | void, css: string): string { |
| 1134 | // Check if `this` is undefined, which means style was not called as a macro but as a normal function. |
no test coverage detected