react-mcu 1.3.0 → 1.3.1

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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
1
  import { CustomColor, TonalPalette } from '@material/material-color-utilities';
2
+ import * as react_jsx_runtime from 'react/jsx-runtime';
3
3
 
4
4
  type HexCustomColor = Omit<CustomColor, "value"> & {
5
5
  hex: string;
@@ -34,39 +34,26 @@ type McuConfig = {
34
34
  colorMatch?: boolean;
35
35
  /**
36
36
  * Array of custom colors to include in the generated palette.
37
- *
38
- * @example
39
- * ```ts
40
- * customColors={[
41
- * { name: "brand", hex: "#FF5733", blend: true },
42
- * { name: "success", hex: "#28A745", blend: false }
43
- * ]}
44
- * ```
37
+ * Each custom color can be blended with the source color for harmonization.
45
38
  */
46
39
  customColors?: HexCustomColor[];
47
40
  /**
48
- * When true, applies the contrast level to all colors including custom colors and tonal palette shades.
49
- * When false (default), only core colors are affected by the contrast level.
41
+ * Apply contrast adjustment to tonal palette shades when true.
42
+ * When true, all tonal palette shades are adjusted based on the contrast level.
43
+ * When false (default), contrast adjustments only apply to core Material Design tokens.
50
44
  */
51
45
  contrastAllColors?: boolean;
52
46
  /**
53
- * When true (default), tonal palette shades adapt to the theme (light/dark) with inverted tone values.
54
- * In dark mode, high tones (light colors) map to low tones (dark colors) and vice versa.
55
- * When false, shades remain the same across themes.
47
+ * Adapt tonal palette shades for dark mode.
48
+ * When true, tonal palette shades automatically invert for dark mode
49
+ * (high values become dark, low values become light).
50
+ * When false (default), tonal palette values remain constant across light/dark mode.
56
51
  */
57
52
  adaptiveShades?: boolean;
58
53
  };
59
54
  declare const schemeNames: readonly ["tonalSpot", "monochrome", "neutral", "vibrant", "expressive", "fidelity", "content"];
60
55
  type SchemeName = (typeof schemeNames)[number];
61
- declare const DEFAULT_SCHEME: SchemeName;
62
- declare const DEFAULT_CONTRAST = 0;
63
- declare const DEFAULT_CONTRAST_ALL_COLORS = false;
64
- declare const DEFAULT_ADAPTIVE_SHADES = false;
65
- declare const DEFAULT_BLEND = true;
66
- declare function Mcu({ source, scheme, contrast, primary, secondary, tertiary, neutral, neutralVariant, error, colorMatch, customColors, contrastAllColors, adaptiveShades, children, }: McuConfig & {
67
- children?: React.ReactNode;
68
- }): react_jsx_runtime.JSX.Element;
69
- declare const tokenNames: readonly ["background", "onBackground", "surface", "surfaceDim", "surfaceBright", "surfaceContainerLowest", "surfaceContainerLow", "surfaceContainer", "surfaceContainerHigh", "surfaceContainerHighest", "onSurface", "surfaceVariant", "onSurfaceVariant", "outline", "outlineVariant", "inverseSurface", "inverseOnSurface", "primary", "surfaceTint", "onPrimary", "primaryContainer", "onPrimaryContainer", "primaryFixed", "primaryFixedDim", "onPrimaryFixed", "onPrimaryFixedVariant", "inversePrimary", "secondary", "onSecondary", "secondaryContainer", "onSecondaryContainer", "secondaryFixed", "secondaryFixedDim", "onSecondaryFixed", "onSecondaryFixedVariant", "tertiary", "onTertiary", "tertiaryContainer", "onTertiaryContainer", "tertiaryFixed", "tertiaryFixedDim", "onTertiaryFixed", "onTertiaryFixedVariant", "error", "onError", "errorContainer", "onErrorContainer", "scrim", "shadow"];
56
+ declare const tokenNames: readonly ["background", "error", "errorContainer", "inverseOnSurface", "inversePrimary", "inverseSurface", "onBackground", "onError", "onErrorContainer", "onPrimary", "onPrimaryContainer", "onPrimaryFixed", "onPrimaryFixedVariant", "onSecondary", "onSecondaryContainer", "onSecondaryFixed", "onSecondaryFixedVariant", "onSurface", "onSurfaceVariant", "onTertiary", "onTertiaryContainer", "onTertiaryFixed", "onTertiaryFixedVariant", "outline", "outlineVariant", "primary", "primaryContainer", "primaryFixed", "primaryFixedDim", "scrim", "secondary", "secondaryContainer", "secondaryFixed", "secondaryFixedDim", "shadow", "surface", "surfaceBright", "surfaceContainer", "surfaceContainerHigh", "surfaceContainerHighest", "surfaceContainerLow", "surfaceContainerLowest", "surfaceDim", "surfaceTint", "surfaceVariant", "tertiary", "tertiaryContainer", "tertiaryFixed", "tertiaryFixedDim"];
70
57
  type TokenName = (typeof tokenNames)[number];
71
58
  declare function builder(hexSource: McuConfig["source"], { scheme, contrast, primary, secondary, tertiary, neutral, neutralVariant, error, customColors: hexCustomColors, contrastAllColors, adaptiveShades, }?: Omit<McuConfig, "source">): {
72
59
  toCss(): string;
@@ -162,6 +149,10 @@ declare function builder(hexSource: McuConfig["source"], { scheme, contrast, pri
162
149
  };
163
150
  };
164
151
 
152
+ declare function Mcu({ source, scheme, contrast, primary, secondary, tertiary, neutral, neutralVariant, error, colorMatch, customColors, contrastAllColors, adaptiveShades, children, }: McuConfig & {
153
+ children?: React.ReactNode;
154
+ }): react_jsx_runtime.JSX.Element;
155
+
165
156
  type Api = {
166
157
  initials: McuConfig;
167
158
  setMcuConfig: (config: McuConfig) => void;
@@ -170,4 +161,4 @@ type Api = {
170
161
  };
171
162
  declare const useMcu: () => Api;
172
163
 
173
- export { DEFAULT_ADAPTIVE_SHADES, DEFAULT_BLEND, DEFAULT_CONTRAST, DEFAULT_CONTRAST_ALL_COLORS, DEFAULT_SCHEME, Mcu, builder, schemeNames, useMcu };
164
+ export { Mcu, builder, useMcu };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use client";
2
2
 
3
- // src/Mcu.tsx
3
+ // src/lib/builder.ts
4
4
  import {
5
5
  argbFromHex,
6
6
  Blend,
@@ -9,7 +9,7 @@ import {
9
9
  DynamicScheme,
10
10
  greenFromArgb,
11
11
  Hct,
12
- hexFromArgb as hexFromArgb2,
12
+ hexFromArgb,
13
13
  MaterialDynamicColors,
14
14
  redFromArgb,
15
15
  SchemeContent,
@@ -22,85 +22,6 @@ import {
22
22
  TonalPalette
23
23
  } from "@material/material-color-utilities";
24
24
  import { kebabCase, startCase, upperFirst } from "lodash-es";
25
- import { useMemo as useMemo2 } from "react";
26
-
27
- // src/Mcu.context.tsx
28
- import {
29
- hexFromArgb
30
- } from "@material/material-color-utilities";
31
- import React, {
32
- useCallback,
33
- useInsertionEffect,
34
- useMemo,
35
- useState
36
- } from "react";
37
-
38
- // src/lib/createRequiredContext.ts
39
- import { createContext, useContext } from "react";
40
- var createRequiredContext = () => {
41
- const Ctx = createContext(null);
42
- const useCtx = () => {
43
- const contextValue = useContext(Ctx);
44
- if (contextValue === null) {
45
- throw new Error("Context value is null");
46
- }
47
- return contextValue;
48
- };
49
- return [useCtx, Ctx.Provider, Ctx];
50
- };
51
-
52
- // src/Mcu.context.tsx
53
- import { jsx } from "react/jsx-runtime";
54
- var [useMcu, Provider, McuContext] = createRequiredContext();
55
- var McuProvider = ({
56
- styleId,
57
- children,
58
- ...configProps
59
- }) => {
60
- const [initials] = useState(() => configProps);
61
- const [mcuConfig, setMcuConfig] = useState(initials);
62
- const configKey = JSON.stringify(configProps);
63
- React.useEffect(() => {
64
- setMcuConfig(configProps);
65
- }, [configKey]);
66
- const { css, mergedColorsLight, mergedColorsDark, allPalettes } = useMemo(() => {
67
- const { toCss, ...rest } = builder(mcuConfig.source, mcuConfig);
68
- return { css: toCss(), ...rest };
69
- }, [mcuConfig]);
70
- useInsertionEffect(() => {
71
- let tag = document.getElementById(styleId);
72
- if (!tag) {
73
- tag = document.createElement("style");
74
- tag.id = styleId;
75
- document.head.appendChild(tag);
76
- }
77
- tag.textContent = css;
78
- }, [css, styleId]);
79
- const getMcuColor = useCallback(
80
- (colorName, theme) => {
81
- const mergedColors = theme === "light" ? mergedColorsLight : mergedColorsDark;
82
- const colorValue = mergedColors[colorName];
83
- if (colorValue === void 0) {
84
- throw new Error(`Unknown MCU token '${colorName}'`);
85
- }
86
- return hexFromArgb(colorValue);
87
- },
88
- [mergedColorsDark, mergedColorsLight]
89
- );
90
- const value = useMemo(
91
- () => ({
92
- initials,
93
- setMcuConfig,
94
- getMcuColor,
95
- allPalettes
96
- }),
97
- [getMcuColor, initials, allPalettes]
98
- );
99
- return /* @__PURE__ */ jsx(Provider, { value, children });
100
- };
101
-
102
- // src/Mcu.tsx
103
- import { jsx as jsx2 } from "react/jsx-runtime";
104
25
  function adjustToneForContrast(baseTone, contrastLevel, adjustmentFactor = DEFAULT_CONTRAST_ADJUSTMENT_FACTOR) {
105
26
  if (contrastLevel === 0) return baseTone;
106
27
  const distanceToCenter = baseTone - 50;
@@ -108,15 +29,6 @@ function adjustToneForContrast(baseTone, contrastLevel, adjustmentFactor = DEFAU
108
29
  const adjustedTone = baseTone + delta;
109
30
  return Math.max(0, Math.min(100, adjustedTone));
110
31
  }
111
- var schemeNames = [
112
- "tonalSpot",
113
- "monochrome",
114
- "neutral",
115
- "vibrant",
116
- "expressive",
117
- "fidelity",
118
- "content"
119
- ];
120
32
  var schemesMap = {
121
33
  tonalSpot: SchemeTonalSpot,
122
34
  monochrome: SchemeMonochrome,
@@ -128,7 +40,6 @@ var schemesMap = {
128
40
  };
129
41
  var DEFAULT_SCHEME = "tonalSpot";
130
42
  var DEFAULT_CONTRAST = 0;
131
- var DEFAULT_COLOR_MATCH = false;
132
43
  var DEFAULT_CUSTOM_COLORS = [];
133
44
  var DEFAULT_CONTRAST_ALL_COLORS = false;
134
45
  var DEFAULT_ADAPTIVE_SHADES = false;
@@ -166,116 +77,64 @@ var Variant = {
166
77
  FRUIT_SALAD: 8
167
78
  };
168
79
  var schemeToVariant = {
169
- tonalSpot: Variant.TONAL_SPOT,
170
80
  monochrome: Variant.MONOCHROME,
171
81
  neutral: Variant.NEUTRAL,
82
+ tonalSpot: Variant.TONAL_SPOT,
172
83
  vibrant: Variant.VIBRANT,
173
84
  expressive: Variant.EXPRESSIVE,
174
85
  fidelity: Variant.FIDELITY,
175
86
  content: Variant.CONTENT
176
87
  };
177
- var mcuStyleId = "mcu-styles";
178
- function Mcu({
179
- source,
180
- scheme = DEFAULT_SCHEME,
181
- contrast = DEFAULT_CONTRAST,
182
- primary,
183
- secondary,
184
- tertiary,
185
- neutral,
186
- neutralVariant,
187
- error,
188
- colorMatch = DEFAULT_COLOR_MATCH,
189
- customColors = DEFAULT_CUSTOM_COLORS,
190
- contrastAllColors = DEFAULT_CONTRAST_ALL_COLORS,
191
- adaptiveShades = DEFAULT_ADAPTIVE_SHADES,
192
- children
193
- }) {
194
- const config = useMemo2(
195
- () => ({
196
- source,
197
- scheme,
198
- contrast,
199
- primary,
200
- secondary,
201
- tertiary,
202
- neutral,
203
- neutralVariant,
204
- error,
205
- colorMatch,
206
- customColors,
207
- // extras features
208
- contrastAllColors,
209
- adaptiveShades
210
- }),
211
- [
212
- contrast,
213
- customColors,
214
- scheme,
215
- source,
216
- primary,
217
- secondary,
218
- tertiary,
219
- neutral,
220
- neutralVariant,
221
- error,
222
- colorMatch,
223
- contrastAllColors,
224
- adaptiveShades
225
- ]
226
- );
227
- return /* @__PURE__ */ jsx2(McuProvider, { ...config, styleId: mcuStyleId, children });
228
- }
229
88
  var tokenNames = [
230
89
  "background",
90
+ "error",
91
+ "errorContainer",
92
+ "inverseOnSurface",
93
+ "inversePrimary",
94
+ "inverseSurface",
231
95
  "onBackground",
232
- "surface",
233
- "surfaceDim",
234
- "surfaceBright",
235
- "surfaceContainerLowest",
236
- "surfaceContainerLow",
237
- "surfaceContainer",
238
- "surfaceContainerHigh",
239
- "surfaceContainerHighest",
96
+ "onError",
97
+ "onErrorContainer",
98
+ "onPrimary",
99
+ "onPrimaryContainer",
100
+ "onPrimaryFixed",
101
+ "onPrimaryFixedVariant",
102
+ "onSecondary",
103
+ "onSecondaryContainer",
104
+ "onSecondaryFixed",
105
+ "onSecondaryFixedVariant",
240
106
  "onSurface",
241
- "surfaceVariant",
242
107
  "onSurfaceVariant",
108
+ "onTertiary",
109
+ "onTertiaryContainer",
110
+ "onTertiaryFixed",
111
+ "onTertiaryFixedVariant",
243
112
  "outline",
244
113
  "outlineVariant",
245
- "inverseSurface",
246
- "inverseOnSurface",
247
114
  "primary",
248
- "surfaceTint",
249
- "onPrimary",
250
115
  "primaryContainer",
251
- "onPrimaryContainer",
252
116
  "primaryFixed",
253
117
  "primaryFixedDim",
254
- "onPrimaryFixed",
255
- "onPrimaryFixedVariant",
256
- "inversePrimary",
118
+ "scrim",
257
119
  "secondary",
258
- "onSecondary",
259
120
  "secondaryContainer",
260
- "onSecondaryContainer",
261
121
  "secondaryFixed",
262
122
  "secondaryFixedDim",
263
- "onSecondaryFixed",
264
- "onSecondaryFixedVariant",
123
+ "shadow",
124
+ "surface",
125
+ "surfaceBright",
126
+ "surfaceContainer",
127
+ "surfaceContainerHigh",
128
+ "surfaceContainerHighest",
129
+ "surfaceContainerLow",
130
+ "surfaceContainerLowest",
131
+ "surfaceDim",
132
+ "surfaceTint",
133
+ "surfaceVariant",
265
134
  "tertiary",
266
- "onTertiary",
267
135
  "tertiaryContainer",
268
- "onTertiaryContainer",
269
136
  "tertiaryFixed",
270
- "tertiaryFixedDim",
271
- "onTertiaryFixed",
272
- "onTertiaryFixedVariant",
273
- "error",
274
- "onError",
275
- "errorContainer",
276
- "onErrorContainer",
277
- "scrim",
278
- "shadow"
137
+ "tertiaryFixedDim"
279
138
  ];
280
139
  function toRecord(arr, getEntry) {
281
140
  return Object.fromEntries(arr.map(getEntry));
@@ -489,7 +348,7 @@ function builder(hexSource, {
489
348
  toCss() {
490
349
  function cssVar(colorName, colorValue) {
491
350
  const name = `--mcu-${kebabCase(colorName)}`;
492
- const value = hexFromArgb2(colorValue);
351
+ const value = hexFromArgb(colorValue);
493
352
  return `${name}:${value};`;
494
353
  }
495
354
  function toCssVars(mergedColors) {
@@ -611,7 +470,7 @@ function builder(hexSource, {
611
470
  for (const tokenName of fixtureTokenOrder) {
612
471
  const dynamicColor = MaterialDynamicColors[tokenName];
613
472
  const useScheme = backgroundScheme && (tokenName === "background" || tokenName === "onBackground") ? backgroundScheme : scheme2;
614
- colors[tokenName] = hexFromArgb2(
473
+ colors[tokenName] = hexFromArgb(
615
474
  dynamicColor.getArgb(useScheme)
616
475
  ).toUpperCase();
617
476
  }
@@ -669,7 +528,7 @@ function builder(hexSource, {
669
528
  const palette = rawPalettes[name];
670
529
  const tones = {};
671
530
  for (const tone of STANDARD_TONES) {
672
- tones[tone.toString()] = hexFromArgb2(
531
+ tones[tone.toString()] = hexFromArgb(
673
532
  palette.tone(tone)
674
533
  ).toUpperCase();
675
534
  }
@@ -727,7 +586,7 @@ function builder(hexSource, {
727
586
  blueFromArgb(argb) / 255
728
587
  ],
729
588
  alpha: 1,
730
- hex: hexFromArgb2(argb).toUpperCase()
589
+ hex: hexFromArgb(argb).toUpperCase()
731
590
  };
732
591
  }
733
592
  function figmaToken(argb) {
@@ -788,14 +647,142 @@ function builder(hexSource, {
788
647
  allPalettes
789
648
  };
790
649
  }
650
+
651
+ // src/Mcu.tsx
652
+ import { useMemo as useMemo2 } from "react";
653
+
654
+ // src/Mcu.context.tsx
655
+ import {
656
+ hexFromArgb as hexFromArgb2
657
+ } from "@material/material-color-utilities";
658
+ import React, {
659
+ useCallback,
660
+ useInsertionEffect,
661
+ useMemo,
662
+ useState
663
+ } from "react";
664
+
665
+ // src/lib/createRequiredContext.ts
666
+ import { createContext, useContext } from "react";
667
+ var createRequiredContext = () => {
668
+ const Ctx = createContext(null);
669
+ const useCtx = () => {
670
+ const contextValue = useContext(Ctx);
671
+ if (contextValue === null) {
672
+ throw new Error("Context value is null");
673
+ }
674
+ return contextValue;
675
+ };
676
+ return [useCtx, Ctx.Provider, Ctx];
677
+ };
678
+
679
+ // src/Mcu.context.tsx
680
+ import { jsx } from "react/jsx-runtime";
681
+ var [useMcu, Provider, McuContext] = createRequiredContext();
682
+ var McuProvider = ({
683
+ styleId,
684
+ children,
685
+ ...configProps
686
+ }) => {
687
+ const [initials] = useState(() => configProps);
688
+ const [mcuConfig, setMcuConfig] = useState(initials);
689
+ const configKey = JSON.stringify(configProps);
690
+ React.useEffect(() => {
691
+ setMcuConfig(configProps);
692
+ }, [configKey]);
693
+ const { css, mergedColorsLight, mergedColorsDark, allPalettes } = useMemo(() => {
694
+ const { toCss, ...rest } = builder(mcuConfig.source, mcuConfig);
695
+ return { css: toCss(), ...rest };
696
+ }, [mcuConfig]);
697
+ useInsertionEffect(() => {
698
+ let tag = document.getElementById(styleId);
699
+ if (!tag) {
700
+ tag = document.createElement("style");
701
+ tag.id = styleId;
702
+ document.head.appendChild(tag);
703
+ }
704
+ tag.textContent = css;
705
+ }, [css, styleId]);
706
+ const getMcuColor = useCallback(
707
+ (colorName, theme) => {
708
+ const mergedColors = theme === "light" ? mergedColorsLight : mergedColorsDark;
709
+ const colorValue = mergedColors[colorName];
710
+ if (colorValue === void 0) {
711
+ throw new Error(`Unknown MCU token '${colorName}'`);
712
+ }
713
+ return hexFromArgb2(colorValue);
714
+ },
715
+ [mergedColorsDark, mergedColorsLight]
716
+ );
717
+ const value = useMemo(
718
+ () => ({
719
+ initials,
720
+ setMcuConfig,
721
+ getMcuColor,
722
+ allPalettes
723
+ }),
724
+ [getMcuColor, initials, allPalettes]
725
+ );
726
+ return /* @__PURE__ */ jsx(Provider, { value, children });
727
+ };
728
+
729
+ // src/Mcu.tsx
730
+ import { jsx as jsx2 } from "react/jsx-runtime";
731
+ var mcuStyleId = "mcu-styles";
732
+ var DEFAULT_COLOR_MATCH = false;
733
+ function Mcu({
734
+ source,
735
+ scheme = DEFAULT_SCHEME,
736
+ contrast = DEFAULT_CONTRAST,
737
+ primary,
738
+ secondary,
739
+ tertiary,
740
+ neutral,
741
+ neutralVariant,
742
+ error,
743
+ colorMatch = DEFAULT_COLOR_MATCH,
744
+ customColors = DEFAULT_CUSTOM_COLORS,
745
+ contrastAllColors = DEFAULT_CONTRAST_ALL_COLORS,
746
+ adaptiveShades = DEFAULT_ADAPTIVE_SHADES,
747
+ children
748
+ }) {
749
+ const config = useMemo2(
750
+ () => ({
751
+ source,
752
+ scheme,
753
+ contrast,
754
+ primary,
755
+ secondary,
756
+ tertiary,
757
+ neutral,
758
+ neutralVariant,
759
+ error,
760
+ colorMatch,
761
+ customColors,
762
+ // extras features
763
+ contrastAllColors,
764
+ adaptiveShades
765
+ }),
766
+ [
767
+ contrast,
768
+ customColors,
769
+ scheme,
770
+ source,
771
+ primary,
772
+ secondary,
773
+ tertiary,
774
+ neutral,
775
+ neutralVariant,
776
+ error,
777
+ colorMatch,
778
+ contrastAllColors,
779
+ adaptiveShades
780
+ ]
781
+ );
782
+ return /* @__PURE__ */ jsx2(McuProvider, { ...config, styleId: mcuStyleId, children });
783
+ }
791
784
  export {
792
- DEFAULT_ADAPTIVE_SHADES,
793
- DEFAULT_BLEND,
794
- DEFAULT_CONTRAST,
795
- DEFAULT_CONTRAST_ALL_COLORS,
796
- DEFAULT_SCHEME,
797
785
  Mcu,
798
786
  builder,
799
- schemeNames,
800
787
  useMcu
801
788
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-mcu",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "A React component library",
5
5
  "keywords": [
6
6
  "react",
package/src/cli.ts CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  DEFAULT_CONTRAST_ALL_COLORS,
21
21
  DEFAULT_SCHEME,
22
22
  schemeNames,
23
- } from "react-mcu";
23
+ } from "./lib/builder";
24
24
 
25
25
  const program = new Command();
26
26