@primer/primitives 11.4.0 → 11.4.1-rc.24c79953

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 (152) hide show
  1. package/DESIGN_TOKENS_GUIDE.md +185 -0
  2. package/DESIGN_TOKENS_SPEC.md +565 -0
  3. package/dist/build/formats/jsonFigma.js +8 -1
  4. package/dist/build/formats/markdownLlmGuidelines.d.ts +7 -6
  5. package/dist/build/formats/markdownLlmGuidelines.js +1034 -60
  6. package/dist/build/schemas/borderToken.d.ts +17 -3
  7. package/dist/build/schemas/colorToken.d.ts +1 -1
  8. package/dist/build/schemas/cubicBezierToken.d.ts +1 -1
  9. package/dist/build/schemas/dimensionToken.d.ts +9 -2
  10. package/dist/build/schemas/dimensionValue.d.ts +12 -1
  11. package/dist/build/schemas/dimensionValue.js +10 -13
  12. package/dist/build/schemas/durationToken.d.ts +8 -2
  13. package/dist/build/schemas/durationValue.d.ts +11 -1
  14. package/dist/build/schemas/durationValue.js +13 -3
  15. package/dist/build/schemas/fontFamilyToken.d.ts +1 -1
  16. package/dist/build/schemas/fontWeightToken.d.ts +1 -1
  17. package/dist/build/schemas/gradientToken.d.ts +1 -1
  18. package/dist/build/schemas/numberToken.d.ts +1 -1
  19. package/dist/build/schemas/shadowToken.d.ts +673 -85
  20. package/dist/build/schemas/stringToken.d.ts +1 -1
  21. package/dist/build/schemas/stringToken.js +1 -1
  22. package/dist/build/schemas/tokenType.d.ts +1 -1
  23. package/dist/build/schemas/transitionToken.d.ts +15 -3
  24. package/dist/build/schemas/typographyToken.d.ts +19 -5
  25. package/dist/build/schemas/typographyToken.js +1 -1
  26. package/dist/build/schemas/validTokenType.d.ts +1 -1
  27. package/dist/build/schemas/validTokenType.js +1 -1
  28. package/dist/build/schemas/viewportRangeToken.d.ts +1 -1
  29. package/dist/build/transformers/borderToCss.js +17 -1
  30. package/dist/build/transformers/dimensionToPixelUnitless.d.ts +3 -2
  31. package/dist/build/transformers/dimensionToPixelUnitless.js +22 -26
  32. package/dist/build/transformers/dimensionToRem.d.ts +2 -1
  33. package/dist/build/transformers/dimensionToRem.js +21 -22
  34. package/dist/build/transformers/dimensionToRemPxArray.d.ts +2 -1
  35. package/dist/build/transformers/dimensionToRemPxArray.js +21 -22
  36. package/dist/build/transformers/durationToCss.d.ts +2 -1
  37. package/dist/build/transformers/durationToCss.js +18 -11
  38. package/dist/build/transformers/shadowToCss.js +12 -1
  39. package/dist/build/transformers/utilities/parseDimension.d.ts +12 -0
  40. package/dist/build/transformers/utilities/parseDimension.js +31 -0
  41. package/dist/build/types/borderTokenValue.d.ts +3 -1
  42. package/dist/build/types/dimensionTokenValue.d.ts +9 -0
  43. package/dist/build/types/shadowTokenValue.d.ts +6 -4
  44. package/dist/css/functional/themes/dark-colorblind-high-contrast.css +32 -28
  45. package/dist/css/functional/themes/dark-colorblind.css +32 -28
  46. package/dist/css/functional/themes/dark-dimmed-high-contrast.css +32 -28
  47. package/dist/css/functional/themes/dark-dimmed.css +32 -28
  48. package/dist/css/functional/themes/dark-high-contrast.css +32 -28
  49. package/dist/css/functional/themes/dark-tritanopia-high-contrast.css +32 -28
  50. package/dist/css/functional/themes/dark-tritanopia.css +32 -28
  51. package/dist/css/functional/themes/dark.css +32 -28
  52. package/dist/css/functional/themes/light-colorblind-high-contrast.css +32 -28
  53. package/dist/css/functional/themes/light-colorblind.css +32 -28
  54. package/dist/css/functional/themes/light-high-contrast.css +32 -28
  55. package/dist/css/functional/themes/light-tritanopia-high-contrast.css +32 -28
  56. package/dist/css/functional/themes/light-tritanopia.css +32 -28
  57. package/dist/css/functional/themes/light.css +32 -28
  58. package/dist/css/primitives.css +4 -0
  59. package/dist/docs/base/motion/motion.json +96 -24
  60. package/dist/docs/base/size/size.json +76 -19
  61. package/dist/docs/base/typography/typography.json +24 -6
  62. package/dist/docs/functional/size/border.json +26 -11
  63. package/dist/docs/functional/size/breakpoints.json +24 -6
  64. package/dist/docs/functional/size/radius.json +16 -4
  65. package/dist/docs/functional/size/size.json +60 -15
  66. package/dist/docs/functional/themes/dark-colorblind-high-contrast.json +1423 -346
  67. package/dist/docs/functional/themes/dark-colorblind.json +1423 -346
  68. package/dist/docs/functional/themes/dark-dimmed-high-contrast.json +1423 -346
  69. package/dist/docs/functional/themes/dark-dimmed.json +1423 -346
  70. package/dist/docs/functional/themes/dark-high-contrast.json +1423 -346
  71. package/dist/docs/functional/themes/dark-tritanopia-high-contrast.json +1423 -346
  72. package/dist/docs/functional/themes/dark-tritanopia.json +1423 -346
  73. package/dist/docs/functional/themes/dark.json +1423 -346
  74. package/dist/docs/functional/themes/light-colorblind-high-contrast.json +1426 -349
  75. package/dist/docs/functional/themes/light-colorblind.json +1426 -349
  76. package/dist/docs/functional/themes/light-high-contrast.json +1426 -349
  77. package/dist/docs/functional/themes/light-tritanopia-high-contrast.json +1426 -349
  78. package/dist/docs/functional/themes/light-tritanopia.json +1426 -349
  79. package/dist/docs/functional/themes/light.json +1426 -349
  80. package/dist/docs/functional/typography/typography.json +8 -2
  81. package/dist/fallbacks/base/motion/motion.json +48 -12
  82. package/dist/figma/themes/light-colorblind.json +4 -4
  83. package/dist/figma/themes/light-high-contrast.json +4 -4
  84. package/dist/figma/themes/light-tritanopia.json +4 -4
  85. package/dist/figma/themes/light.json +4 -4
  86. package/dist/internalCss/dark-colorblind-high-contrast.css +28 -28
  87. package/dist/internalCss/dark-colorblind.css +28 -28
  88. package/dist/internalCss/dark-dimmed-high-contrast.css +28 -28
  89. package/dist/internalCss/dark-dimmed.css +28 -28
  90. package/dist/internalCss/dark-high-contrast.css +28 -28
  91. package/dist/internalCss/dark-tritanopia-high-contrast.css +28 -28
  92. package/dist/internalCss/dark-tritanopia.css +28 -28
  93. package/dist/internalCss/dark.css +28 -28
  94. package/dist/internalCss/light-colorblind-high-contrast.css +28 -28
  95. package/dist/internalCss/light-colorblind.css +28 -28
  96. package/dist/internalCss/light-high-contrast.css +28 -28
  97. package/dist/internalCss/light-tritanopia-high-contrast.css +28 -28
  98. package/dist/internalCss/light-tritanopia.css +28 -28
  99. package/dist/internalCss/light.css +28 -28
  100. package/dist/styleLint/base/motion/motion.json +96 -24
  101. package/dist/styleLint/base/size/size.json +76 -19
  102. package/dist/styleLint/base/typography/typography.json +30 -12
  103. package/dist/styleLint/functional/size/border.json +27 -12
  104. package/dist/styleLint/functional/size/breakpoints.json +24 -6
  105. package/dist/styleLint/functional/size/radius.json +17 -5
  106. package/dist/styleLint/functional/size/size-coarse.json +3 -3
  107. package/dist/styleLint/functional/size/size-fine.json +3 -3
  108. package/dist/styleLint/functional/size/size.json +111 -66
  109. package/dist/styleLint/functional/themes/dark-colorblind-high-contrast.json +1551 -366
  110. package/dist/styleLint/functional/themes/dark-colorblind.json +1551 -366
  111. package/dist/styleLint/functional/themes/dark-dimmed-high-contrast.json +1551 -366
  112. package/dist/styleLint/functional/themes/dark-dimmed.json +1551 -366
  113. package/dist/styleLint/functional/themes/dark-high-contrast.json +1551 -366
  114. package/dist/styleLint/functional/themes/dark-tritanopia-high-contrast.json +1551 -366
  115. package/dist/styleLint/functional/themes/dark-tritanopia.json +1551 -366
  116. package/dist/styleLint/functional/themes/dark.json +1551 -366
  117. package/dist/styleLint/functional/themes/light-colorblind-high-contrast.json +1554 -369
  118. package/dist/styleLint/functional/themes/light-colorblind.json +1554 -369
  119. package/dist/styleLint/functional/themes/light-high-contrast.json +1554 -369
  120. package/dist/styleLint/functional/themes/light-tritanopia-high-contrast.json +1554 -369
  121. package/dist/styleLint/functional/themes/light-tritanopia.json +1554 -369
  122. package/dist/styleLint/functional/themes/light.json +1554 -369
  123. package/dist/styleLint/functional/typography/typography.json +28 -22
  124. package/package.json +6 -5
  125. package/src/tokens/base/motion/timing.json5 +12 -12
  126. package/src/tokens/base/size/size.json5 +19 -19
  127. package/src/tokens/base/typography/typography.json5 +6 -6
  128. package/src/tokens/component/avatar.json5 +72 -44
  129. package/src/tokens/component/button.json5 +1545 -1193
  130. package/src/tokens/functional/border/border.json5 +4 -1
  131. package/src/tokens/functional/color/bgColor.json5 +8 -0
  132. package/src/tokens/functional/color/display.json5 +7 -0
  133. package/src/tokens/functional/color/fgColor.json5 +8 -0
  134. package/src/tokens/functional/color/syntax.json5 +14 -0
  135. package/src/tokens/functional/shadow/shadow.json5 +678 -163
  136. package/src/tokens/functional/size/border.json5 +8 -8
  137. package/src/tokens/functional/size/breakpoints.json5 +6 -6
  138. package/src/tokens/functional/size/radius.json5 +4 -4
  139. package/src/tokens/functional/size/size.json5 +15 -15
  140. package/src/tokens/functional/typography/typography.json5 +8 -4
  141. package/dist/build/parsers/index.d.ts +0 -1
  142. package/dist/build/parsers/index.js +0 -1
  143. package/dist/build/parsers/w3cJsonParser.d.ts +0 -6
  144. package/dist/build/parsers/w3cJsonParser.js +0 -25
  145. package/dist/removed/testing.json5 +0 -4
  146. package/guidelines/color.llm.md +0 -16
  147. package/guidelines/guidelines.llm.md +0 -34
  148. package/guidelines/motion.llm.md +0 -41
  149. package/guidelines/spacing.llm.md +0 -20
  150. package/guidelines/typography.llm.md +0 -14
  151. package/src/tokens/removed/testing.json5 +0 -4
  152. package/token-guidelines.llm.md +0 -695
