react-mcu 1.1.0 → 1.1.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
@@ -28,9 +28,22 @@ type McuConfig = {
28
28
  * When true, stays true to input colors without harmonization.
29
29
  * When false (default), colors may be adjusted for better harmonization.
30
30
  * Corresponds to "Color match - Stay true to my color inputs" in Material Theme Builder.
31
+ *
32
+ * @default false
33
+ * @deprecated Not yet implemented. This prop is currently ignored.
31
34
  */
32
35
  colorMatch?: boolean;
33
- /** Array of custom colors to include in the generated palette */
36
+ /**
37
+ * Array of custom colors to include in the generated palette.
38
+ *
39
+ * @example
40
+ * ```ts
41
+ * customColors={[
42
+ * { name: "brand", hex: "#FF5733", blend: true },
43
+ * { name: "success", hex: "#28A745", blend: false }
44
+ * ]}
45
+ * ```
46
+ */
34
47
  customColors?: HexCustomColor[];
35
48
  };
36
49
  declare const schemesMap: {
package/dist/index.js CHANGED
@@ -3,8 +3,8 @@
3
3
  // src/Mcu.tsx
4
4
  import {
5
5
  argbFromHex,
6
- CorePalette,
7
- customColor,
6
+ Blend,
7
+ DynamicColor,
8
8
  DynamicScheme,
9
9
  Hct,
10
10
  hexFromArgb as hexFromArgb2,
@@ -270,22 +270,56 @@ function toRecord(arr, getEntry) {
270
270
  {}
271
271
  );
272
272
  }
273
- function mergeBaseAndCustomColors(scheme, customColors, sourceArgb) {
273
+ function getPalette(palettes, colorName) {
274
+ const palette = palettes[colorName];
275
+ if (!palette) {
276
+ throw new Error(
277
+ `Custom color palette not found for '${colorName}'. This is likely a bug in the implementation.`
278
+ );
279
+ }
280
+ return palette;
281
+ }
282
+ function mergeBaseAndCustomColors(scheme, customColors, colorPalettes) {
274
283
  const baseVars = toRecord(tokenNames, (tokenName) => {
275
284
  const dynamicColor = MaterialDynamicColors[tokenName];
276
285
  const argb = dynamicColor.getArgb(scheme);
277
286
  return [tokenName, argb];
278
287
  });
279
288
  const customVars = {};
280
- const isDark = scheme.isDark;
281
289
  customColors.forEach((color) => {
282
- const customColorGroup = customColor(sourceArgb, color);
283
- const colorGroup = isDark ? customColorGroup.dark : customColorGroup.light;
284
290
  const colorname = color.name;
285
- customVars[colorname] = colorGroup.color;
286
- customVars[`on${upperFirst(colorname)}`] = colorGroup.onColor;
287
- customVars[`${colorname}Container`] = colorGroup.colorContainer;
288
- customVars[`on${upperFirst(colorname)}Container`] = colorGroup.onColorContainer;
291
+ const colorDynamicColor = new DynamicColor(
292
+ colorname,
293
+ (s) => getPalette(colorPalettes, colorname),
294
+ (s) => s.isDark ? 80 : 40,
295
+ // Same as primary
296
+ false
297
+ );
298
+ const onColorDynamicColor = new DynamicColor(
299
+ `on${upperFirst(colorname)}`,
300
+ (s) => getPalette(colorPalettes, colorname),
301
+ (s) => s.isDark ? 20 : 100,
302
+ // Same as onPrimary
303
+ false
304
+ );
305
+ const containerDynamicColor = new DynamicColor(
306
+ `${colorname}Container`,
307
+ (s) => getPalette(colorPalettes, colorname),
308
+ (s) => s.isDark ? 30 : 90,
309
+ // Same as primaryContainer
310
+ false
311
+ );
312
+ const onContainerDynamicColor = new DynamicColor(
313
+ `on${upperFirst(colorname)}Container`,
314
+ (s) => getPalette(colorPalettes, colorname),
315
+ (s) => s.isDark ? 90 : 30,
316
+ // Same as onPrimaryContainer
317
+ false
318
+ );
319
+ customVars[colorname] = colorDynamicColor.getArgb(scheme);
320
+ customVars[`on${upperFirst(colorname)}`] = onColorDynamicColor.getArgb(scheme);
321
+ customVars[`${colorname}Container`] = containerDynamicColor.getArgb(scheme);
322
+ customVars[`on${upperFirst(colorname)}Container`] = onContainerDynamicColor.getArgb(scheme);
289
323
  });
290
324
  return { ...baseVars, ...customVars };
291
325
  }
@@ -300,6 +334,24 @@ var generateTonalPaletteVars = (paletteName, palette) => {
300
334
  return cssVar(`${paletteName}-${tone}`, color);
301
335
  }).join(" ");
302
336
  };
