@digdir/designsystemet 1.13.2 → 1.14.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.
Files changed (89) hide show
  1. package/dist/bin/config.js +50 -658
  2. package/dist/bin/designsystemet.d.ts.map +1 -1
  3. package/dist/bin/designsystemet.js +136 -4623
  4. package/dist/bin/options.js +22 -14
  5. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/BorderValues.js +9 -0
  6. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/BoxShadowTypes.js +8 -0
  7. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/BoxShadowValues.js +13 -0
  8. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/ColorModifierTypes.js +10 -0
  9. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/ColorSpaceTypes.js +10 -0
  10. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/Properties.js +53 -0
  11. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/StrokeStyleValues.js +14 -0
  12. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/TokenSetStatus.js +9 -0
  13. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/TokenTypes.js +32 -0
  14. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/constants/TypographyValues.js +15 -0
  15. package/dist/node_modules/.pnpm/@tokens-studio_types@0.5.2/node_modules/@tokens-studio/types/dist/types/index.js +11 -0
  16. package/dist/package.js +5 -0
  17. package/dist/src/colors/colorMetadata.js +255 -261
  18. package/dist/src/colors/index.js +5 -522
  19. package/dist/src/colors/theme.js +104 -343
  20. package/dist/src/colors/types.js +22 -25
  21. package/dist/src/colors/utils.js +241 -154
  22. package/dist/src/config.js +103 -444
  23. package/dist/src/index.js +10 -3588
  24. package/dist/src/migrations/beta-to-v1.js +339 -537
  25. package/dist/src/migrations/codemods/css/plugins.js +31 -42
  26. package/dist/src/migrations/codemods/css/run.js +20 -151
  27. package/dist/src/migrations/color-rename-next49.js +44 -220
  28. package/dist/src/migrations/index.js +7 -577
  29. package/dist/src/scripts/update-preview-tokens.d.ts.map +1 -1
  30. package/dist/src/tokens/build.js +39 -1816
  31. package/dist/src/tokens/create/files.d.ts +10 -5
  32. package/dist/src/tokens/create/files.d.ts.map +1 -1
  33. package/dist/src/tokens/create/files.js +44 -601
  34. package/dist/src/tokens/create/generators/$designsystemet.js +8 -97
  35. package/dist/src/tokens/create/generators/$metadata.d.ts +7 -3
  36. package/dist/src/tokens/create/generators/$metadata.d.ts.map +1 -1
  37. package/dist/src/tokens/create/generators/$metadata.js +24 -21
  38. package/dist/src/tokens/create/generators/$themes.d.ts +2 -4
  39. package/dist/src/tokens/create/generators/$themes.d.ts.map +1 -1
  40. package/dist/src/tokens/create/generators/$themes.js +139 -312
  41. package/dist/src/tokens/create/generators/primitives/color-scheme.js +68 -443
  42. package/dist/src/tokens/create/generators/primitives/globals.js +141 -147
  43. package/dist/src/tokens/create/generators/primitives/size.js +146 -156
  44. package/dist/src/tokens/create/generators/primitives/typography.js +213 -217
  45. package/dist/src/tokens/create/generators/semantic/color-modes.js +24 -59
  46. package/dist/src/tokens/create/generators/semantic/color.js +42 -326
  47. package/dist/src/tokens/create/generators/semantic/style.js +379 -382
  48. package/dist/src/tokens/create/generators/themes/theme.js +108 -392
  49. package/dist/src/tokens/create.d.ts +2 -1
  50. package/dist/src/tokens/create.d.ts.map +1 -1
  51. package/dist/src/tokens/create.js +50 -1591
  52. package/dist/src/tokens/format.d.ts.map +1 -1
  53. package/dist/src/tokens/format.js +35 -3324
  54. package/dist/src/tokens/generate-config.js +155 -298
  55. package/dist/src/tokens/index.js +3 -3344
  56. package/dist/src/tokens/process/configs/color.js +50 -1085
  57. package/dist/src/tokens/process/configs/semantic.js +45 -1083
  58. package/dist/src/tokens/process/configs/shared.js +18 -109
  59. package/dist/src/tokens/process/configs/size-mode.js +27 -1082
  60. package/dist/src/tokens/process/configs/size.js +32 -1083
  61. package/dist/src/tokens/process/configs/type-scale.js +49 -1083
  62. package/dist/src/tokens/process/configs/typography.js +63 -1084
  63. package/dist/src/tokens/process/configs.js +91 -1224
  64. package/dist/src/tokens/process/formats/css/color.js +58 -1079
  65. package/dist/src/tokens/process/formats/css/semantic.js +27 -1081
  66. package/dist/src/tokens/process/formats/css/size-mode.js +43 -1077
  67. package/dist/src/tokens/process/formats/css/size.js +86 -1079
  68. package/dist/src/tokens/process/formats/css/type-scale.js +53 -1083
  69. package/dist/src/tokens/process/formats/css/typography.js +27 -1081
  70. package/dist/src/tokens/process/formats/css.js +18 -1081
  71. package/dist/src/tokens/process/output/declarations.js +18 -1201
  72. package/dist/src/tokens/process/output/tailwind.js +26 -40
  73. package/dist/src/tokens/process/output/theme.js +73 -206
  74. package/dist/src/tokens/process/platform.js +165 -1355
  75. package/dist/src/tokens/process/transformers.js +49 -89
  76. package/dist/src/tokens/process/utils/getMultidimensionalThemes.js +100 -1183
  77. package/dist/src/tokens/process/utils/kebab-case.js +7 -5
  78. package/dist/src/tokens/types.d.ts +6 -0
  79. package/dist/src/tokens/types.d.ts.map +1 -1
  80. package/dist/src/tokens/types.js +6 -7
  81. package/dist/src/tokens/utils.d.ts +2 -1
  82. package/dist/src/tokens/utils.d.ts.map +1 -1
  83. package/dist/src/tokens/utils.js +107 -93
  84. package/dist/src/types.js +1 -5
  85. package/dist/src/utils/filesystem.js +112 -124
  86. package/package.json +12 -15
  87. package/configs/test-tokens.config.json +0 -82
  88. package/dist/src/scripts/createJsonSchema.js +0 -409
  89. package/dist/src/scripts/update-preview-tokens.js +0 -3353