@@ -3,7 +3,7 @@ export declare const stringToken: z.ZodObject<{
3
3
  $description: z.ZodOptional<z.ZodString>;
4
4
  $deprecated: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>;
5
5
  $value: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
6
- $type: z.ZodLiteral<"string" | "number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange">;
6
+ $type: z.ZodLiteral<"number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange" | "custom-string">;
7
7
  $extensions: z.ZodOptional<z.ZodObject<{
8
8
  'org.primer.llm': z.ZodOptional<z.ZodObject<{
9
9
  usage: z.ZodOptional<z.ZodArray<z.ZodString>>;
@@ -6,7 +6,7 @@ import { llmExtension } from './llmExtension.js';
6
6
  export const stringToken = baseToken
7
7
  .extend({
8
8
  $value: z.union([z.string(), referenceValue]),
9
- $type: tokenType('string'),
9
+ $type: tokenType('custom-string'),
10
10
  $extensions: z
11
11
  .object({
12
12
  'org.primer.llm': llmExtension,
@@ -1,3 +1,3 @@
1
1
  import { z } from 'zod';
2
2
  import type { TokenType } from './validTokenType.js';
3
- export declare const tokenType: ($type: TokenType) => z.ZodLiteral<"string" | "number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange">;
3
+ export declare const tokenType: ($type: TokenType) => z.ZodLiteral<"number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange" | "custom-string">;
@@ -3,11 +3,23 @@ export declare const transitionToken: z.ZodObject<{
3
3
  $description: z.ZodOptional<z.ZodString>;
4
4
  $deprecated: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>;
5
5
  $value: z.ZodUnion<readonly [z.ZodObject<{
6
- duration: z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodString, z.ZodString]>, z.ZodString]>;
6
+ duration: z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodObject<{
7
+ value: z.ZodNumber;
8
+ unit: z.ZodEnum<{
9
+ s: "s";
10
+ ms: "ms";
11
+ }>;
12
+ }, z.core.$strict>, z.ZodString]>, z.ZodString]>;
7
13
  timingFunction: z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodArray<z.ZodNumber>, z.ZodString]>, z.ZodString]>;
8
- delay: z.ZodOptional<z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodString, z.ZodString]>, z.ZodString]>>;
14
+ delay: z.ZodOptional<z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodObject<{
15
+ value: z.ZodNumber;
16
+ unit: z.ZodEnum<{
17
+ s: "s";
18
+ ms: "ms";
19
+ }>;
20
+ }, z.core.$strict>, z.ZodString]>, z.ZodString]>>;
9
21
  }, z.core.$strip>, z.ZodString]>;
