reshaped 3.6.0-canary.3 → 3.6.0-canary.5

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.
Files changed (150) hide show
  1. package/CHANGELOG.md +3 -0
  2. package/dist/bundle.css +1 -1
  3. package/dist/bundle.d.ts +1 -0
  4. package/dist/bundle.js +5 -5
  5. package/dist/cjs/cli/theming/index.d.ts +3 -3
  6. package/dist/cjs/cli/theming/index.js +2 -2
  7. package/dist/cjs/cli/theming/tailwind.d.ts +2 -3
  8. package/dist/cjs/themes/_generator/definitions/slate.js +1 -1
  9. package/dist/cjs/themes/_generator/tokens/color/color.transforms.d.ts +2 -2
  10. package/dist/cjs/themes/_generator/tokens/color/color.transforms.js +29 -7
  11. package/dist/cjs/themes/_generator/tokens/color/color.types.d.ts +28 -3
  12. package/dist/cjs/themes/_generator/tokens/color/utilities/a11y.d.ts +20 -0
  13. package/dist/cjs/themes/_generator/tokens/color/utilities/a11y.js +64 -0
  14. package/dist/cjs/themes/_generator/tokens/color/utilities/convert.d.ts +11 -0
  15. package/dist/cjs/themes/_generator/tokens/color/utilities/convert.js +33 -0
  16. package/dist/cjs/themes/_generator/tokens/color/utilities/generateColors.d.ts +4 -0
  17. package/dist/cjs/themes/_generator/tokens/color/utilities/generateColors.js +152 -0
  18. package/dist/cjs/themes/_generator/tokens/color/utilities/generateMetaColors.d.ts +4 -0
  19. package/dist/cjs/themes/_generator/tokens/color/utilities/generateMetaColors.js +68 -0
  20. package/dist/cjs/themes/_generator/tokens/color/utilities/tests/a11y.test.js +38 -0
  21. package/dist/cjs/themes/_generator/tokens/color/utilities/tests/convert.test.d.ts +1 -0
  22. package/dist/cjs/themes/_generator/tokens/color/utilities/tests/convert.test.js +48 -0
  23. package/dist/cjs/themes/_generator/tokens/color/utilities/tests/generateColors.test.d.ts +1 -0
  24. package/dist/cjs/themes/_generator/tokens/color/utilities/tests/generateColors.test.js +21 -0
  25. package/dist/{themes/_generator/utilities → cjs/themes/_generator/tokens}/css.d.ts +1 -1
  26. package/dist/cjs/themes/_generator/tokens/duration/duration.transforms.d.ts +2 -2
  27. package/dist/cjs/themes/_generator/tokens/duration/duration.transforms.js +2 -2
  28. package/dist/cjs/themes/_generator/tokens/easing/easing.transforms.d.ts +2 -2
  29. package/dist/cjs/themes/_generator/tokens/easing/easing.transforms.js +2 -2
  30. package/dist/cjs/themes/_generator/tokens/font/font.transforms.d.ts +2 -2
  31. package/dist/cjs/themes/_generator/tokens/font/font.transforms.js +3 -3
  32. package/dist/cjs/themes/_generator/tokens/fontFamily/fontFamily.transforms.d.ts +2 -2
  33. package/dist/cjs/themes/_generator/tokens/fontFamily/fontFamily.transforms.js +2 -2
  34. package/dist/cjs/themes/_generator/tokens/fontWeight/fontWeight.transforms.d.ts +2 -2
  35. package/dist/cjs/themes/_generator/tokens/fontWeight/fontWeight.transforms.js +2 -2
  36. package/dist/cjs/themes/_generator/tokens/radius/radius.transforms.d.ts +2 -2
  37. package/dist/cjs/themes/_generator/tokens/radius/radius.transforms.js +2 -2
  38. package/dist/cjs/themes/_generator/tokens/shadow/shadow.transforms.d.ts +2 -2
  39. package/dist/cjs/themes/_generator/tokens/shadow/shadow.transforms.js +4 -4
  40. package/dist/cjs/themes/_generator/tokens/types.d.ts +19 -17
  41. package/dist/cjs/themes/_generator/tokens/unit/unit.transforms.d.ts +2 -2
  42. package/dist/cjs/themes/_generator/tokens/unit/unit.transforms.js +2 -2
  43. package/dist/cjs/themes/_generator/tokens/unit/utilities/generate.d.ts +5 -0
  44. package/dist/cjs/themes/_generator/{utilities/generateUnits.js → tokens/unit/utilities/generate.js} +5 -4
  45. package/dist/cjs/themes/_generator/tokens/viewport/viewport.transforms.d.ts +2 -2
  46. package/dist/cjs/themes/_generator/tokens/viewport/viewport.transforms.js +2 -2
  47. package/dist/cjs/themes/_generator/transform.d.ts +3 -2
  48. package/dist/cjs/themes/_generator/transform.js +39 -11
  49. package/dist/cjs/themes/_generator/types.d.ts +4 -8
  50. package/dist/cjs/themes/_generator/utilities/mergeDefinitions.d.ts +1 -3
  51. package/dist/cjs/themes/_generator/utilities/mergeDefinitions.js +6 -4
  52. package/dist/cjs/themes/figma/theme.css +1 -1
  53. package/dist/cjs/themes/fragments/twitter/theme.css +1 -1
  54. package/dist/cjs/themes/index.d.ts +4 -11
  55. package/dist/cjs/themes/index.js +2 -5
  56. package/dist/cjs/themes/reshaped/theme.css +1 -1
  57. package/dist/cjs/themes/slate/theme.css +1 -1
  58. package/dist/cjs/types/config.d.ts +11 -7
  59. package/dist/cli/theming/index.d.ts +3 -3
  60. package/dist/cli/theming/index.js +2 -2
  61. package/dist/cli/theming/tailwind.d.ts +2 -3
  62. package/dist/components/Avatar/Avatar.module.css +1 -1
  63. package/dist/components/FormControl/FormControl.context.d.ts +1 -1
  64. package/dist/components/Reshaped/Reshaped.js +2 -2
  65. package/dist/components/Reshaped/Reshaped.types.d.ts +1 -0
  66. package/dist/components/Reshaped/tests/Reshaped.stories.d.ts +1 -0
  67. package/dist/components/Reshaped/tests/Reshaped.stories.js +20 -0
  68. package/dist/components/Theme/GlobalColorMode.js +6 -11
  69. package/dist/components/Theme/Theme.types.d.ts +1 -0
  70. package/dist/components/Theme/index.d.ts +1 -1
  71. package/dist/index.d.ts +1 -0
  72. package/dist/tests/ThemesPlayground.d.ts +2 -0
  73. package/dist/tests/ThemesPlayground.js +90 -0
  74. package/dist/tests/themes.stories.d.ts +16 -0
  75. package/dist/{themes/_generator/tests → tests}/themes.stories.js +74 -15
  76. package/dist/themes/_generator/definitions/slate.js +1 -1
  77. package/dist/themes/_generator/tokens/color/color.transforms.d.ts +2 -2
  78. package/dist/themes/_generator/tokens/color/color.transforms.js +29 -7
  79. package/dist/themes/_generator/tokens/color/color.types.d.ts +28 -3
  80. package/dist/themes/_generator/tokens/color/utilities/a11y.d.ts +20 -0
  81. package/dist/themes/_generator/tokens/color/utilities/a11y.js +58 -0
  82. package/dist/themes/_generator/tokens/color/utilities/convert.d.ts +11 -0
  83. package/dist/themes/_generator/tokens/color/utilities/convert.js +27 -0
  84. package/dist/themes/_generator/tokens/color/utilities/generateColors.d.ts +4 -0
  85. package/dist/themes/_generator/tokens/color/utilities/generateColors.js +150 -0
  86. package/dist/themes/_generator/tokens/color/utilities/generateMetaColors.d.ts +4 -0
  87. package/dist/themes/_generator/tokens/color/utilities/generateMetaColors.js +66 -0
  88. package/dist/{cjs/themes/_generator/utilities → themes/_generator/tokens}/css.d.ts +1 -1
  89. package/dist/themes/_generator/tokens/duration/duration.transforms.d.ts +2 -2
  90. package/dist/themes/_generator/tokens/duration/duration.transforms.js +2 -2
  91. package/dist/themes/_generator/tokens/easing/easing.transforms.d.ts +2 -2
  92. package/dist/themes/_generator/tokens/easing/easing.transforms.js +2 -2
  93. package/dist/themes/_generator/tokens/font/font.transforms.d.ts +2 -2
  94. package/dist/themes/_generator/tokens/font/font.transforms.js +3 -3
  95. package/dist/themes/_generator/tokens/fontFamily/fontFamily.transforms.d.ts +2 -2
  96. package/dist/themes/_generator/tokens/fontFamily/fontFamily.transforms.js +2 -2
  97. package/dist/themes/_generator/tokens/fontWeight/fontWeight.transforms.d.ts +2 -2
  98. package/dist/themes/_generator/tokens/fontWeight/fontWeight.transforms.js +2 -2
  99. package/dist/themes/_generator/tokens/radius/radius.transforms.d.ts +2 -2
  100. package/dist/themes/_generator/tokens/radius/radius.transforms.js +2 -2
  101. package/dist/themes/_generator/tokens/shadow/shadow.transforms.d.ts +2 -2
  102. package/dist/themes/_generator/tokens/shadow/shadow.transforms.js +4 -4
  103. package/dist/themes/_generator/tokens/types.d.ts +19 -17
  104. package/dist/themes/_generator/tokens/unit/unit.transforms.d.ts +2 -2
  105. package/dist/themes/_generator/tokens/unit/unit.transforms.js +2 -2
  106. package/dist/themes/_generator/tokens/unit/utilities/generate.d.ts +5 -0
  107. package/dist/themes/_generator/{utilities/generateUnits.js → tokens/unit/utilities/generate.js} +4 -5
  108. package/dist/themes/_generator/tokens/viewport/viewport.transforms.d.ts +2 -2
  109. package/dist/themes/_generator/tokens/viewport/viewport.transforms.js +2 -2
  110. package/dist/themes/_generator/transform.d.ts +3 -2
  111. package/dist/themes/_generator/transform.js +39 -11
  112. package/dist/themes/_generator/types.d.ts +4 -8
  113. package/dist/themes/_generator/utilities/mergeDefinitions.d.ts +1 -3
  114. package/dist/themes/_generator/utilities/mergeDefinitions.js +6 -4
  115. package/dist/themes/figma/theme.css +1 -1
  116. package/dist/themes/fragments/twitter/theme.css +1 -1
  117. package/dist/themes/index.d.ts +4 -11
  118. package/dist/themes/index.js +1 -4
  119. package/dist/themes/reshaped/theme.css +1 -1
  120. package/dist/themes/slate/theme.css +1 -1
  121. package/dist/types/config.d.ts +11 -7
  122. package/dist/types/global.d.ts +3 -0
  123. package/package.json +11 -11
  124. package/dist/cjs/themes/_generator/utilities/color.d.ts +0 -105
  125. package/dist/cjs/themes/_generator/utilities/color.js +0 -409
  126. package/dist/cjs/themes/_generator/utilities/generateBackgroundColors.d.ts +0 -4
  127. package/dist/cjs/themes/_generator/utilities/generateBackgroundColors.js +0 -59
  128. package/dist/cjs/themes/_generator/utilities/generateColors.d.ts +0 -11
  129. package/dist/cjs/themes/_generator/utilities/generateColors.js +0 -178
  130. package/dist/cjs/themes/_generator/utilities/generateUnits.d.ts +0 -4
  131. package/dist/cjs/themes/_generator/utilities/mergeDeep.d.ts +0 -5
  132. package/dist/cjs/themes/_generator/utilities/mergeDeep.js +0 -24
  133. package/dist/cjs/themes/_generator/utilities/resolveTokenReference.d.ts +0 -3
  134. package/dist/cjs/themes/_generator/utilities/resolveTokenReference.js +0 -18
  135. package/dist/cjs/themes/_generator/utilities/tests/color.test.js +0 -81
  136. package/dist/themes/_generator/tests/themes.stories.d.ts +0 -16
  137. package/dist/themes/_generator/utilities/color.d.ts +0 -105
  138. package/dist/themes/_generator/utilities/color.js +0 -377
  139. package/dist/themes/_generator/utilities/generateBackgroundColors.d.ts +0 -4
  140. package/dist/themes/_generator/utilities/generateBackgroundColors.js +0 -57
  141. package/dist/themes/_generator/utilities/generateColors.d.ts +0 -11
  142. package/dist/themes/_generator/utilities/generateColors.js +0 -176
  143. package/dist/themes/_generator/utilities/generateUnits.d.ts +0 -4
  144. package/dist/themes/_generator/utilities/mergeDeep.d.ts +0 -5
  145. package/dist/themes/_generator/utilities/mergeDeep.js +0 -22
  146. package/dist/themes/_generator/utilities/resolveTokenReference.d.ts +0 -3
  147. package/dist/themes/_generator/utilities/resolveTokenReference.js +0 -16
  148. /package/dist/cjs/themes/_generator/{utilities/tests/color.test.d.ts → tokens/color/utilities/tests/a11y.test.d.ts} +0 -0
  149. /package/dist/cjs/themes/_generator/{utilities → tokens}/css.js +0 -0
  150. /package/dist/themes/_generator/{utilities → tokens}/css.js +0 -0
