MCPcopy Create free account
hub / github.com/adobe/react-spectrum / createTheme

Function createTheme

packages/@react-spectrum/s2/style/style-macro.ts:286–777  ·  view source on GitHub ↗
(
  theme: T
)

Source from the content-addressed store, hash-verified

284let isCompilingDependencies: boolean | null | string = false;
285
286export 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 {

Callers 1

spectrum-theme.tsFile · 0.90

Calls 15

getStaticClassNameMethod · 0.95
toJSMethod · 0.95
toCSSMethod · 0.95
mapConditionalShorthandFunction · 0.85
shortCSSPropertyNameFunction · 0.85
layerNameFunction · 0.85
classNamePrefixFunction · 0.85
toBase62Function · 0.85
setRulesFunction · 0.70
compileValueFunction · 0.70
generateNameFunction · 0.70
hashFunction · 0.70

Tested by

no test coverage detected