10
- $type: z.ZodLiteral<"string" | "number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange">;
22
+ $type: z.ZodLiteral<"number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange" | "custom-string">;
11
23
  $extensions: z.ZodOptional<z.ZodObject<{
12
24
  'org.primer.llm': z.ZodOptional<z.ZodObject<{
13
25
  usage: z.ZodOptional<z.ZodArray<z.ZodString>>;
@@ -1,7 +1,14 @@
1
1
  import { z } from 'zod';
2
2
  export declare const typographyValue: z.ZodObject<{
3
- fontSize: z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodString, z.ZodLiteral<"0">, z.ZodLiteral<0>]>, z.ZodString]>;
4
- lineHeight: z.ZodOptional<z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodString, z.ZodLiteral<"0">, z.ZodLiteral<0>]>, z.ZodString]>>;
3
+ fontSize: z.ZodUnion<readonly [z.ZodObject<{
4
+ value: z.ZodNumber;
5
+ unit: z.ZodEnum<{
6
+ em: "em";
7
+ px: "px";
8
+ rem: "rem";
9
+ }>;
10
+ }, z.core.$strict>, z.ZodString]>;
11
+ lineHeight: z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodString]>>;
5
12
  fontWeight: z.ZodUnion<readonly [z.ZodNumber, z.ZodString]>;
6
13
  fontFamily: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
7
14
  }, z.core.$strip>;