337
+ function createColorPalette(colorDef, baseScheme, effectiveSourceForHarmonization) {
338
+ const colorArgb = argbFromHex(colorDef.hex);
339
+ const harmonizedArgb = colorDef.blend ? Blend.harmonize(colorArgb, effectiveSourceForHarmonization) : colorArgb;
340
+ const hct = Hct.fromInt(harmonizedArgb);
341
+ let targetChroma;
342
+ if (colorDef.core && colorDef.chromaSource) {
343
+ if (colorDef.chromaSource === "neutral") {
344
+ targetChroma = baseScheme.neutralPalette.chroma;
345
+ } else if (colorDef.chromaSource === "neutralVariant") {
346
+ targetChroma = baseScheme.neutralVariantPalette.chroma;
347
+ } else {
348
+ targetChroma = baseScheme.primaryPalette.chroma;
349
+ }
350
+ } else {
351
+ targetChroma = baseScheme.primaryPalette.chroma;
352
+ }
353
+ return TonalPalette.fromHueAndChroma(hct.hue, targetChroma);
354
+ }
303
355
  var toCssVars = (mergedColors) => {
304
356
  return Object.entries(mergedColors).map(([name, value]) => cssVar(name, value)).join(" ");
305
357
  };
@@ -316,74 +368,122 @@ function generateCss({
316
368
  colorMatch = DEFAULT_COLOR_MATCH,
317
369
  customColors: hexCustomColors = DEFAULT_CUSTOM_COLORS
318
370
  }) {
319
- const hasCoreColors = primary ?? secondary ?? tertiary ?? neutral ?? neutralVariant ?? error;
320
371
  const sourceArgb = argbFromHex(hexSource);
372
+ const effectiveSource = primary || hexSource;
373
+ const effectiveSourceArgb = argbFromHex(effectiveSource);
374
+ const effectiveSourceForHarmonization = primary ? argbFromHex(primary) : sourceArgb;
375
+ const SchemeClass = schemesMap[scheme];
376
+ const primaryHct = Hct.fromInt(effectiveSourceArgb);
377
+ const baseScheme = new SchemeClass(primaryHct, false, contrast);
378
+ const allColors = [
379
+ // Core colors (hex may be undefined)
380
+ {
381
+ name: "primary",
382
+ hex: primary,
383
+ core: true,
384
+ chromaSource: "primary"
385
+ },
386
+ {
387
+ name: "secondary",
388
+ hex: secondary,
389
+ core: true,
390
+ chromaSource: "primary"
391
+ },
392
+ {
393
+ name: "tertiary",
394
+ hex: tertiary,
395
+ core: true,
396
+ chromaSource: "primary"
397
+ },
398
+ { name: "error", hex: error, core: true, chromaSource: "primary" },
399
+ {
400
+ name: "neutral",
401
+ hex: neutral,
402
+ core: true,
403
+ chromaSource: "neutral"
404
+ },
405
+ {
406
+ name: "neutralVariant",
407
+ hex: neutralVariant,
408
+ core: true,
409
+ chromaSource: "neutralVariant"
410
+ },
411
+ //
412
+ // Custom colors
413
+ //
414
+ ...hexCustomColors.map((c) => ({
415
+ name: c.name,
416
+ hex: c.hex,
417
+ blend: c.blend,
418
+ core: false
419
+ }))
420
+ ];
421
+ const definedColors = allColors.filter(
422
+ (c) => c.hex !== void 0
423
+ );
424
+ const colorPalettes = Object.fromEntries(
425
+ definedColors.map((colorDef) => [
426
+ colorDef.name,
427
+ createColorPalette(colorDef, baseScheme, effectiveSourceForHarmonization)
428
+ ])
429
+ );
321
430
  const createSchemes = (baseConfig) => [
322
431
  new DynamicScheme({ ...baseConfig, isDark: false }),
323
432
  new DynamicScheme({ ...baseConfig, isDark: true })
324
433
  ];
325
- let lightScheme;
326
- let darkScheme;
327
- let corePalette;
328
- if (hasCoreColors) {
329
- const coreColorsArgb = {
330
- primary: primary ? argbFromHex(primary) : sourceArgb,
331
- secondary: secondary ? argbFromHex(secondary) : void 0,
332
- tertiary: tertiary ? argbFromHex(tertiary) : void 0,
333
- neutral: neutral ? argbFromHex(neutral) : void 0,
334
- neutralVariant: neutralVariant ? argbFromHex(neutralVariant) : void 0,
335
- error: error ? argbFromHex(error) : void 0
336
- };
337
- corePalette = colorMatch ? CorePalette.fromColors(coreColorsArgb) : CorePalette.contentFromColors(coreColorsArgb);
338
- const variant = schemeToVariant[scheme];
339
- [lightScheme, darkScheme] = createSchemes({
340
- sourceColorArgb: sourceArgb,
341
- variant,
342
- contrastLevel: contrast,
343
- primaryPalette: corePalette.a1,
344
- secondaryPalette: corePalette.a2,
345
- tertiaryPalette: corePalette.a3,
346
- neutralPalette: corePalette.n1,
347
- neutralVariantPalette: corePalette.n2
348
- });
349
- } else {
350
- const SchemeClass = schemesMap[scheme];
351
- const hct = Hct.fromInt(sourceArgb);
352
- lightScheme = new SchemeClass(hct, false, contrast);
353
- darkScheme = new SchemeClass(hct, true, contrast);
354
- corePalette = CorePalette.of(sourceArgb);
434
+ const variant = schemeToVariant[scheme];
435
+ const [lightScheme, darkScheme] = createSchemes({
436
+ sourceColorArgb: effectiveSourceArgb,
437
+ variant,
438
+ contrastLevel: contrast,
439
+ primaryPalette: colorPalettes["primary"] || baseScheme.primaryPalette,
440
+ secondaryPalette: colorPalettes["secondary"] || baseScheme.secondaryPalette,
441
+ tertiaryPalette: colorPalettes["tertiary"] || baseScheme.tertiaryPalette,
442
+ neutralPalette: colorPalettes["neutral"] || baseScheme.neutralPalette,
443
+ neutralVariantPalette: colorPalettes["neutralVariant"] || baseScheme.neutralVariantPalette
444
+ });
445
+ const errorPalette = colorPalettes["error"];
446
+ if (errorPalette) {
447
+ lightScheme.errorPalette = errorPalette;
448
+ darkScheme.errorPalette = errorPalette;
355
449
  }
356
- const customColors = hexCustomColors.map(({ hex, ...rest }) => ({
357
- ...rest,
358
- value: argbFromHex(hex)
450
+ const customColors = definedColors.filter((c) => !c.core).map((c) => ({
451
+ name: c.name,
452
+ blend: c.blend ?? false,
453
+ value: argbFromHex(c.hex)
359
454
  }));
360
455
  const mergedColorsLight = mergeBaseAndCustomColors(
361
456
  lightScheme,
362
457
  customColors,
363
- sourceArgb
458
+ colorPalettes
364
459
  );
365
460
  const mergedColorsDark = mergeBaseAndCustomColors(
366
461
  darkScheme,
367
462
  customColors,
368
- sourceArgb
463
+ colorPalettes
369
464
  );
370
465
  const lightVars = toCssVars(mergedColorsLight);
371
466
  const darkVars = toCssVars(mergedColorsDark);
372
- const coreColorsTonalVars = [
373
- generateTonalPaletteVars("primary", corePalette.a1),
374
- generateTonalPaletteVars("secondary", corePalette.a2),
375
- generateTonalPaletteVars("tertiary", corePalette.a3),
376
- generateTonalPaletteVars("neutral", corePalette.n1),
377
- generateTonalPaletteVars("neutral-variant", corePalette.n2),
378
- generateTonalPaletteVars("error", corePalette.error)
467
+ const allTonalVars = [
468
+ // Core colors from the scheme
469
+ generateTonalPaletteVars("primary", lightScheme.primaryPalette),
470
+ generateTonalPaletteVars("secondary", lightScheme.secondaryPalette),
471
+ generateTonalPaletteVars("tertiary", lightScheme.tertiaryPalette),
472
+ generateTonalPaletteVars("error", lightScheme.errorPalette),
473
+ generateTonalPaletteVars("neutral", lightScheme.neutralPalette),
474
+ generateTonalPaletteVars(
475
+ "neutral-variant",
476
+ lightScheme.neutralVariantPalette
477
+ ),
478
+ // Custom colors from our unified palette map
479
+ ...customColors.map((customColorObj) => {
480
+ const palette = getPalette(colorPalettes, customColorObj.name);
481
+ return generateTonalPaletteVars(kebabCase(customColorObj.name), palette);
482
+ })
379
483
  ].join(" ");
380
- const customColorTonalVars = customColors.map((customColorObj) => {
381
- const palette = TonalPalette.fromInt(customColorObj.value);
382
- return generateTonalPaletteVars(kebabCase(customColorObj.name), palette);
383
- }).join(" ");
384
484
  return {
385
485
  css: `
386
- :root { ${lightVars} ${coreColorsTonalVars} ${customColorTonalVars} }
486
+ :root { ${lightVars} ${allTonalVars} }
387
487
  .dark { ${darkVars} }
388
488
  `,
389
489
  mergedColorsLight,
package/dist/tailwind.css CHANGED
@@ -164,23 +164,4 @@
164
164
  --color-myCustomColor2-800: var(--mcu-my-custom-color-2-20);
165
165
  --color-myCustomColor2-900: var(--mcu-my-custom-color-2-10);
166
166
  --color-myCustomColor2-950: var(--mcu-my-custom-color-2-5);
167
-
168
- --color-myCustomColor3: var(--mcu-my-custom-color-3);
169
- --color-on-myCustomColor3: var(--mcu-on-my-custom-color-3);
170
- --color-myCustomColor3-container: var(--mcu-my-custom-color-3-container);
171
- --color-on-myCustomColor3-container: var(
172
- --mcu-on-my-custom-color-3-container
173
- );
174
- /* Shades */
175
- --color-myCustomColor3-50: var(--mcu-my-custom-color-3-95);
176
- --color-myCustomColor3-100: var(--mcu-my-custom-color-3-90);
177
- --color-myCustomColor3-200: var(--mcu-my-custom-color-3-80);
178
- --color-myCustomColor3-300: var(--mcu-my-custom-color-3-70);
179
- --color-myCustomColor3-400: var(--mcu-my-custom-color-3-60);
180
- --color-myCustomColor3-500: var(--mcu-my-custom-color-3-50);
181
- --color-myCustomColor3-600: var(--mcu-my-custom-color-3-40);
182
- --color-myCustomColor3-700: var(--mcu-my-custom-color-3-30);
183
- --color-myCustomColor3-800: var(--mcu-my-custom-color-3-20);
184
- --color-myCustomColor3-900: var(--mcu-my-custom-color-3-10);
185
- --color-myCustomColor3-950: var(--mcu-my-custom-color-3-5);
186
167
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-mcu",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "A React component library",
5
5
  "keywords": [
6
6
  "react",
@@ -66,14 +66,14 @@
66
66
  },
67
67
  "scripts": {
68
68
  "build": "tsup",
69
- "ci": "pnpm run build && pnpm run check-format && pnpm run check-exports && pnpm run lint && pnpm run test",
70
- "lint": "tsc",
69
+ "lgtm": "pnpm run build && pnpm run check-format && pnpm run check-exports && pnpm run typecheck && pnpm run test",
70
+ "typecheck": "tsc",
71
71
  "test": "vitest run",
72
72
  "format": "prettier --write .",
73
73
  "check-format": "prettier --check .",
74
74
  "check-exports": "attw --pack . --ignore-rules cjs-resolves-to-esm no-resolution",
75
75
  "release": "pnpm run build && changeset publish",
76
- "local-release": "pnpm run ci && changeset version && changeset publish",
76
+ "local-release": "pnpm run lgtm && changeset version && changeset publish",
77
77
  "storybook": "storybook dev -p 6006",
78
78
  "build-storybook": "storybook build",
79
79
  "chromatic": "chromatic --project-token $CHROMATIC_PROJECT_TOKEN",
package/src/tailwind.css CHANGED
@@ -164,23 +164,4 @@
164
164
  --color-myCustomColor2-800: var(--mcu-my-custom-color-2-20);
165
165
  --color-myCustomColor2-900: var(--mcu-my-custom-color-2-10);
166
166
  --color-myCustomColor2-950: var(--mcu-my-custom-color-2-5);
167
-
168
- --color-myCustomColor3: var(--mcu-my-custom-color-3);
169
- --color-on-myCustomColor3: var(--mcu-on-my-custom-color-3);
170
- --color-myCustomColor3-container: var(--mcu-my-custom-color-3-container);
171
- --color-on-myCustomColor3-container: var(
172
- --mcu-on-my-custom-color-3-container
173
- );
174
- /* Shades */
175
- --color-myCustomColor3-50: var(--mcu-my-custom-color-3-95);
176
- --color-myCustomColor3-100: var(--mcu-my-custom-color-3-90);
177
- --color-myCustomColor3-200: var(--mcu-my-custom-color-3-80);
178
- --color-myCustomColor3-300: var(--mcu-my-custom-color-3-70);
179
- --color-myCustomColor3-400: var(--mcu-my-custom-color-3-60);
180
- --color-myCustomColor3-500: var(--mcu-my-custom-color-3-50);
181
- --color-myCustomColor3-600: var(--mcu-my-custom-color-3-40);
182
- --color-myCustomColor3-700: var(--mcu-my-custom-color-3-30);
183
- --color-myCustomColor3-800: var(--mcu-my-custom-color-3-20);
184
- --color-myCustomColor3-900: var(--mcu-my-custom-color-3-10);
185
- --color-myCustomColor3-950: var(--mcu-my-custom-color-3-5);
186
167
  }