@@ -1,157 +1,244 @@
1
- // src/colors/utils.ts
2
1
  import chroma from "chroma-js";
3
2
  import Colorjs from "colorjs.io";
4
3
  import { Hsluv } from "hsluv";
5
- var hexToCssHsl = (hex, valuesOnly = false) => {
6
- const [h, s, l] = chroma(hex).hsl();
7
- const hRounded = Math.round(h);
8
- const sRounded = Math.round(s * 100);
9
- const lRounded = Math.round(l * 100);
10
- const cssString = `${hRounded},${sRounded}%,${lRounded}%`;
11
- return valuesOnly ? cssString : `hsl(${cssString})`;
12
- };
13
- var hexToHSL = (hex) => {
14
- const [h, s, l] = chroma(hex).hsl();
15
- return [Math.round(h), Math.round(s * 100), Math.round(l * 100)];
16
- };
17
- var hexToHsluv = (hex) => {
18
- const conv = new Hsluv();
19
- conv.hex = hex;
20
- conv.hexToHsluv();
21
- return [conv.hsluv_h, conv.hsluv_s, conv.hsluv_l];
22
- };
23
- var hslArrToCss = (HSL) => {
24
- return "hsl(" + HSL[0] + "," + HSL[1] + "%," + HSL[2] + "%)";
25
- };
26
- var HSLToHex = (h, s, l) => {
27
- return chroma.hsl(h, s / 100, l / 100).hex();
28
- };
29
- var hexToRgb = (hex, type = "255") => {
30
- const rgb = chroma(hex).rgb();
31
- return {
32
- r: type === "255" ? rgb[0] : rgb[0] / 255,
33
- g: type === "255" ? rgb[1] : rgb[1] / 255,
34
- b: type === "255" ? rgb[2] : rgb[2] / 255
35
- };
36
- };
37
- var getContrastFromHex = (color1, color2) => {
38
- const lum1 = chroma(color1).luminance();
39
- const lum2 = chroma(color2).luminance();
40
- return (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
41
- };
42
- var getContrastFromLightness = (lightness, mainColor, backgroundColor) => {
43
- const conv = new Hsluv();
44
- conv.hex = mainColor;
45
- conv.hexToHsluv();
46
- conv.hsluv_l = lightness;
47
- conv.hsluvToHex();
48
- const lightMainColor = conv.hex;
49
- const lum1 = chroma(lightMainColor).luminance();
50
- const lum2 = chroma(backgroundColor).luminance();
51
- const ratio = (Math.max(lum1, lum2) + 0.05) / (Math.min(lum1, lum2) + 0.05);
52
- return ratio;
53
- };
54
- var areColorsContrasting = (color1, color2, type = "aa") => {
55
- const contrast = getContrastFromHex(color1, color2);
56
- if (contrast !== null) {
57
- if (type === "aaa") {
58
- return contrast >= 7;
59
- }
60
- if (type === "aa") {
61
- return contrast >= 4.5;
62
- }
63
- return contrast >= 3;
64
- }
65
- return false;
66
- };
67
- var isHexColor = (hex) => {
68
- return typeof hex === "string" && hex.length === 6 && !Number.isNaN(Number("0x" + hex));
69
- };
70
- var getLuminanceFromLightness = (lightness) => {
71
- const conv = new Hsluv();
72
- conv.hsluv_l = lightness;
73
- conv.hsluvToHex();
74
- return chroma(conv.hex).luminance();
75
- };
76
- var getLuminanceFromColor = (color) => {
77
- return chroma(color).luminance();
78
- };
79
- var getLightnessFromHex = (hex) => {
80
- const conv = new Hsluv();
81
- conv.hex = hex;
82
- conv.hexToHsluv();
83
- return conv.hsluv_l;
84
- };
85
- var canTextBeUsedOnColors = (baseDefaultColor, baseActiveColor) => {
86
- const defaultAgainstWhite = getContrastFromHex(baseDefaultColor, "#ffffff");
87
- const defaultAgainstBlack = getContrastFromHex(baseDefaultColor, "#000000");
88
- const activeAgainstWhite = getContrastFromHex(baseActiveColor, "#ffffff");
89
- const activeAgainstBlack = getContrastFromHex(baseActiveColor, "#000000");
90
- if (defaultAgainstWhite >= 4.5 && activeAgainstWhite >= 4.5) {
91
- return true;
92
- }
93
- if (defaultAgainstBlack >= 4.5 && activeAgainstBlack >= 4.5) {
94
- return true;
95
- }
96
- return false;
97
- };
98
- var convertToHex = (color) => {
99
- if (!color) {
100
- return "#000000";
101
- }
102
- if (/^#[0-9A-Fa-f]{6}$/.test(color)) {
103
- return color;
104
- }
105
- return chroma(color).hex();
106
- };
107
- var rgbToHex = (rgb) => {
108
- return `#${[rgb.r, rgb.g, rgb.b].map((x) => {
109
- const hex = Math.round(x * 255).toString(16);
110
- return hex.length === 1 ? "0" + hex : hex;
111
- }).join("")}`;
112
- };
113
- var convertColor = (cssColor, format) => {
114
- const color = new Colorjs(cssColor);
115
- switch (format) {
116
- case "rgb":
117
- case "rgba":
118
- return color.toString({
119
- format: {
120
- name: format,
121
- coords: ["<number>[0, 255]", "<number>[0, 255]", "<number>[0, 255]"]
122
- },
123
- precision: 3
124
- });
125
- case "hex":
126
- return color.toString({ format, precision: 3 });
127
- case "hct":
128
- return color.to(format).toString({
129
- format: {
130
- name: format,
131
- coords: ["<number>", "<number>", "<number>"]
132
- },
133
- precision: 3
134
- });
135
- default:
136
- return color.to(format).toString({ precision: 3 });
137
- }
138
- };
139
- export {
140
- HSLToHex,
141
- areColorsContrasting,
142
- canTextBeUsedOnColors,
143
- convertColor,
144
- convertToHex,
145
- getContrastFromHex,
146
- getContrastFromLightness,
147
- getLightnessFromHex,
148
- getLuminanceFromColor,
149
- getLuminanceFromLightness,
150
- hexToCssHsl,
151
- hexToHSL,
152
- hexToHsluv,
153
- hexToRgb,
154
- hslArrToCss,
155
- isHexColor,
156
- rgbToHex
157
- };
4
+ //#region src/colors/utils.ts
5
+ /**
6
+ * Converts a HEX color '#xxxxxx' into a CSS HSL string 'hsl(x,x,x)'
7
+ *
8
+ * @param hex A hex color string
9
+ * @param valuesOnly If true, only the values are returned
10
+ * @returns A CSS HSL string
11
+ */
12
+ const hexToCssHsl = (hex, valuesOnly = false) => {
13
+ const [h, s, l] = chroma(hex).hsl();
14
+ const cssString = `${Math.round(h)},${Math.round(s * 100)}%,${Math.round(l * 100)}%`;
15
+ return valuesOnly ? cssString : `hsl(${cssString})`;
16
+ };
17
+ /**
18
+ * Converts a HEX string '#xxxxxx' into an array of HSL values '[h,s,l]'
19
+ *
20
+ * @param H A Hex color string
21
+ * @returns HSL values in an array
22
+ */
23
+ const hexToHSL = (hex) => {
24
+ const [h, s, l] = chroma(hex).hsl();
25
+ return [
26
+ Math.round(h),
27
+ Math.round(s * 100),
28
+ Math.round(l * 100)
29
+ ];
30
+ };
31
+ /**
32
+ * Converts a HEX color '#xxxxxx' into an array of HSLuv values '[h,s,l]'
33
+ *
34
+ * @param hex A hex color string
35
+ * @returns
36
+ */
37
+ const hexToHsluv = (hex) => {
38
+ const conv = new Hsluv();
39
+ conv.hex = hex;
40
+ conv.hexToHsluv();
41
+ return [
42
+ conv.hsluv_h,
43
+ conv.hsluv_s,
44
+ conv.hsluv_l
45
+ ];
46
+ };
47
+ /**
48
+ * Converts a HSL number array '[h,s,l]' into HSL CSS string 'hsl(x,x,x)'
49
+ *
50
+ * @param HSL A HSL number array '[h,s,l]'
51
+ * @returns A hex color string
52
+ */
53
+ const hslArrToCss = (HSL) => {
54
+ return "hsl(" + HSL[0] + "," + HSL[1] + "%," + HSL[2] + "%)";
55
+ };
56
+ /**
57
+ * Converts a HSL CSS string 'hsl(x,x,x)' into an array of HSL values '[h,s,l]'
58
+ *
59
+ * @param h The HSL hue
60
+ * @param s The HSL saturation
61
+ * @param l The HSL lightness
62
+ * @returns HEX color string
63
+ */
64
+ const HSLToHex = (h, s, l) => {
65
+ return chroma.hsl(h, s / 100, l / 100).hex();
66
+ };
67
+ /**
68
+ * Converts a HEX color '#xxxxxx' into an array of RGB values '[R, G, B]'
69
+ *
70
+ * @param hex A hex color string
71
+ * @param type The type of RGB values to return
72
+ * @returns RGB values in an array
73
+ */
74
+ const hexToRgb = (hex, type = "255") => {
75
+ const rgb = chroma(hex).rgb();
76
+ return {
77
+ r: type === "255" ? rgb[0] : rgb[0] / 255,
78
+ g: type === "255" ? rgb[1] : rgb[1] / 255,
79
+ b: type === "255" ? rgb[2] : rgb[2] / 255
80
+ };
81
+ };
82
+ /**
83
+ * Get the contrast ratio between two HEX colors
84
+ *
85
+ * @param color1 The first color to compare
86
+ * @param color2 The second color to compare
87
+ * @returns
88
+ */
89
+ const getContrastFromHex = (color1, color2) => {
90
+ const lum1 = chroma(color1).luminance();
91
+ const lum2 = chroma(color2).luminance();
92
+ return (Math.max(lum1, lum2) + .05) / (Math.min(lum1, lum2) + .05);
93
+ };
94
+ /**
95
+ * Get the contrast ratio between two colors at a specific lightness
96
+ *
97
+ * @param lightness The lightness value
98
+ * @param mainColor The main color
99
+ * @param backgroundColor The background color
100
+ * @returns The contrast ratio
101
+ */
102
+ const getContrastFromLightness = (lightness, mainColor, backgroundColor) => {
103
+ const conv = new Hsluv();
104
+ conv.hex = mainColor;
105
+ conv.hexToHsluv();
106
+ conv.hsluv_l = lightness;
107
+ conv.hsluvToHex();
108
+ const lightMainColor = conv.hex;
109
+ const lum1 = chroma(lightMainColor).luminance();
110
+ const lum2 = chroma(backgroundColor).luminance();
111
+ return (Math.max(lum1, lum2) + .05) / (Math.min(lum1, lum2) + .05);
112
+ };
113
+ /**
114
+ * Check if two colors have enough contrast to be used together
115
+ *
116
+ * @param color1 The first color
117
+ * @param color2 The second color
118
+ * @returns If the colors have enough contrast
119
+ */
120
+ const areColorsContrasting = (color1, color2, type = "aa") => {
121
+ const contrast = getContrastFromHex(color1, color2);
122
+ if (contrast !== null) {
123
+ if (type === "aaa") return contrast >= 7;
124
+ if (type === "aa") return contrast >= 4.5;
125
+ return contrast >= 3;
126
+ }
127
+ return false;
128
+ };
129
+ /**
130
+ * Check if aa string value is a HEX color
131
+ *
132
+ * @param hex The hex color
133
+ */
134
+ const isHexColor = (hex) => {
135
+ return typeof hex === "string" && hex.length === 6 && !Number.isNaN(Number("0x" + hex));
136
+ };
137
+ /**
138
+ * Get the luminance value from a lightness value
139
+ *
140
+ * @param lightness The lightness value
141
+ */
142
+ const getLuminanceFromLightness = (lightness) => {
143
+ const conv = new Hsluv();
144
+ conv.hsluv_l = lightness;
145
+ conv.hsluvToHex();
146
+ return chroma(conv.hex).luminance();
147
+ };
148
+ /**
149
+ * Get the relative luminance from any valid css color
150
+ *
151
+ * @param color
152
+ */
153
+ const getLuminanceFromColor = (color) => {
154
+ return chroma(color).luminance();
155
+ };
156
+ /**
157
+ * Get the HSLuv lightness from a HEX color
158
+ *
159
+ * @param hex The hex color
160
+ */
161
+ const getLightnessFromHex = (hex) => {
162
+ const conv = new Hsluv();
163
+ conv.hex = hex;
164
+ conv.hexToHsluv();
165
+ return conv.hsluv_l;
166
+ };
167
+ /**
168
+ *
169
+ * This function checks if white or black text can be used on 2 different colors at 4.5:1 contrast.
170
+ *
171
+ * @param baseDefaultColor Base default color
172
+ * @param baseActiveColor Base active color
173
+ */
174
+ const canTextBeUsedOnColors = (baseDefaultColor, baseActiveColor) => {
175
+ const defaultAgainstWhite = getContrastFromHex(baseDefaultColor, "#ffffff");
176
+ const defaultAgainstBlack = getContrastFromHex(baseDefaultColor, "#000000");
177
+ const activeAgainstWhite = getContrastFromHex(baseActiveColor, "#ffffff");
178
+ const activeAgainstBlack = getContrastFromHex(baseActiveColor, "#000000");
179
+ if (defaultAgainstWhite >= 4.5 && activeAgainstWhite >= 4.5) return true;
180
+ if (defaultAgainstBlack >= 4.5 && activeAgainstBlack >= 4.5) return true;
181
+ return false;
182
+ };
183
+ /**
184
+ * Converts a color to a HEX color
185
+ *
186
+ * @param color
187
+ * @returns
188
+ */
189
+ const convertToHex = (color) => {
190
+ if (!color) return "#000000";
191
+ if (/^#[0-9A-Fa-f]{6}$/.test(color)) return color;
192
+ return chroma(color).hex();
193
+ };
194
+ const rgbToHex = (rgb) => {
195
+ return `#${[
196
+ rgb.r,
197
+ rgb.g,
198
+ rgb.b
199
+ ].map((x) => {
200
+ const hex = Math.round(x * 255).toString(16);
201
+ return hex.length === 1 ? "0" + hex : hex;
202
+ }).join("")}`;
203
+ };
204
+ /**
205
+ * Convert a color to a different format
206
+ *
207
+ * @param cssColor Any valid css color
208
+ * @param format Color space/format supported here https://colorjs.io/docs/spaces
209
+ */
210
+ const convertColor = (cssColor, format) => {
211
+ const color = new Colorjs(cssColor);
212
+ switch (format) {
213
+ case "rgb":
214
+ case "rgba": return color.toString({
215
+ format: {
216
+ name: format,
217
+ coords: [
218
+ "<number>[0, 255]",
219
+ "<number>[0, 255]",
220
+ "<number>[0, 255]"
221
+ ]
222
+ },
223
+ precision: 3
224
+ });
225
+ case "hex": return color.toString({
226
+ format,
227
+ precision: 3
228
+ });
229
+ case "hct": return color.to(format).toString({
230
+ format: {
231
+ name: format,
232
+ coords: [
233
+ "<number>",
234
+ "<number>",
235
+ "<number>"
236
+ ]
237
+ },
238
+ precision: 3
239
+ });
240
+ default: return color.to(format).toString({ precision: 3 });
241
+ }
242
+ };
243
+ //#endregion
244
+ export { HSLToHex, areColorsContrasting, canTextBeUsedOnColors, convertColor, convertToHex, getContrastFromHex, getContrastFromLightness, getLightnessFromHex, getLuminanceFromColor, getLuminanceFromLightness, hexToCssHsl, hexToHSL, hexToHsluv, hexToRgb, hslArrToCss, isHexColor, rgbToHex };