@@ -9,12 +16,19 @@ export declare const typographyToken: z.ZodObject<{
9
16
  $description: z.ZodOptional<z.ZodString>;
10
17
  $deprecated: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>;
11
18
  $value: z.ZodUnion<readonly [z.ZodObject<{
12
- fontSize: z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodString, z.ZodLiteral<"0">, z.ZodLiteral<0>]>, z.ZodString]>;
13
- lineHeight: z.ZodOptional<z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodString, z.ZodLiteral<"0">, z.ZodLiteral<0>]>, z.ZodString]>>;
19
+ fontSize: z.ZodUnion<readonly [z.ZodObject<{
20
+ value: z.ZodNumber;
21
+ unit: z.ZodEnum<{
22
+ em: "em";
23
+ px: "px";
24
+ rem: "rem";
25
+ }>;
26
+ }, z.core.$strict>, z.ZodString]>;
27
+ lineHeight: z.ZodOptional<z.ZodUnion<readonly [z.ZodNumber, z.ZodString]>>;
14
28
  fontWeight: z.ZodUnion<readonly [z.ZodNumber, z.ZodString]>;
15
29
  fontFamily: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
16
30
  }, z.core.$strip>, z.ZodString]>;
17
- $type: z.ZodLiteral<"string" | "number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange">;
31
+ $type: z.ZodLiteral<"number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange" | "custom-string">;
18
32
  $extensions: z.ZodOptional<z.ZodObject<{
19
33
  'org.primer.llm': z.ZodOptional<z.ZodObject<{
20
34
  usage: z.ZodOptional<z.ZodArray<z.ZodString>>;
@@ -7,7 +7,7 @@ import { tokenType } from './tokenType.js';
7
7
  import { llmExtension } from './llmExtension.js';
8
8
  export const typographyValue = z.object({
9
9
  fontSize: z.union([dimensionValue, referenceValue]),
10
- lineHeight: z.union([dimensionValue, referenceValue]).optional(),
10
+ lineHeight: z.union([z.number(), referenceValue]).optional(),
11
11
  fontWeight: z.union([fontWeightValue, referenceValue]),
12
12
  fontFamily: z.union([z.string().min(1), referenceValue]),
13
13
  });
@@ -1,5 +1,5 @@
1
1
  import { z } from 'zod';
2
- declare const validTypes: readonly ["color", "cubicBezier", "typography", "dimension", "duration", "border", "shadow", "fontFamily", "fontWeight", "gradient", "number", "string", "transition", "custom-viewportRange"];
2
+ declare const validTypes: readonly ["color", "cubicBezier", "typography", "dimension", "duration", "border", "shadow", "fontFamily", "fontWeight", "gradient", "number", "custom-string", "transition", "custom-viewportRange"];
3
3
  export type TokenType = (typeof validTypes)[number];
4
4
  /**
5
5
  * Recursively validates $type properties in token structure
@@ -12,7 +12,7 @@ const validTypes = [
12
12
  'fontWeight',
13
13
  'gradient',
14
14
  'number',
15
- 'string',
15
+ 'custom-string',
16
16
  'transition',
17
17
  'custom-viewportRange',
18
18
  ];
@@ -3,7 +3,7 @@ export declare const viewportRangeToken: z.ZodObject<{
3
3
  $description: z.ZodOptional<z.ZodString>;
4
4
  $deprecated: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodBoolean]>>;
5
5
  $value: z.ZodUnion<readonly [z.ZodString, z.ZodString]>;
6
- $type: z.ZodLiteral<"string" | "number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange">;
6
+ $type: z.ZodLiteral<"number" | "border" | "color" | "fontFamily" | "fontWeight" | "transition" | "dimension" | "duration" | "gradient" | "shadow" | "typography" | "cubicBezier" | "custom-viewportRange" | "custom-string">;
7
7
  $extensions: z.ZodOptional<z.ZodObject<{
8
8
  'org.primer.llm': z.ZodOptional<z.ZodObject<{
9
9
  usage: z.ZodOptional<z.ZodArray<z.ZodString>>;
@@ -1,4 +1,20 @@
1
1
  import { isBorder } from '../filters/isBorder.js';
2
+ import { parseDimension } from './utilities/parseDimension.js';
3
+ /**
4
+ * @description Converts a W3C dimension object to CSS string, preserving the original unit
5
+ * @param dim - The dimension value in W3C object format or a string
6
+ * @returns CSS dimension string (e.g., "2px", "0.125rem", "1em", "0")
7
+ */
8
+ const dimensionToCss = (dim) => {
9
+ if (typeof dim === 'string') {
10
+ return dim;
11
+ }
12
+ const { value, unit } = parseDimension(dim);
13
+ if (value === 0) {
14
+ return '0';
15
+ }
16
+ return `${value}${unit}`;
17
+ };
2
18
  /**
3
19
  * checks if all required properties exist on shadow token
4
20
  * @param object - BorderTokenValue
@@ -33,6 +49,6 @@ export const borderToCss = {
33
49
  throw new Error(`Invalid border token property ${JSON.stringify(value)}. Must be an object with color, width and style properties.`);
34
50
  }
35
51
  /* width | style | color */
36
- return `${value.width} ${value.style} ${value.color}`;
52
+ return `${dimensionToCss(value.width)} ${value.style} ${value.color}`;
37
53
  },
38
54
  };
