react-mcu 1.3.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -22,13 +22,6 @@ import {
22
22
  TonalPalette
23
23
  } from "@material/material-color-utilities";
24
24
  import { kebabCase, startCase, upperFirst } from "lodash-es";
25
- function adjustToneForContrast(baseTone, contrastLevel, adjustmentFactor = DEFAULT_CONTRAST_ADJUSTMENT_FACTOR) {
26
- if (contrastLevel === 0) return baseTone;
27
- const distanceToCenter = baseTone - 50;
28
- const delta = distanceToCenter * contrastLevel * adjustmentFactor;
29
- const adjustedTone = baseTone + delta;
30
- return Math.max(0, Math.min(100, adjustedTone));
31
- }
32
25
  var schemesMap = {
33
26
  tonalSpot: SchemeTonalSpot,
34
27
  monochrome: SchemeMonochrome,
@@ -41,11 +34,39 @@ var schemesMap = {
41
34
  var DEFAULT_SCHEME = "tonalSpot";
42
35
  var DEFAULT_CONTRAST = 0;
43
36
  var DEFAULT_CUSTOM_COLORS = [];
44
- var DEFAULT_CONTRAST_ALL_COLORS = false;
45
- var DEFAULT_ADAPTIVE_SHADES = false;
46
37
  var DEFAULT_BLEND = true;
47
- var DEFAULT_CONTRAST_ADJUSTMENT_FACTOR = 0.2;
38
+ var DEFAULT_PREFIX = "md";
48
39
  var STANDARD_TONES = [
40
+ 0,
41
+ 4,
42
+ 5,
43
+ 6,
44
+ 10,
45
+ 12,
46
+ 15,
47
+ 17,
48
+ 20,
49
+ 22,
50
+ 24,
51
+ 25,
52
+ 30,
53
+ 35,
54
+ 40,
55
+ 50,
56
+ 60,
57
+ 70,
58
+ 80,
59
+ 87,
60
+ 90,
61
+ 92,
62
+ 94,
63
+ 95,
64
+ 96,
65
+ 98,
66
+ 99,
67
+ 100
68
+ ];
69
+ var MTB_TONES = [
49
70
  0,
50
71
  5,
51
72
  10,
@@ -85,57 +106,60 @@ var schemeToVariant = {
85
106
  fidelity: Variant.FIDELITY,
86
107
  content: Variant.CONTENT
87
108
  };
88
- var tokenNames = [
89
- "background",
90
- "error",
91
- "errorContainer",
92
- "inverseOnSurface",
93
- "inversePrimary",
94
- "inverseSurface",
95
- "onBackground",
96
- "onError",
97
- "onErrorContainer",
98
- "onPrimary",
99
- "onPrimaryContainer",
100
- "onPrimaryFixed",
101
- "onPrimaryFixedVariant",
102
- "onSecondary",
103
- "onSecondaryContainer",
104
- "onSecondaryFixed",
105
- "onSecondaryFixedVariant",
106
- "onSurface",
107
- "onSurfaceVariant",
108
- "onTertiary",
109
- "onTertiaryContainer",
110
- "onTertiaryFixed",
111
- "onTertiaryFixedVariant",
112
- "outline",
113
- "outlineVariant",
114
- "primary",
115
- "primaryContainer",
116
- "primaryFixed",
117
- "primaryFixedDim",
118
- "scrim",
119
- "secondary",
120
- "secondaryContainer",
121
- "secondaryFixed",
122
- "secondaryFixedDim",
123
- "shadow",
124
- "surface",
125
- "surfaceBright",
126
- "surfaceContainer",
127
- "surfaceContainerHigh",
128
- "surfaceContainerHighest",
129
- "surfaceContainerLow",
130
- "surfaceContainerLowest",
131
- "surfaceDim",
132
- "surfaceTint",
133
- "surfaceVariant",
134
- "tertiary",
135
- "tertiaryContainer",
136
- "tertiaryFixed",
137
- "tertiaryFixedDim"
138
- ];
109
+ var tokenDescriptions = {
110
+ background: "Default background color for screens and large surfaces.",
111
+ error: "Color for error states, used on elements like error text and icons.",
112
+ errorContainer: "Fill color for error container elements like error banners.",
113
+ inverseOnSurface: "Color for text and icons on inverse surface backgrounds.",
114
+ inversePrimary: "Primary color used on inverse surface, e.g. buttons on snackbars.",
115
+ inverseSurface: "Background for elements that require reverse contrast, such as snackbars.",
116
+ onBackground: "Color for text and icons displayed on the background.",
117
+ onError: "Color for text and icons on error-colored elements.",
118
+ onErrorContainer: "Color for text and icons on error container elements.",
119
+ onPrimary: "Color for text and icons on primary-colored elements like filled buttons.",
120
+ onPrimaryContainer: "Color for text and icons on primary container elements like tonal buttons.",
121
+ onPrimaryFixed: "Color for text and icons on primary fixed elements, constant across themes.",
122
+ onPrimaryFixedVariant: "Lower-emphasis color for text and icons on primary fixed elements.",
123
+ onSecondary: "Color for text and icons on secondary-colored elements.",
124
+ onSecondaryContainer: "Color for text and icons on secondary container elements.",
125
+ onSecondaryFixed: "Color for text and icons on secondary fixed elements, constant across themes.",
126
+ onSecondaryFixedVariant: "Lower-emphasis color for text and icons on secondary fixed elements.",
127
+ onSurface: "High-emphasis color for text and icons on surface backgrounds.",
128
+ onSurfaceVariant: "Medium-emphasis color for text and icons on surface variant backgrounds.",
129
+ onTertiary: "Color for text and icons on tertiary-colored elements.",
130
+ onTertiaryContainer: "Color for text and icons on tertiary container elements.",
131
+ onTertiaryFixed: "Color for text and icons on tertiary fixed elements, constant across themes.",
132
+ onTertiaryFixedVariant: "Lower-emphasis color for text and icons on tertiary fixed elements.",
133
+ outline: "Subtle color for borders and dividers to create visual separation.",
134
+ outlineVariant: "Lower-emphasis border color used for decorative dividers.",
135
+ primary: "Main brand color, used for key components like filled buttons and active states.",
136
+ primaryContainer: "Fill color for large primary elements like cards and tonal buttons.",
137
+ primaryFixed: "Fixed primary color that stays the same in light and dark themes.",
138
+ primaryFixedDim: "Dimmed variant of the fixed primary color for lower emphasis.",
139
+ scrim: "Color overlay for modals and dialogs to obscure background content.",
140
+ secondary: "Accent color for less prominent elements like filter chips and selections.",
141
+ secondaryContainer: "Fill color for secondary container elements like tonal buttons and input fields.",
142
+ secondaryFixed: "Fixed secondary color that stays the same in light and dark themes.",
143
+ secondaryFixedDim: "Dimmed variant of the fixed secondary color for lower emphasis.",
144
+ shadow: "Color for elevation shadows applied to surfaces and components.",
145
+ surface: "Default surface color for cards, sheets, and dialogs.",
146
+ surfaceBright: "Brightest surface variant, used for elevated surfaces in dark themes.",
147
+ surfaceContainer: "Middle-emphasis container color for grouping related content.",
148
+ surfaceContainerHigh: "Higher-emphasis container color for elements like cards.",
149
+ surfaceContainerHighest: "Highest-emphasis container color for text fields and other input areas.",
150
+ surfaceContainerLow: "Lower-emphasis container color for subtle surface groupings.",
151
+ surfaceContainerLowest: "Lowest-emphasis container, typically the lightest surface in light theme.",
152
+ surfaceDim: "Dimmest surface variant, used for recessed areas or dark theme backgrounds.",
153
+ surfaceTint: "Tint color applied to surfaces for subtle primary color elevation overlay.",
154
+ surfaceVariant: "Alternative surface color for differentiated areas like sidebar backgrounds.",
155
+ tertiary: "Third accent color for complementary elements that balance primary and secondary.",
156
+ tertiaryContainer: "Fill color for tertiary container elements like complementary cards.",
157
+ tertiaryFixed: "Fixed tertiary color that stays the same in light and dark themes.",
158
+ tertiaryFixedDim: "Dimmed variant of the fixed tertiary color for lower emphasis."
159
+ };
160
+ var tokenNames = Object.keys(
161
+ tokenDescriptions
162
+ );
139
163
  function toRecord(arr, getEntry) {
140
164
  return Object.fromEntries(arr.map(getEntry));
141
165
  }
@@ -148,7 +172,7 @@ function getPalette(palettes, colorName) {
148
172
  }
149
173
  return palette;
150
174
  }
151
- function mergeBaseAndCustomColors(scheme, customColors, colorPalettes, contrastAllColors) {
175
+ function mergeBaseAndCustomColors(scheme, customColors, colorPalettes) {
152
176
  const baseVars = toRecord(tokenNames, (tokenName) => {
153
177
  const dynamicColor = MaterialDynamicColors[tokenName];
154
178
  const argb = dynamicColor.getArgb(scheme);
@@ -158,9 +182,8 @@ function mergeBaseAndCustomColors(scheme, customColors, colorPalettes, contrastA
158
182
  customColors.forEach((color) => {
159
183
  const colorname = color.name;
160
184
  const getPaletteForColor = (s) => getPalette(colorPalettes, colorname);
161
- const getTone = (baseTone) => (s) => {
162
- if (!contrastAllColors) return baseTone;
163
- return adjustToneForContrast(baseTone, s.contrastLevel);
185
+ const getTone = (baseTone) => (_s) => {
186
+ return baseTone;
164
187
  };
165
188
  const colorDynamicColor = new DynamicColor(
166
189
  colorname,
@@ -227,8 +250,7 @@ function builder(hexSource, {
227
250
  neutralVariant,
228
251
  error,
229
252
  customColors: hexCustomColors = DEFAULT_CUSTOM_COLORS,
230
- contrastAllColors = DEFAULT_CONTRAST_ALL_COLORS,
231
- adaptiveShades = DEFAULT_ADAPTIVE_SHADES
253
+ prefix = DEFAULT_PREFIX
232
254
  } = {}) {
233
255
  const sourceArgb = argbFromHex(hexSource);
234
256
  const sourceHct = Hct.fromInt(sourceArgb);
@@ -328,15 +350,49 @@ function builder(hexSource, {
328
350
  const mergedColorsLight = mergeBaseAndCustomColors(
329
351
  lightScheme,
330
352
  customColors,
331
- colorPalettes,
332
- contrastAllColors
353
+ colorPalettes
333
354
  );
334
355
  const mergedColorsDark = mergeBaseAndCustomColors(
335
356
  darkScheme,
336
357
  customColors,
337
- colorPalettes,
338
- contrastAllColors
358
+ colorPalettes
339
359
  );
360
+ const schemePalettes = [
361
+ ["primary", lightScheme.primaryPalette],
362
+ ["secondary", lightScheme.secondaryPalette],
363
+ ["tertiary", lightScheme.tertiaryPalette],
364
+ ["error", lightScheme.errorPalette],
365
+ ["neutral", lightScheme.neutralPalette],
366
+ ["neutral-variant", lightScheme.neutralVariantPalette]
367
+ ];
368
+ function buildTokenToPaletteMap(schemePalettes2, scheme2) {
369
+ const result = {};
370
+ for (const propName of Object.getOwnPropertyNames(MaterialDynamicColors)) {
371
+ const dc = MaterialDynamicColors[propName];
372
+ if (!(dc instanceof DynamicColor)) continue;
373
+ const palette = dc.palette(scheme2);
374
+ for (const [palName, pal] of schemePalettes2) {
375
+ if (palette === pal) {
376
+ result[propName] = palName;
377
+ break;
378
+ }
379
+ }
380
+ }
381
+ return result;
382
+ }
383
+ const tokenToPalette = buildTokenToPaletteMap(schemePalettes, lightScheme);
384
+ const allPaletteNamesKebab = new Set(Object.keys(allPalettes).map(kebabCase));
385
+ function deriveCustomPaletteName(tokenName) {
386
+ let baseName = tokenName;
387
+ if (/^on[A-Z]/.test(baseName) && baseName.length > 2) {
388
+ baseName = baseName.charAt(2).toLowerCase() + baseName.slice(3);
389
+ }
390
+ if (baseName.endsWith("Container")) {
391
+ baseName = baseName.slice(0, -"Container".length);
392
+ }
393
+ const kebab = kebabCase(baseName);
394
+ return allPaletteNamesKebab.has(kebab) ? kebab : void 0;
395
+ }
340
396
  return {
341
397
  //
342
398
  // ██████ ███████ ███████
@@ -346,45 +402,55 @@ function builder(hexSource, {
346
402
  // ██████ ███████ ███████
347
403
  //
348
404
  toCss() {
349
- function cssVar(colorName, colorValue) {
350
- const name = `--mcu-${kebabCase(colorName)}`;
405
+ function buildRefPaletteLookup() {
406
+ const lookup = {};
407
+ for (const [name, palette] of Object.entries(allPalettes)) {
408
+ const paletteName = kebabCase(name);
409
+ for (const tone of STANDARD_TONES) {
410
+ const hex = hexFromArgb(palette.tone(tone));
411
+ if (!lookup[hex]) lookup[hex] = [];
412
+ lookup[hex].push({ paletteName, tone });
413
+ }
414
+ }
415
+ return lookup;
416
+ }
417
+ function sysColorVar(colorName, colorValue, lookup) {
418
+ const name = `--${prefix}-sys-color-${kebabCase(colorName)}`;
419
+ const hex = hexFromArgb(colorValue);
420
+ const matches = lookup[hex];
421
+ if (matches && matches.length > 0) {
422
+ const preferred = tokenToPalette[colorName] ?? deriveCustomPaletteName(colorName);
423
+ const match = (preferred ? matches.find((m) => m.paletteName === preferred) : void 0) ?? matches[0];
424
+ return `${name}:var(--${prefix}-ref-palette-${match.paletteName}-${match.tone});`;
425
+ }
426
+ return `${name}:${hex};`;
427
+ }
428
+ function toCssVars(mergedColors, lookup) {
429
+ return Object.entries(mergedColors).map(([name, value]) => sysColorVar(name, value, lookup)).join(" ");
430
+ }
431
+ function refPaletteVar(paletteName, tone, colorValue) {
432
+ const name = `--${prefix}-ref-palette-${paletteName}-${tone}`;
351
433
  const value = hexFromArgb(colorValue);
352
434
  return `${name}:${value};`;
353
435
  }
354
- function toCssVars(mergedColors) {
355
- return Object.entries(mergedColors).map(([name, value]) => cssVar(name, value)).join(" ");
356
- }
357
- function generateTonalPaletteVars(paletteName, palette, scheme2, applyContrast, adaptiveShades2) {
436
+ function generateTonalPaletteVars(paletteName, palette) {
358
437
  return STANDARD_TONES.map((tone) => {
359
- let toneToUse = tone;
360
- if (adaptiveShades2 && scheme2.isDark) {
361
- toneToUse = 100 - tone;
362
- }
363
- if (applyContrast) {
364
- toneToUse = adjustToneForContrast(toneToUse, scheme2.contrastLevel);
365
- }
366
- const color = palette.tone(toneToUse);
367
- return cssVar(`${paletteName}-${tone}`, color);
438
+ const color = palette.tone(tone);
439
+ return refPaletteVar(paletteName, tone, color);
368
440
  }).join(" ");
369
441
  }
370
- function generateTonalVars(s) {
442
+ function generateTonalVars() {
371
443
  return Object.entries(allPalettes).map(
372
- ([name, palette]) => generateTonalPaletteVars(
373
- kebabCase(name),
374
- palette,
375
- s,
376
- contrastAllColors,
377
- adaptiveShades
378
- )
444
+ ([name, palette]) => generateTonalPaletteVars(kebabCase(name), palette)
379
445
  ).join(" ");
380
446
  }
381
- const lightVars = toCssVars(mergedColorsLight);
382
- const darkVars = toCssVars(mergedColorsDark);
383
- const lightTonalVars = generateTonalVars(lightScheme);
384
- const darkTonalVars = generateTonalVars(darkScheme);
447
+ const refPaletteLookup = buildRefPaletteLookup();
448
+ const lightVars = toCssVars(mergedColorsLight, refPaletteLookup);
449
+ const darkVars = toCssVars(mergedColorsDark, refPaletteLookup);
450
+ const tonalVars = generateTonalVars();
385
451
  return `
386
- :root { ${lightVars} ${lightTonalVars} }
387
- .dark { ${darkVars} ${adaptiveShades ? darkTonalVars : lightTonalVars} }
452
+ :root { ${lightVars} ${tonalVars} }
453
+ .dark { ${darkVars} ${tonalVars} }
388
454
  `;
389
455
  },
390
456
  //
@@ -527,7 +593,7 @@ function builder(hexSource, {
527
593
  for (const name of RAW_PALETTE_NAMES) {
528
594
  const palette = rawPalettes[name];
529
595
  const tones = {};
530
- for (const tone of STANDARD_TONES) {
596
+ for (const tone of MTB_TONES) {
531
597
  tones[tone.toString()] = hexFromArgb(
532
598
  palette.tone(tone)
533
599
  ).toUpperCase();
@@ -599,44 +665,82 @@ function builder(hexSource, {
599
665
  }
600
666
  };
601
667
  }
602
- function buildFigmaSchemeTokens(mergedColors) {
603
- const tokens = {};
604
- for (const [name, argb] of Object.entries(mergedColors)) {
605
- tokens[startCase(name)] = figmaToken(argb);
606
- }
607
- return tokens;
608
- }
609
- function buildFigmaPaletteTokens(isDark) {
668
+ function buildRefPaletteTokens() {
610
669
  const palettes = {};
611
670
  for (const [name, palette] of Object.entries(allPalettes)) {
612
671
  const tones = {};
613
672
  for (const tone of STANDARD_TONES) {
614
- let toneToUse = tone;
615
- if (adaptiveShades && isDark) {
616
- toneToUse = 100 - tone;
617
- }
618
- if (contrastAllColors) {
619
- toneToUse = adjustToneForContrast(toneToUse, contrast);
620
- }
621
- const argb = palette.tone(toneToUse);
673
+ const argb = palette.tone(tone);
622
674
  tones[tone.toString()] = figmaToken(argb);
623
675
  }
624
676
  palettes[startCase(name)] = tones;
625
677
  }
626
678
  return palettes;
627
679
  }
628
- function buildModeFile(modeName, mergedColors, isDark) {
680
+ function findToneInPalette(hex, paletteName, tones) {
681
+ for (const [tone, token] of Object.entries(tones)) {
682
+ if (token.$value.hex === hex) {
683
+ return `{ref.palette.${paletteName}.${tone}}`;
684
+ }
685
+ }
686
+ return null;
687
+ }
688
+ function findAlias(hex, tokenName, refPalettes2) {
689
+ const preferredPaletteKebab = tokenToPalette[tokenName] ?? deriveCustomPaletteName(tokenName);
690
+ const preferredPalette = preferredPaletteKebab ? startCase(preferredPaletteKebab) : void 0;
691
+ if (preferredPalette && refPalettes2[preferredPalette]) {
692
+ const match = findToneInPalette(
693
+ hex,
694
+ preferredPalette,
695
+ refPalettes2[preferredPalette]
696
+ );
697
+ if (match) return match;
698
+ }
699
+ for (const [palName, tones] of Object.entries(refPalettes2)) {
700
+ const match = findToneInPalette(hex, palName, tones);
701
+ if (match) return match;
702
+ }
703
+ return null;
704
+ }
705
+ function resolveModeValue(argb, tokenName, refPalettes2) {
706
+ const hex = hexFromArgb(argb).toUpperCase();
707
+ return findAlias(hex, tokenName, refPalettes2) ?? argbToFigmaColorValue(argb);
708
+ }
709
+ function buildSysColorTokens(mergedColors, refPalettes2) {
710
+ const tokens = {};
711
+ for (const [name, argb] of Object.entries(mergedColors)) {
712
+ const description = tokenDescriptions[name];
713
+ const cssVar = `--${prefix}-sys-color-${kebabCase(name)}`;
714
+ const value = resolveModeValue(argb, name, refPalettes2);
715
+ tokens[startCase(name)] = {
716
+ $type: "color",
717
+ $value: value,
718
+ ...description ? { $description: description } : {},
719
+ $extensions: {
720
+ "com.figma.scopes": ["ALL_SCOPES"],
721
+ "css.variable": cssVar
722
+ }
723
+ };
724
+ }
725
+ return tokens;
726
+ }
727
+ const refPalettes = buildRefPaletteTokens();
728
+ function buildModeFile(modeName, mergedColors) {
629
729
  return {
630
- Schemes: buildFigmaSchemeTokens(mergedColors),
631
- Palettes: buildFigmaPaletteTokens(isDark),
730
+ ref: {
731
+ palette: refPalettes
732
+ },
733
+ sys: {
734
+ color: buildSysColorTokens(mergedColors, refPalettes)
735
+ },
632
736
  $extensions: {
633
737
  "com.figma.modeName": modeName
634
738
  }
635
739
  };
636
740
  }
637
741
  return {
638
- "Light.tokens.json": buildModeFile("Light", mergedColorsLight, false),
639
- "Dark.tokens.json": buildModeFile("Dark", mergedColorsDark, true)
742
+ "Light.tokens.json": buildModeFile("Light", mergedColorsLight),
743
+ "Dark.tokens.json": buildModeFile("Dark", mergedColorsDark)
640
744
  };
641
745
  },
642
746
  //
@@ -742,8 +846,7 @@ function Mcu({
742
846
  error,
743
847
  colorMatch = DEFAULT_COLOR_MATCH,
744
848
  customColors = DEFAULT_CUSTOM_COLORS,
745
- contrastAllColors = DEFAULT_CONTRAST_ALL_COLORS,
746
- adaptiveShades = DEFAULT_ADAPTIVE_SHADES,
849
+ prefix = DEFAULT_PREFIX,
747
850
  children
748
851
  }) {
749
852
  const config = useMemo2(
@@ -759,9 +862,7 @@ function Mcu({
759
862
  error,
760
863
  colorMatch,
761
864
  customColors,
762
- // extras features
763
- contrastAllColors,
764
- adaptiveShades
865
+ prefix
765
866
  }),
766
867
  [
767
868
  contrast,
@@ -775,8 +876,7 @@ function Mcu({
775
876
  neutralVariant,
776
877
  error,
777
878
  colorMatch,
778
- contrastAllColors,
779
- adaptiveShades
879
+ prefix
780
880
  ]
781
881
  );
782
882
  return /* @__PURE__ */ jsx2(McuProvider, { ...config, styleId: mcuStyleId, children });