( theme: T )
| 284 | let isCompilingDependencies: boolean | null | string = false; |
| 285 | |
| 286 | export function createTheme<T extends Theme>( |
| 287 | theme: T |
| 288 | ): StyleFunction<ThemeProperties<T>, 'default' | Extract<keyof T['conditions'], string>> { |
| 289 | let properties = new Map<string, Property<any>>( |
| 290 | Object.entries(theme.properties).map(([k, v]) => { |
| 291 | if (!Array.isArray(v) && v.cssProperties) { |
| 292 | return [k, v as Property<any>]; |
| 293 | } |
| 294 | |
| 295 | return [k, new MappedProperty(k, v as any)]; |
| 296 | }) |
| 297 | ); |
| 298 | |
| 299 | let dependencies = new Set<string>(); |
| 300 | let hasConditions = false; |
| 301 | return function style(this: MacroContext | void, style, allowedOverrides?: readonly string[]) { |
| 302 | // Check if `this` is undefined, which means style was not called as a macro but as a normal function. |
| 303 | // We also check if this is globalThis, which happens in non-strict mode bundles. |
| 304 | // Also allow style to be called as a normal function in tests. |
| 305 | // @ts-ignore |
| 306 | |
| 307 | if ((this == null || this === globalThis) && process.env.NODE_ENV !== 'test') { |
| 308 | throw new Error('The style macro must be imported with {type: "macro"}.'); |
| 309 | } |
| 310 | |
| 311 | // Generate rules for each property. |
| 312 | let rules = new Map<string, Rule>(); |
| 313 | let values = new Map(); |
| 314 | dependencies.clear(); |
| 315 | let usedPriorities = 0; |
| 316 | let setRules = (key: string, value: [number, Rule[]]) => { |
| 317 | usedPriorities = Math.max(usedPriorities, value[0]); |
| 318 | rules.set(key, new GroupRule(value[1])); |
| 319 | }; |
| 320 | |
| 321 | hasConditions = false; |
| 322 | for (let key in style) { |
| 323 | let value = style[key]!; |
| 324 | let themeProperty = key; |
| 325 | values.set(key, value); |
| 326 | |
| 327 | // Get the type of custom properties in the theme. |
| 328 | if (key.startsWith('--')) { |
| 329 | themeProperty = value.type; |
| 330 | value = value.value; |
| 331 | } |
| 332 | |
| 333 | // Expand shorthands to longhands so that merging works as expected. |
| 334 | if (theme.shorthands[key]) { |
| 335 | let shorthand = theme.shorthands[key]; |
| 336 | if (typeof shorthand === 'function') { |
| 337 | let expanded = mapConditionalShorthand(value, shorthand); |
| 338 | for (let k in expanded) { |
| 339 | let v = expanded[k]; |
| 340 | values.set(k, v); |
| 341 | setRules(k, compileValue(k, k, v)); |
| 342 | } |
| 343 | } else { |
no test coverage detected