@@ -1,8 +1,9 @@
1
1
  import type { Transform } from 'style-dictionary/types';
2
2
  /**
3
- * @description converts dimension tokens value to pixel value without unit, ignores `em` as they are relative to the font size of the parent element
3
+ * @description converts dimension tokens value to pixel value without unit
4
4
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
5
5
  * @matcher matches all tokens of $type `dimension`
6
- * @transformer returns a float number
6
+ * @transformer returns a number for px/rem values, or a string with unit for em values (cannot convert to unitless)
7
+ * @note Expects W3C DTCG format { value: number, unit: "px" | "rem" | "em" }
7
8
  */
8
9
  export declare const dimensionToPixelUnitless: Transform;
@@ -1,4 +1,5 @@
1
1
  import { isDimension } from '../filters/index.js';
2
+ import { parseDimension } from './utilities/parseDimension.js';
2
3
  /**
3
4
  * @description base font size from options or 16
4
5
  * @param options
@@ -6,22 +7,11 @@ import { isDimension } from '../filters/index.js';
6
7
  */
7
8
  const getBasePxFontSize = (options) => (options && options.basePxFontSize) || 16;
8
9
  /**
9
- * @description checks if token value has a specific unit
10
- * @param value token value
11
- * @param unit unit string like px or value
12
- * @returns boolean
13
- */
14
- const hasUnit = (value, unit) => {
15
- if (typeof value === 'number') {
16
- return false;
17
- }
18
- return value.indexOf(unit) > -1;
19
- };
20
- /**
21
- * @description converts dimension tokens value to pixel value without unit, ignores `em` as they are relative to the font size of the parent element
10
+ * @description converts dimension tokens value to pixel value without unit
22
11
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
23
12
  * @matcher matches all tokens of $type `dimension`
24
- * @transformer returns a float number
13
+ * @transformer returns a number for px/rem values, or a string with unit for em values (cannot convert to unitless)
14
+ * @note Expects W3C DTCG format { value: number, unit: "px" | "rem" | "em" }
25
15
  */