@@ -1,4 +1,4 @@
1
- import type { PartialThemeDefinition } from "../../themes/_generator/tokens/types";
1
+ import type { PassedThemeDefinition } from "../../themes/_generator/tokens/types";
2
2
  import type * as T from "../../themes/_generator/types";
3
- export declare const addThemeFragment: (name: string, definition: PartialThemeDefinition, options: T.CLIOptions) => void;
4
- export declare const addTheme: (name: string, definition: PartialThemeDefinition, options: T.CLIOptions) => void;
3
+ export declare const addThemeFragment: (name: string, definition: PassedThemeDefinition, options: T.CLIOptions) => void;
4
+ export declare const addTheme: (name: string, definition: PassedThemeDefinition, options: T.CLIOptions) => void;
@@ -23,8 +23,8 @@ const transformDefinition = (name, definition, options) => {
23
23
  const themeJsonPath = path_1.default.resolve(themeFolderPath, "theme.json");
24
24
  fs_1.default.mkdirSync(themeFolderPath, { recursive: true });
25
25
  fs_1.default.writeFileSync(themePath, code.variables);
26
- fs_1.default.writeFileSync(themeJsonPath, JSON.stringify(definition));
27
- fs_1.default.writeFileSync(twPath, (0, tailwind_1.transformToTailwind)(definition));
26
+ fs_1.default.writeFileSync(themeJsonPath, JSON.stringify(code.theme));
27
+ fs_1.default.writeFileSync(twPath, (0, tailwind_1.transformToTailwind)(code.theme));
28
28
  if (code.media)
29
29
  fs_1.default.writeFileSync(themeMediaPath, code.media);
30
30
  const logOutput = `Compiled ${chalk_1.default.bold(name)} theme${isFragment ? " fragment" : ""}`;
@@ -1,3 +1,2 @@
1
- import type { FullThemeDefinition } from "../../themes/_generator/tokens/types";
2
- import type * as T from "../../themes/_generator/types";
3
- export declare const transformToTailwind: (theme?: T.PartialDeep<FullThemeDefinition>) => string;
1
+ import type { GeneratedThemeDefinition } from "../../themes/_generator/tokens/types";
2
+ export declare const transformToTailwind: (theme?: GeneratedThemeDefinition) => string;
@@ -4,7 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const reshaped_1 = __importDefault(require("./reshaped"));
7
- const generateColors_1 = __importDefault(require("../utilities/generateColors"));
7
+ const generateColors_1 = __importDefault(require("../tokens/color/utilities/generateColors"));
8
8
  const theme = {
9
9
  ...reshaped_1.default,
10
10
  color: (0, generateColors_1.default)(),
@@ -1,4 +1,4 @@
1
1
  import type * as T from "./color.types";
2
2
  import type { Transformer } from "../types";
3
- declare const transformedToken: Transformer<T.Token>;
4
- export default transformedToken;
3
+ declare const transformToken: Transformer<T.Token>;
4
+ export default transformToken;
@@ -1,20 +1,42 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const transformedToken = (name, token) => {
4
- const hasDarkMode = !!token.hexDark;
5
- const defaultMode = hasDarkMode ? "light" : undefined;
3
+ const transformTokenForMode = (args) => {
4
+ const { hex, oklch } = args;
5
+ if (oklch) {
6
+ const components = `${Number(oklch.l.toFixed(4))} ${Number(oklch.c.toFixed(4))} ${Number(oklch.h?.toFixed(4) || 0)}`;
7
+ const alphaSuffix = oklch?.alpha === undefined ? "" : ` / ${Number(oklch.alpha.toFixed(4))}`;
8
+ return `oklch(${components}${alphaSuffix})`;
9
+ }
10
+ if (hex)
11
+ return hex;
12
+ throw new Error(`[Reshaped] ${JSON.stringify(args)} is missing a color value`);
13
+ };
14
+ const transformToken = (name, token) => {
15
+ const { hex, hexDark, oklch, oklchDark } = token;
16
+ // Apply color to both modes if dark mode is not available
17
+ const hasDark = !!hexDark || !!oklchDark;
18
+ const value = transformTokenForMode({ oklch, hex });
19
+ const darkValue = hasDark ? transformTokenForMode({ oklch: oklchDark, hex: hexDark }) : undefined;
20
+ const separateModes = hasDark && value !== darkValue;
21
+ const defaultMode = separateModes ? "light" : undefined;
6
22
  const result = [
7
- { name, tokenType: "color", type: "variable", value: token.hex, mode: defaultMode },
23
+ {
24
+ name,
25
+ tokenType: "color",
26
+ type: "variable",
27
+ value,
28
+ mode: defaultMode,
29
+ },
8
30
  ];
9
- if (token.hexDark) {
31
+ if (darkValue && separateModes) {
10
32
  result.push({
11
33
  name,
12
34
  tokenType: "color",
13
35
  type: "variable",
14
- value: token.hexDark,
36
+ value: darkValue,
15
37
  mode: "dark",
16
38
  });
17
39
  }
18
40
  return result;
19
41
  };
20
- exports.default = transformedToken;
42
+ exports.default = transformToken;
@@ -1,7 +1,32 @@
1
+ import type { Oklch } from "culori/fn";
2
+ export type Hue = "primary" | "positive" | "critical" | "warning" | "neutral" | "brand";
1
3
  export type Name = "foregroundNeutral" | "foregroundNeutralFaded" | "foregroundDisabled" | "foregroundPrimary" | "foregroundCritical" | "foregroundWarning" | "foregroundPositive" | "borderNeutral" | "borderNeutralFaded" | "borderDisabled" | "borderPrimary" | "borderPrimaryFaded" | "borderCritical" | "borderCriticalFaded" | "borderWarning" | "borderWarningFaded" | "borderPositive" | "borderPositiveFaded" | "backgroundNeutral" | "backgroundNeutralFaded" | "backgroundDisabled" | "backgroundDisabledFaded" | "backgroundPrimary" | "backgroundPrimaryFaded" | "backgroundCritical" | "backgroundCriticalFaded" | "backgroundWarning" | "backgroundWarningFaded" | "backgroundPositive" | "backgroundPositiveFaded" | "backgroundPage" | "backgroundPageFaded" | "backgroundElevationBase" | "backgroundElevationRaised" | "backgroundElevationOverlay" | "brand" | "white" | "black";
2
4
  export type GeneratedOnName = "onBackgroundNeutral" | "onBackgroundPrimary" | "onBackgroundPositive" | "onBackgroundWarning" | "onBackgroundCritical";
3
5
  export type GeneratedRGBName = "rgbBackgroundNeutral" | "rgbBackgroundNeutralFaded" | "rgbBackgroundDisabled" | "rgbBackgroundDisabledFaded" | "rgbBackgroundPrimary" | "rgbBackgroundPrimaryFaded" | "rgbBackgroundCritical" | "rgbBackgroundCriticalFaded" | "rgbBackgroundWarning" | "rgbBackgroundWarningFaded" | "rgbBackgroundPositive" | "rgbBackgroundPositiveFaded" | "rgbBackgroundPage" | "rgbBackgroundPageFaded" | "rgbBackgroundElevationBase" | "rgbBackgroundElevationRaised" | "rgbBackgroundElevationOverlay";
4
- export type Token = {
5
- hex: string;
6
- hexDark?: string;
6
+ export type RgbColor = {
7
+ r: number;
8
+ g: number;
9
+ b: number;
10
+ };
11
+ export type OklchColor = Omit<Oklch, "mode">;
12
+ export type HexColor = string;
13
+ export type ColorValue = HexColor | Token;
14
+ export type OklchOnlyToken = {
15
+ oklch: OklchColor;
16
+ oklchDark?: OklchColor;
17
+ };
18
+ export type OklchToken = OklchOnlyToken & {
19
+ hex?: never;
20
+ hexDark?: never;
21
+ };
22
+ export type HexToken = {
23
+ hex: HexColor;
24
+ hexDark?: HexColor;
25
+ oklch?: never;
26
+ oklchDark?: never;
27
+ };
28
+ export type Token = OklchToken | HexToken;
29
+ export type InternalToken = {
30
+ oklch: Oklch;
31
+ oklchDark?: Oklch;
7
32
  };
@@ -0,0 +1,20 @@
1
+ import { type Oklch } from "culori/fn";
2
+ export declare const getOnColorWCAG: (args: {
3
+ bgColor: Oklch;
4
+ lightColor: Oklch;
5
+ darkColor: Oklch;
6
+ }) => Oklch;
7
+ export declare const getOnColorAPCA: (args: {
8
+ bgColor: Oklch;
9
+ lightColor: Oklch;
10
+ darkColor: Oklch;
11
+ }) => Oklch;
12
+ /**
13
+ * General
14
+ */
15
+ export declare const getOnColor: (args: {
16
+ bgColor: Oklch;
17
+ lightColor: Oklch;
18
+ darkColor: Oklch;
19
+ algorithm?: "wcag" | "apca";
20
+ }) => Oklch;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getOnColor = exports.getOnColorAPCA = exports.getOnColorWCAG = void 0;
4
+ const convert_1 = require("./convert");
5
+ /**
6
+ * WCAG
7
+ */
8
+ const RED = 0.2126;
9
+ const GREEN = 0.7152;
10
+ const BLUE = 0.0722;
11
+ const GAMMA = 2.4;
12
+ function luminanceWCAG(r, g, b) {
13
+ const a = [r, g, b].map((v) => {
14
+ return v <= 0.03928 ? v / 12.92 : Math.pow((v + 0.055) / 1.055, GAMMA);
15
+ });
16
+ return a[0] * RED + a[1] * GREEN + a[2] * BLUE;
17
+ }
18
+ function contrastWCAG(rgb1, rgb2) {
19
+ const lum1 = luminanceWCAG(...rgb1);
20
+ const lum2 = luminanceWCAG(...rgb2);
21
+ const brightest = Math.max(lum1, lum2);
22
+ const darkest = Math.min(lum1, lum2);
23
+ return (brightest + 0.05) / (darkest + 0.05);
24
+ }
25
+ const getOnColorWCAG = (args) => {
26
+ const { bgColor, lightColor, darkColor } = args;
27
+ const bgRgb = (0, convert_1.oklchToRgb)(bgColor);
28
+ const lightRgb = (0, convert_1.oklchToRgb)(lightColor);
29
+ return contrastWCAG([bgRgb.r, bgRgb.g, bgRgb.b], [lightRgb.r, lightRgb.g, lightRgb.b]) > 4.5
30
+ ? lightColor
31
+ : darkColor;
32
+ };
33
+ exports.getOnColorWCAG = getOnColorWCAG;
34
+ /**
35
+ * APCA
36
+ */
37
+ function luminanceAPCA(oklch) {
38
+ const { r, g, b } = (0, convert_1.oklchToRgb)(oklch);
39
+ return (0.2126 * Math.pow(r / 255, 2.2) +
40
+ 0.7152 * Math.pow(g / 255, 2.2) +
41
+ 0.0722 * Math.pow(b / 255, 2.2));
42
+ }
43
+ const getOnColorAPCA = (args) => {
44
+ const { bgColor, lightColor, darkColor } = args;
45
+ const backgroundLuminance = luminanceAPCA(bgColor);
46
+ const lightLuminance = luminanceAPCA(lightColor);
47
+ const darkLuminance = luminanceAPCA(darkColor);
48
+ const contrastWithLight = Math.abs(lightLuminance - backgroundLuminance);
49
+ const contrastWithDark = Math.abs(darkLuminance - backgroundLuminance);
50
+ return contrastWithLight > contrastWithDark ? lightColor : darkColor;
51
+ };
52
+ exports.getOnColorAPCA = getOnColorAPCA;
53
+ /**
54
+ * General
55
+ */
56
+ const getOnColor = (args) => {
57
+ if (args.algorithm === "apca") {
58
+ return (0, exports.getOnColorAPCA)(args);
59
+ }
60
+ else {
61
+ return (0, exports.getOnColorWCAG)(args);
62
+ }
63
+ };
64
+ exports.getOnColor = getOnColor;
@@ -0,0 +1,11 @@
1
+ import { type Oklch } from "culori/fn";
2
+ import type * as TColor from "../color.types";
3
+ export declare const hexToOklch: (hex: string) => Oklch;
4
+ export declare const oklchToRgb: (oklch: Oklch) => {
5
+ r: number;
6
+ g: number;
7
+ b: number;
8
+ mode: "rgb";
9
+ alpha?: number;
10
+ };
11
+ export declare const tokenToOklchToken: (token: TColor.Token) => TColor.InternalToken;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.tokenToOklchToken = exports.oklchToRgb = exports.hexToOklch = void 0;
4
+ const fn_1 = require("culori/require");
5
+ // eslint-disable-next-line react-hooks/rules-of-hooks
6
+ const oklch = (0, fn_1.useMode)(fn_1.modeOklch);
7
+ // eslint-disable-next-line react-hooks/rules-of-hooks
8
+ const rgb = (0, fn_1.useMode)(fn_1.modeRgb);
9
+ const hexToOklch = (hex) => {
10
+ const result = oklch((0, fn_1.formatHex)(hex));
11
+ if (!result)
12
+ throw new Error(`[Reshaped] Can't convert ${hex} to oklch`);
13
+ return result;
14
+ };
15
+ exports.hexToOklch = hexToOklch;
16
+ const oklchToRgb = (oklch) => {
17
+ const value = rgb(oklch);
18
+ return {
19
+ ...value,
20
+ r: Number(value.r.toFixed(4)),
21
+ g: Number(value.g.toFixed(4)),
22
+ b: Number(value.b.toFixed(4)),
23
+ };
24
+ };
25
+ exports.oklchToRgb = oklchToRgb;
26
+ const tokenToOklchToken = (token) => {
27
+ const hexDarkFallback = token.hexDark ? (0, exports.hexToOklch)(token.hexDark) : undefined;
28
+ return {
29
+ oklch: token.oklch ? { ...token.oklch, mode: "oklch" } : (0, exports.hexToOklch)(token.hex),
30
+ oklchDark: token.oklchDark ? { ...token.oklchDark, mode: "oklch" } : hexDarkFallback,
31
+ };
32
+ };
33
+ exports.tokenToOklchToken = tokenToOklchToken;
@@ -0,0 +1,4 @@
1
+ import type { ThemeDefinition } from "../../types";
2
+ import { type ColorValue, Hue } from "../color.types";
3
+ declare const generateColors: (args?: Partial<Record<Hue, ColorValue>>) => ThemeDefinition["color"];
4
+ export default generateColors;
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const convert_1 = require("./convert");
4
+ const parseColor = (color) => {
5
+ const isString = typeof color === "string";
6
+ if (!isString && "oklch" in color && color.oklch) {
7
+ return {
8
+ light: color.oklch,
9
+ dark: color.oklchDark,
10
+ };
11
+ }
12
+ const hexLight = isString ? color : color.hex;
13
+ const hexDark = isString ? undefined : color.hexDark;
14
+ const light = (0, convert_1.hexToOklch)(hexLight);
15
+ const dark = hexDark ? (0, convert_1.hexToOklch)(hexDark) : undefined;
16
+ if (!light) {
17
+ throw new Error(`[Reshaped] Failed when parsing color: ${JSON.stringify(color)}`);
18
+ }
19
+ return { light, dark };
20
+ };
21
+ const getDarkModeColor = (lch) => {
22
+ const { l, c } = lch;
23
+ // Top boundary at which background colors are perceived as vibrant
24
+ const vibrancyThreshold = 0.25;
25
+ // A linear modifier of how gray a color is, gray = 1, vibrant = 0
26
+ const neutralFactor = Math.min(1, Math.max(0, (vibrancyThreshold - c) / vibrancyThreshold));
27
+ // Non-linear modification to make gray color inversion progress faster
28
+ const easedVibrancy = 1 - Math.pow(neutralFactor, 2);
29
+ const invertedL = 1 - l;
30
+ // Take plain inversion and increase it back by vibrancy modifier
31
+ // Vibrant colors should get back to almost the original value
32
+ // while non-briant colors should lean towards full inversion
33
+ // Example:
34
+ // Black should become white in dark mode
35
+ // Light gray should become dark gray
36
+ // Vibrant blue should stay vibrant blue
37
+ const darkL = invertedL + (l - invertedL) * easedVibrancy;
38
+ // Make sure dark mode value doesn't become too dark and is still visible on dark page background
39
+ const clampedDarkL = Math.max(0.36, darkL);
40
+ return { ...lch, c: c * 0.8, l: clampedDarkL };
41
+ };
42
+ const generateColorValues = (key, token) => {
43
+ const { light: bg, dark } = parseColor(token);
44
+ const capitalizedKey = key.charAt(0).toUpperCase() + key.slice(1);
45
+ const neutral = key === "neutral";
46
+ const warning = key === "warning";
47
+ const bgDark = dark || getDarkModeColor(bg);
48
+ const bgFaded = {
49
+ ...bg,
50
+ l: 0.97,
51
+ c: neutral ? 0.005 : warning ? 0.04 : 0.02,
52
+ };
53
+ const bgFadedDark = {
54
+ ...bgDark,
55
+ l: 0.25,
56
+ // For primary color with low chroma, we still have to make sure it stays low
57
+ c: neutral ? 0.01 : bgDark.c / 5,
58
+ };
59
+ const fg = neutral ? { ...bg, l: 0.2 } : { ...bg, l: 0.5 };
60
+ const fgDark = neutral ? { ...bgDark, l: 0.96 } : { ...bgDark, l: 0.75, c: bg.c * 0.85 };
61
+ const bd = neutral ? { ...bg, l: 0, alpha: 0.12 } : { ...bg, l: bg.l - 0.08 };
62
+ const bdDark = neutral ? { ...bgDark, l: 1, alpha: 0.16 } : { ...bgDark, l: bgDark.l + 0.08 };
63
+ const bdFaded = neutral
64
+ ? { ...bgFaded, l: 0, alpha: 0.08 }
65
+ : { ...bgFaded, l: bgFaded.l - 0.05, c: bgFaded.c + 0.01 };
66
+ const bdFadedDark = neutral
67
+ ? { ...bgFadedDark, l: 1, alpha: 0.08 }
68
+ : { ...bgFadedDark, l: bgFadedDark.l + 0.1 };
69
+ const output = {
70
+ [`background${capitalizedKey}`]: {
71
+ oklch: bg,
72
+ oklchDark: bgDark,
73
+ },
74
+ [`background${capitalizedKey}Faded`]: {
75
+ oklch: bgFaded,
76
+ oklchDark: bgFadedDark,
77
+ },
78
+ [`border${capitalizedKey}`]: {
79
+ oklch: bd,
80
+ oklchDark: bdDark,
81
+ },
82
+ [`border${capitalizedKey}Faded`]: {
83
+ oklch: bdFaded,
84
+ oklchDark: bdFadedDark,
85
+ },
86
+ [`foreground${capitalizedKey}`]: {
87
+ oklch: fg,
88
+ oklchDark: fgDark,
89
+ },
90
+ };
91
+ if (neutral) {
92
+ output[`foreground${capitalizedKey}Faded`] = {
93
+ oklch: { ...fg, l: fg.l + 0.25 },
94
+ oklchDark: { ...fgDark, l: fgDark.l - 0.15 },
95
+ };
96
+ output[`backgroundDisabled`] = {
97
+ oklch: { ...bg, l: 0.95, c: 0 },
98
+ oklchDark: { ...bgDark, l: 0.28, c: 0 },
99
+ };
100
+ output[`backgroundDisabledFaded`] = {
101
+ oklch: { ...bg, l: 0.98, c: 0 },
102
+ oklchDark: { ...bgDark, l: 0.23, c: 0 },
103
+ };
104
+ output[`borderDisabled`] = {
105
+ oklch: { ...bd, alpha: 0.06 },
106
+ oklchDark: { ...bgDark, l: 0.28, c: 0 },
107
+ };
108
+ output[`foregroundDisabled`] = {
109
+ oklch: { ...fg, l: 0.84, c: 0 },
110
+ oklchDark: { ...fgDark, l: 0.4, c: 0 },
111
+ };
112
+ output[`backgroundElevationBase`] = {
113
+ oklch: { ...bg, l: 1, c: 0 },
114
+ oklchDark: { ...bgDark, l: 0.2, c: 0 },
115
+ };
116
+ output[`backgroundElevationRaised`] = {
117
+ oklch: { ...bg, l: 1, c: 0 },
118
+ oklchDark: { ...bgDark, l: 0.21, c: 0 },
119
+ };
120
+ output[`backgroundElevationOverlay`] = {
121
+ oklch: { ...bg, l: 1, c: 0 },
122
+ oklchDark: { ...bgDark, l: 0.22, c: 0 },
123
+ };
124
+ output[`backgroundPage`] = {
125
+ oklch: { ...bg, l: 1, c: 0 },
126
+ oklchDark: { ...bgDark, l: 0.16, c: 0 },
127
+ };
128
+ output[`backgroundPageFaded`] = {
129
+ oklch: { ...bg, l: 0.97, c: 0 },
130
+ oklchDark: { ...bgDark, l: 0.18, c: 0 },
131
+ };
132
+ }
133
+ return output;
134
+ };
135
+ const getOklchToken = (color) => {
136
+ const token = typeof color === "string" ? { hex: color } : color;
137
+ return (0, convert_1.tokenToOklchToken)(token);
138
+ };
139
+ const generateColors = (args = {}) => {
140
+ const { primary = { oklch: { l: 0.55, c: 0.24, h: 262.67 } }, critical = { oklch: { l: 0.59, c: 0.22, h: 26.97 } }, warning = { oklch: { l: 0.86, c: 0.18, h: 89.84 } }, positive = { oklch: { l: 0.53, c: 0.13, h: 153.78 } }, neutral = { oklch: { l: 0.92, c: 0.01, h: 262.67 } }, brand, } = args;
141
+ return {
142
+ ...generateColorValues("primary", getOklchToken(primary)),
143
+ ...generateColorValues("critical", getOklchToken(critical)),
144
+ ...generateColorValues("warning", getOklchToken(warning)),
145
+ ...generateColorValues("positive", getOklchToken(positive)),
146
+ ...generateColorValues("neutral", getOklchToken(neutral)),
147
+ brand: getOklchToken(brand || primary),
148
+ white: getOklchToken("#ffffff"),
149
+ black: getOklchToken("#000000"),
150
+ };
151
+ };
152
+ exports.default = generateColors;
@@ -0,0 +1,4 @@
1
+ import type * as T from "../../../types";
2
+ import { GeneratedThemeDefinition, PassedThemeDefinition } from "../../types";
3
+ declare const generateMetaColors: (definition: PassedThemeDefinition, themeOptions?: T.PublicOptions["themeOptions"]) => GeneratedThemeDefinition["color"] | undefined;
4
+ export default generateMetaColors;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const string_1 = require("../../../../../utilities/string");
4
+ const constants_1 = require("../../../constants");
5
+ const convert_1 = require("./convert");
6
+ const a11y_1 = require("./a11y");
7
+ const whiteColor = (0, convert_1.hexToOklch)("#ffffff");
8
+ const blackColor = (0, convert_1.hexToOklch)("#000000");
9
+ const generateMetaColors = (definition, themeOptions = {}) => {
10
+ if (!definition.color)
11
+ return;
12
+ const { onColorValues = {}, colorContrastAlgorithm } = themeOptions;
13
+ const result = {};
14
+ Object.keys(definition.color).forEach((tokenName) => {
15
+ const bgToken = definition.color?.[tokenName];
16
+ const generatedForegroundName = `on${(0, string_1.capitalize)(tokenName)}`;
17
+ const generatedRGBName = `rgb${(0, string_1.capitalize)(tokenName)}`;
18
+ const generateOnColorsFor = [
19
+ ...constants_1.bgWithDynamicForeground,
20
+ ...(themeOptions?.generateOnColorsFor || []),
21
+ ];
22
+ const needsDynamicForeground = generateOnColorsFor.includes(tokenName) && !definition.color?.[generatedForegroundName];
23
+ const needsRGB = tokenName.startsWith("background") ||
24
+ tokenName.endsWith("black") ||
25
+ tokenName.endsWith("white");
26
+ if (!bgToken)
27
+ return;
28
+ const oklchBgToken = (0, convert_1.tokenToOklchToken)(bgToken);
29
+ if (needsDynamicForeground) {
30
+ const overrideKeys = Object.keys(onColorValues);
31
+ const onColorKey = overrideKeys.find((key) => tokenName.toLowerCase().includes(key));
32
+ const onColor = onColorKey && onColorValues[onColorKey];
33
+ const lightColor = onColor && "hexLight" in onColor
34
+ ? (0, convert_1.hexToOklch)(onColor.hexLight)
35
+ : onColor?.oklchLight || whiteColor;
36
+ const darkColor = onColor && "hexDark" in onColor
37
+ ? (0, convert_1.hexToOklch)(onColor.hexDark)
38
+ : onColor?.oklchDark || blackColor;
39
+ const light = (0, a11y_1.getOnColor)({
40
+ bgColor: oklchBgToken.oklch,
41
+ lightColor: { ...lightColor, mode: "oklch" },
42
+ darkColor: { ...darkColor, mode: "oklch" },
43
+ algorithm: colorContrastAlgorithm,
44
+ });
45
+ const dark = oklchBgToken.oklchDark &&
46
+ (0, a11y_1.getOnColor)({
47
+ bgColor: oklchBgToken.oklchDark,
48
+ lightColor: { ...lightColor, mode: "oklch" },
49
+ darkColor: { ...darkColor, mode: "oklch" },
50
+ algorithm: colorContrastAlgorithm,
51
+ });
52
+ result[generatedForegroundName] = {
53
+ oklch: light,
54
+ oklchDark: dark,
55
+ };
56
+ }
57
+ if (needsRGB) {
58
+ const rgb = (0, convert_1.oklchToRgb)(oklchBgToken.oklch);
59
+ const rgbDark = oklchBgToken.oklchDark && (0, convert_1.oklchToRgb)(oklchBgToken.oklchDark);
60
+ result[generatedRGBName] = {
61
+ hex: `${rgb.r * 255}, ${rgb.g * 255}, ${rgb.b * 255}`,
62
+ hexDark: rgbDark && `${rgbDark.r * 255}, ${rgbDark.g * 255}, ${rgbDark.b * 255}`,
63
+ };
64
+ }
65
+ });
66
+ return result;
67
+ };
68
+ exports.default = generateMetaColors;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const a11y_1 = require("../a11y");
5
+ const white = { l: 1, c: 0, h: 0, mode: "oklch" };
6
+ const black = { l: 0, c: 0, h: 0, mode: "oklch" };
7
+ const blue = { l: 0.55, c: 0.24, h: 262.67, mode: "oklch" };
8
+ const rose = { l: 0.64, c: 0.2, h: 16.5, mode: "oklch" };
9
+ (0, vitest_1.describe)("themes/tokens/color/utilities/a11y", () => {
10
+ (0, vitest_1.describe)("wcag", () => {
11
+ (0, vitest_1.test)("returns white for black background", () => {
12
+ (0, vitest_1.expect)((0, a11y_1.getOnColor)({ bgColor: black, lightColor: white, darkColor: black })).toBe(white);
13
+ });
14
+ (0, vitest_1.test)("returns black for white background", () => {
15
+ (0, vitest_1.expect)((0, a11y_1.getOnColor)({ bgColor: white, lightColor: white, darkColor: black })).toBe(black);
16
+ });
17
+ (0, vitest_1.test)("returns white for blue background", () => {
18
+ (0, vitest_1.expect)((0, a11y_1.getOnColor)({ bgColor: blue, lightColor: white, darkColor: black })).toBe(white);
19
+ });
20
+ (0, vitest_1.test)("returns black for rose background", () => {
21
+ (0, vitest_1.expect)((0, a11y_1.getOnColor)({ bgColor: rose, lightColor: white, darkColor: black })).toBe(black);
22
+ });
23
+ });
24
+ (0, vitest_1.describe)("apca", () => {
25
+ (0, vitest_1.test)("returns white for black background", () => {
26
+ (0, vitest_1.expect)((0, a11y_1.getOnColor)({ bgColor: black, lightColor: white, darkColor: black, algorithm: "apca" })).toBe(white);
27
+ });
28
+ (0, vitest_1.test)("returns black for white background", () => {
29
+ (0, vitest_1.expect)((0, a11y_1.getOnColor)({ bgColor: white, lightColor: white, darkColor: black, algorithm: "apca" })).toBe(black);
30
+ });
31
+ (0, vitest_1.test)("returns white for blue background", () => {
32
+ (0, vitest_1.expect)((0, a11y_1.getOnColor)({ bgColor: blue, lightColor: white, darkColor: black, algorithm: "apca" })).toBe(white);
33
+ });
34
+ (0, vitest_1.test)("returns white for rose background", () => {
35
+ (0, vitest_1.expect)((0, a11y_1.getOnColor)({ bgColor: rose, lightColor: white, darkColor: black, algorithm: "apca" })).toBe(white);
36
+ });
37
+ });
38
+ });
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const convert_1 = require("../convert");
5
+ const white = (0, convert_1.hexToOklch)("#ffffff");
6
+ const black = (0, convert_1.hexToOklch)("#000000");
7
+ const red = (0, convert_1.hexToOklch)("#ff0000");
8
+ (0, vitest_1.describe)("themes/tokens/color/utilities/convert", () => {
9
+ (0, vitest_1.describe)("hexToOklch", () => {
10
+ (0, vitest_1.test)("white", () => {
11
+ (0, vitest_1.expect)(white).toMatchSnapshot();
12
+ });
13
+ (0, vitest_1.test)("black", () => {
14
+ (0, vitest_1.expect)(black).toMatchSnapshot();
15
+ });
16
+ (0, vitest_1.test)("red", () => {
17
+ (0, vitest_1.expect)(red).toMatchSnapshot();
18
+ });
19
+ (0, vitest_1.test)("white, short", () => {
20
+ (0, vitest_1.expect)((0, convert_1.hexToOklch)("#fff")).toEqual(white);
21
+ });
22
+ });
23
+ (0, vitest_1.describe)("oklchToRgb", () => {
24
+ (0, vitest_1.test)("white", () => {
25
+ (0, vitest_1.expect)((0, convert_1.oklchToRgb)(white)).toMatchSnapshot();
26
+ });
27
+ (0, vitest_1.test)("black", () => {
28
+ (0, vitest_1.expect)((0, convert_1.oklchToRgb)(black)).toMatchSnapshot();
29
+ });
30
+ (0, vitest_1.test)("red", () => {
31
+ (0, vitest_1.expect)((0, convert_1.oklchToRgb)(red)).toMatchSnapshot();
32
+ });
33
+ });
34
+ (0, vitest_1.describe)("tokenToOklchToken", () => {
35
+ (0, vitest_1.test)("hex", () => {
36
+ (0, vitest_1.expect)((0, convert_1.tokenToOklchToken)({ hex: "#ffffff" })).toMatchSnapshot();
37
+ });
38
+ (0, vitest_1.test)("hex and hexDark", () => {
39
+ (0, vitest_1.expect)((0, convert_1.tokenToOklchToken)({ hex: "#ffffff", hexDark: "#000000" })).toMatchSnapshot();
40
+ });
41
+ (0, vitest_1.test)("oklch", () => {
42
+ (0, vitest_1.expect)((0, convert_1.tokenToOklchToken)({ oklch: white })).toMatchSnapshot();
43
+ });
44
+ (0, vitest_1.test)("oklch and oklchDark", () => {
45
+ (0, vitest_1.expect)((0, convert_1.tokenToOklchToken)({ oklch: white, oklchDark: black })).toMatchSnapshot();
46
+ });
47
+ });
48
+ });
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const vitest_1 = require("vitest");
7
+ const generateColors_1 = __importDefault(require("../generateColors"));
8
+ (0, vitest_1.describe)("themes/tokens/color/utilities/generateColors", () => {
9
+ (0, vitest_1.test)("with default values", () => {
10
+ (0, vitest_1.expect)((0, generateColors_1.default)()).toMatchSnapshot();
11
+ });
12
+ (0, vitest_1.test)("with a passed value", () => {
13
+ const black = { l: 0, c: 0, h: 0 };
14
+ const colors = (0, generateColors_1.default)({ primary: { oklch: black } });
15
+ (0, vitest_1.expect)(colors).toMatchSnapshot();
16
+ (0, vitest_1.expect)(colors.backgroundPrimary).toEqual({
17
+ oklch: { ...black, mode: "oklch" },
18
+ oklchDark: { ...black, l: 1, mode: "oklch" },
19
+ });
20
+ });
21
+ });
@@ -1,4 +1,4 @@
1
- import type * as T from "../tokens/types";
1
+ import type * as T from "./types";
2
2
  export declare const getVariableName: (tokenName: string, tokenType?: string) => string;
3
3
  export declare const createVariable: (token: T.TransformedToken) => string;
4
4
  export declare const createMedia: (token: T.TransformedToken) => string;
@@ -1,4 +1,4 @@
1
1
  import type * as T from "./duration.types";
2
2
  import type { Transformer } from "../types";
3
- declare const transformedToken: Transformer<T.Token>;
4
- export default transformedToken;
3
+ declare const transformToken: Transformer<T.Token>;
4
+ export default transformToken;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const transformedToken = (name, token) => [
3
+ const transformToken = (name, token) => [
4
4
  {
5
5
  name,
6
6
  tokenType: "duration",
@@ -8,4 +8,4 @@ const transformedToken = (name, token) => [
8
8
  value: `${token.ms}ms`,
9
9
  },
10
10
  ];
11
- exports.default = transformedToken;
11
+ exports.default = transformToken;