26
16
  export const dimensionToPixelUnitless = {
27
17
  name: 'dimension/pixelUnitless',
@@ -31,19 +21,25 @@ export const dimensionToPixelUnitless = {
31
21
  transform: (token, config, options) => {
32
22
  const valueProp = options.usesDtcg ? '$value' : 'value';
33
23
  const baseFont = getBasePxFontSize(config);
34
- const floatVal = parseFloat(token[valueProp]);
35
- if (isNaN(floatVal)) {
36
- throw new Error(`Invalid dimension token: '${token.path.join('.')}: ${token[valueProp]}' is not valid and cannot be transform to 'float' \n`);
37
- }
38
- if (floatVal === 0) {
39
- return 0;
40
- }
41
- if (hasUnit(token[valueProp], 'rem')) {
42
- return floatVal * baseFont;
24
+ try {
25
+ const { value, unit } = parseDimension(token[valueProp]);
26
+ if (value === 0) {
27
+ return 0;
28
+ }
29
+ // rem values convert to px
30
+ if (unit === 'rem') {
31
+ return value * baseFont;
32
+ }
33
+ // em values pass through as string (relative to parent, cannot convert to unitless px)
34
+ if (unit === 'em') {
35
+ return `${value}em`;
36
+ }
37
+ // px values return the number directly
38
+ return value;
43
39
  }
44
- if (hasUnit(token[valueProp], 'px')) {
45
- return floatVal;
40
+ catch (error) {
41
+ const originalMessage = error instanceof Error ? error.message : String(error);
42
+ throw new Error(`Invalid dimension token: '${token.path.join('.')}: ${JSON.stringify(token[valueProp])}' - ${originalMessage}\n`);
46
43
  }
47
- return token[valueProp];
48
44
  },
49
45
  };
@@ -1,8 +1,9 @@
1
1
  import type { Transform } from 'style-dictionary/types';
2
2
  /**
3
- * @description converts dimension tokens value to `rem`, ignores `em` as they are relative to the font size of the parent element
3
+ * @description converts dimension tokens value to `rem`
4
4
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
5
5
  * @matcher matches all tokens of $type `dimension`
6
6
  * @transformer returns a `rem` string
7
+ * @note Expects W3C DTCG format { value: number, unit: "px" | "rem" | "em" }
7
8
  */
8
9
  export declare const dimensionToRem: Transform;
@@ -1,4 +1,5 @@
1
1
  import { isDimension } from '../filters/index.js';
2
+ import { parseDimension } from './utilities/parseDimension.js';
2
3
  /**
3
4
  * @description base font size from options or 16
4
5
  * @param options
@@ -6,22 +7,11 @@ import { isDimension } from '../filters/index.js';
6
7
  */
7
8
  const getBasePxFontSize = (options) => (options && options.basePxFontSize) || 16;
8
9
  /**
9
- * @description checks if token value has a specific unit
10
- * @param value token value
11
- * @param unit unit string like px or value
12
- * @returns boolean
13
- */
14
- const hasUnit = (value, unit) => {
15
- if (typeof value === 'number') {
16
- return false;
17
- }
18
- return value.indexOf(unit) > -1;
19
- };
20
- /**
21
- * @description converts dimension tokens value to `rem`, ignores `em` as they are relative to the font size of the parent element
10
+ * @description converts dimension tokens value to `rem`
22
11
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
23
12
  * @matcher matches all tokens of $type `dimension`
24
13
  * @transformer returns a `rem` string
14
+ * @note Expects W3C DTCG format { value: number, unit: "px" | "rem" | "em" }
25
15
  */
26
16
  export const dimensionToRem = {
27
17
  name: 'dimension/rem',
@@ -31,16 +21,25 @@ export const dimensionToRem = {
31
21
  transform: (token, config, options) => {
32
22
  const valueProp = options.usesDtcg ? '$value' : 'value';
33
23
  const baseFont = getBasePxFontSize(config);
34
- const floatVal = parseFloat(token[valueProp]);
35
- if (isNaN(floatVal)) {
36
- throw new Error(`Invalid dimension token: '${token.name}: ${token[valueProp]}' is not valid and cannot be transform to 'rem' \n`);
37
- }
38
- if (floatVal === 0) {
39
- return '0';
24
+ try {
25
+ const { value, unit } = parseDimension(token[valueProp]);
26
+ if (value === 0) {
27
+ return '0';
28
+ }
29
+ // rem values pass through unchanged
30
+ if (unit === 'rem') {
31
+ return `${value}rem`;
32
+ }
33
+ // em values pass through unchanged (relative to parent, cannot convert)
34
+ if (unit === 'em') {
35
+ return `${value}em`;
36
+ }
37
+ // px values convert to rem
38
+ return `${value / baseFont}rem`;
40
39
  }
41
- if (hasUnit(token[valueProp], 'rem') || hasUnit(token[valueProp], 'em')) {
42
- return token[valueProp];
40
+ catch (error) {
41
+ const details = error instanceof Error && error.message ? ` - ${error.message}` : error ? ` - ${String(error)}` : '';
42
+ throw new Error(`Invalid dimension token: '${token.name}: ${JSON.stringify(token[valueProp])}' is not valid and cannot be transformed to 'rem'${details}\n`);
43
43
  }
44
- return `${floatVal / baseFont}rem`;
45
44
  },
46
45
  };
@@ -1,8 +1,9 @@
1
1
  import type { Transform } from 'style-dictionary/types';
2
2
  /**
3
- * @description converts dimension tokens value to `rem`, ignores `em` as they are relative to the font size of the parent element
3
+ * @description converts dimension tokens value to an array with both `rem` and `px` values
4
4
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
5
5
  * @matcher matches all tokens of $type `dimension`
6
6
  * @transformer returns an array with the `rem` and `pixel` string
7
+ * @note Expects W3C DTCG format { value: number, unit: "px" | "rem" | "em" }
7
8
  */
8
9
  export declare const dimensionToRemPxArray: Transform;
@@ -1,4 +1,5 @@
1
1
  import { isDimension } from '../filters/index.js';
2
+ import { parseDimension } from './utilities/parseDimension.js';
2
3
  /**
3
4
  * @description base font size from options or 16
4
5
  * @param options
@@ -6,22 +7,11 @@ import { isDimension } from '../filters/index.js';
6
7
  */
7
8
  const getBasePxFontSize = (options) => (options && options.basePxFontSize) || 16;
8
9
  /**
9
- * @description checks if token value has a specific unit
10
- * @param value token value
11
- * @param unit unit string like px or value
12
- * @returns boolean
13
- */
14
- const hasUnit = (value, unit) => {
15
- if (typeof value === 'number') {
16
- return false;
17
- }
18
- return value.indexOf(unit) > -1;
19
- };
20
- /**
21
- * @description converts dimension tokens value to `rem`, ignores `em` as they are relative to the font size of the parent element
10
+ * @description converts dimension tokens value to an array with both `rem` and `px` values
22
11
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
23
12
  * @matcher matches all tokens of $type `dimension`
24
13
  * @transformer returns an array with the `rem` and `pixel` string
14
+ * @note Expects W3C DTCG format { value: number, unit: "px" | "rem" | "em" }
25
15
  */
26
16
  export const dimensionToRemPxArray = {
27
17
  name: 'dimension/remPxArray',
@@ -31,16 +21,25 @@ export const dimensionToRemPxArray = {
31
21
  transform: (token, config, options) => {
32
22
  const valueProp = options.usesDtcg ? '$value' : 'value';
33
23
  const baseFont = getBasePxFontSize(config);
34
- const floatVal = parseFloat(token[valueProp]);
35
- if (isNaN(floatVal)) {
36
- throw new Error(`Invalid dimension token: '${token.name}: ${token[valueProp]}' is not valid and cannot be transform to 'rem' \n`);
37
- }
38
- if (floatVal === 0) {
39
- return ['0', '0'];
24
+ try {
25
+ const { value, unit } = parseDimension(token[valueProp]);
26
+ if (value === 0) {
27
+ return ['0', '0'];
28
+ }
29
+ // em values pass through unchanged (relative to parent, cannot convert)
30
+ if (unit === 'em') {
31
+ return [`${value}em`, `${value}em`];
32
+ }
33
+ // rem values pass through, convert to px for second value
34
+ if (unit === 'rem') {
35
+ return [`${value}rem`, `${value * baseFont}px`];
36
+ }
37
+ // px values convert to rem for first value
38
+ return [`${value / baseFont}rem`, `${value}px`];
40
39
  }
41
- if (hasUnit(token[valueProp], 'rem') || hasUnit(token[valueProp], 'em')) {
42
- return [token.value, `${floatVal * baseFont}px`];
40
+ catch (error) {
41
+ const errorMessage = error instanceof Error ? error.message : String(error);
42
+ throw new Error(`Invalid dimension token: '${token.name}: ${JSON.stringify(token[valueProp])}' is not valid and cannot be transformed to 'rem' - ${errorMessage}\n`);
43
43
  }
44
- return [`${floatVal / baseFont}rem`, `${floatVal}px`];
45
44
  },
46
45
  };
@@ -1,8 +1,9 @@
1
1
  import type { Transform } from 'style-dictionary/types';
2
2
  /**
3
- * @description converts duration tokens string value to number with `ms` unit
3
+ * @description converts duration tokens to css duration string
4
4
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
5
5
  * @matcher matches all tokens of $type `duration`
6
6
  * @transformer returns a css duration
7
+ * @note W3C DTCG format: { value: number, unit: "ms" | "s" }
7
8
  */
8
9
  export declare const durationToCss: Transform;
@@ -1,9 +1,10 @@
1
1
  import { isDuration } from '../filters/index.js';
2
2
  /**
3
- * @description converts duration tokens string value to number with `ms` unit
3
+ * @description converts duration tokens to css duration string
4
4
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
5
5
  * @matcher matches all tokens of $type `duration`
6
6
  * @transformer returns a css duration
7
+ * @note W3C DTCG format: { value: number, unit: "ms" | "s" }
7
8
  */
8
9
  export const durationToCss = {
9
10
  name: 'duration/css',
@@ -12,18 +13,24 @@ export const durationToCss = {
12
13
  filter: isDuration,
13
14
  transform: (token, _config, options) => {
14
15
  const valueProp = options.usesDtcg ? '$value' : 'value';
15
- // throw an error if token value is not a string or does not end with `ms`
16
- if (typeof token[valueProp] !== `string` || !(token[valueProp].endsWith(`ms`) || token[valueProp].endsWith(`s`))) {
17
- throw new Error(`duration token value must be a string with an "ms" || "s" unit, invalid token: ${token.name} with value: ${token[valueProp]}`);
16
+ const tokenValue = token[valueProp];
17
+ // Validate W3C DTCG object format: { value: number, unit: "ms" | "s" }
18
+ if (typeof tokenValue !== 'object' || tokenValue === null || !('value' in tokenValue) || !('unit' in tokenValue)) {
19
+ throw new Error(`duration token value must be an object with "value" and "unit" properties (W3C DTCG format). Invalid token: ${token.name} with value: ${JSON.stringify(tokenValue)}`);
18
20
  }
19
- // get value
20
- let value = parseInt(token[valueProp].replace('ms', ''));
21
- let unit = `ms`;
22
- if (value >= 1000) {
23
- value = value / 1000;
24
- unit = `s`;
21
+ const { value, unit } = tokenValue;
22
+ // Validate unit
23
+ if (unit !== 'ms' && unit !== 's') {
24
+ throw new Error(`duration token unit must be "ms" or "s", invalid token: ${token.name} with unit: ${unit}`);
25
+ }
26
+ // Validate value is a finite, non-negative number
27
+ if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
28
+ throw new Error(`duration token value must be a finite, non-negative number, invalid token: ${token.name} with value: ${value}`);
29
+ }
30
+ // Convert ms >= 1000 to seconds for cleaner output
31
+ if (unit === 'ms' && value >= 1000) {
32
+ return `${value / 1000}s`;
25
33
  }
26
- // return value
27
34
  return `${value}${unit}`;
28
35
  },
29
36
  };
@@ -3,6 +3,17 @@ import { isShadow } from '../filters/index.js';
3
3
  import { alpha } from './utilities/alpha.js';
4
4
  import { checkRequiredTokenProperties } from './utilities/checkRequiredTokenProperties.js';
5
5
  import { getTokenValue } from './utilities/getTokenValue.js';
6
+ /**
7
+ * @description Converts a W3C dimension object to CSS string
8
+ * @param dim - The dimension value in W3C object format
9
+ * @returns CSS dimension string (e.g., "16px", "1rem", "0")
10
+ */
11
+ const dimensionToCss = (dim) => {
12
+ if (dim.value === 0) {
13
+ return '0';
14
+ }
15
+ return `${dim.value}${dim.unit}`;
16
+ };
6
17
  /**
7
18
  * @description converts w3c shadow tokens in css shadow string
8
19
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
@@ -27,7 +38,7 @@ export const shadowToCss = {
27
38
  return shadow;
28
39
  checkRequiredTokenProperties(shadow, ['color', 'offsetX', 'offsetY', 'blur', 'spread']);
29
40
  /*css box shadow: inset? | offset-x | offset-y | blur-radius | spread-radius | color */
30
- return `${shadow.inset === true ? 'inset ' : ''}${shadow.offsetX} ${shadow.offsetY} ${shadow.blur} ${shadow.spread} ${toHex(alpha(getTokenValue(Object.assign(Object.assign({}, token), { [valueProp]: shadow }), 'color'), shadow.alpha || 1, token, config))}`;
41
+ return `${shadow.inset === true ? 'inset ' : ''}${dimensionToCss(shadow.offsetX)} ${dimensionToCss(shadow.offsetY)} ${dimensionToCss(shadow.blur)} ${dimensionToCss(shadow.spread)} ${toHex(alpha(getTokenValue(Object.assign(Object.assign({}, token), { [valueProp]: shadow }), 'color'), shadow.alpha || 1, token, config))}`;
31
42
  })
32
43
  .join(', ');
33
44
  },
@@ -0,0 +1,12 @@
1
+ import type { DimensionTokenValue } from '../../types/dimensionTokenValue.js';
2
+ /**
3
+ * @description Parses and validates a dimension value in W3C DTCG object format
4
+ * @param input - The dimension value in W3C object format { value: number, unit: "px" | "rem" | "em" }
5
+ * @returns Validated DimensionTokenValue
6
+ * @throws Error if the input is not a valid W3C dimension object
7
+ *
8
+ * W3C DTCG format: { value: 16, unit: "px" }
9
+ * @note `em` is not in the W3C spec but is supported for practical use
10
+ * @link https://www.designtokens.org/tr/drafts/format/#dimension
11
+ */
12
+ export declare const parseDimension: (input: DimensionTokenValue) => DimensionTokenValue;
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @description Parses and validates a dimension value in W3C DTCG object format
3
+ * @param input - The dimension value in W3C object format { value: number, unit: "px" | "rem" | "em" }
4
+ * @returns Validated DimensionTokenValue
5
+ * @throws Error if the input is not a valid W3C dimension object
6
+ *
7
+ * W3C DTCG format: { value: 16, unit: "px" }
8
+ * @note `em` is not in the W3C spec but is supported for practical use
9
+ * @link https://www.designtokens.org/tr/drafts/format/#dimension
10
+ */
11
+ export const parseDimension = (input) => {
12
+ // Runtime validation for W3C DTCG object format
13
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
14
+ if (typeof input !== 'object' || input === null) {
15
+ throw new Error(`Invalid dimension value: ${JSON.stringify(input)} - must be a W3C DTCG dimension object with "value" and "unit" properties`);
16
+ }
17
+ const inputObj = input;
18
+ if (!('value' in inputObj) || !('unit' in inputObj)) {
19
+ throw new Error(`Invalid dimension value: ${JSON.stringify(input)} - must be a W3C DTCG dimension object with "value" and "unit" properties`);
20
+ }
21
+ const { value, unit } = input;
22
+ if (typeof value !== 'number' || !Number.isFinite(value)) {
23
+ throw new Error(`Invalid dimension value: ${JSON.stringify(input)} - value must be a finite number`);
24
+ }
25
+ // Runtime check - unit could be invalid at runtime even if types say otherwise
26
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
27
+ if (unit !== 'px' && unit !== 'rem' && unit !== 'em') {
28
+ throw new Error(`Invalid dimension unit: ${String(unit)} - must be "px", "rem", or "em"`);
29
+ }
30
+ return { value, unit };
31
+ };
@@ -1,3 +1,5 @@
1
+ import type {DimensionTokenValue} from './dimensionTokenValue.js'
2
+
1
3
  /**
2
4
  * Type definition for w3c border composite token value
3
5
  * @link https://design-tokens.github.io/community-group/format/#border
@@ -5,6 +7,6 @@
5
7
  export type StrokeStyleString = 'solid' | 'dashed' | 'dotted' | 'double' | 'groove' | 'ridge' | 'outset' | 'inset'
6
8
  export type BorderTokenValue = {
7
9
  color: string
8
- width: string
10
+ width: string | DimensionTokenValue
9
11
  style: StrokeStyleString
10
12
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * W3C DTCG dimension token value format
3
+ * @link https://www.designtokens.org/tr/drafts/format/#dimension
4
+ * @note `em` is not in the W3C spec but is supported for practical use
5
+ */
6
+ export type DimensionTokenValue = {
7
+ value: number
8
+ unit: 'px' | 'rem' | 'em'
9
+ }
@@ -1,13 +1,15 @@
1
+ import type {DimensionTokenValue} from './dimensionTokenValue.js'
2
+
1
3
  /**
2
4
  * Type definition for w3c shadow composite token value
3
5
  * @link https://design-tokens.github.io/community-group/format/#shadow
4
6
  */
5
7
  export type ShadowTokenValue = {
6
8
  color: string
7
- offsetX: string
8
- offsetY: string
9
- blur: string
10
- spread: string
9
+ offsetX: DimensionTokenValue
10
+ offsetY: DimensionTokenValue
11
+ blur: DimensionTokenValue
12
+ spread: DimensionTokenValue
11
13
  // custom non w3c values
12
14
  inset?: boolean
13
15
  alpha?: number