@primer/primitives 7.17.0-rc.132f02a7 → 7.17.0-rc.34cd1021

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 (162) hide show
  1. package/README.md +1 -1
  2. package/dist/css/functional/themes/dark-colorblind.css +10 -0
  3. package/dist/css/functional/themes/dark-dimmed.css +10 -0
  4. package/dist/css/functional/themes/dark-high-contrast.css +10 -0
  5. package/dist/css/functional/themes/dark-tritanopia.css +10 -0
  6. package/dist/css/functional/themes/dark.css +10 -0
  7. package/dist/css/functional/themes/light-colorblind.css +10 -0
  8. package/dist/css/functional/themes/light-high-contrast.css +10 -0
  9. package/dist/css/functional/themes/light-tritanopia.css +10 -0
  10. package/dist/css/functional/themes/light.css +10 -0
  11. package/dist/css/functional/typography/typography.css +1 -1
  12. package/dist/docs/base/typography/typography.json +52 -4
  13. package/dist/docs/functional/themes/dark-colorblind.json +223 -78
  14. package/dist/docs/functional/themes/dark-dimmed.json +218 -73
  15. package/dist/docs/functional/themes/dark-high-contrast.json +220 -75
  16. package/dist/docs/functional/themes/dark-tritanopia.json +223 -78
  17. package/dist/docs/functional/themes/dark.json +218 -73
  18. package/dist/docs/functional/themes/light-colorblind.json +221 -76
  19. package/dist/docs/functional/themes/light-high-contrast.json +215 -70
  20. package/dist/docs/functional/themes/light-tritanopia.json +221 -76
  21. package/dist/docs/functional/themes/light.json +216 -71
  22. package/dist/docs/functional/typography/typography.json +494 -36
  23. package/dist/fallbacks/functional/typography/typography.json +1 -1
  24. package/dist/figma/figma.json +18 -1
  25. package/dist/figma/scales/dark-dimmed.json +20 -20
  26. package/dist/figma/scales/dark-high-constrast.json +20 -20
  27. package/dist/figma/scales/dark.json +20 -20
  28. package/dist/figma/scales/light-high-constrast.json +20 -20
  29. package/dist/figma/scales/light.json +20 -20
  30. package/dist/figma/themes/dark-colorblind.json +150 -70
  31. package/dist/figma/themes/dark-dimmed.json +145 -65
  32. package/dist/figma/themes/dark-high-contrast.json +149 -69
  33. package/dist/figma/themes/dark-tritanopia.json +150 -70
  34. package/dist/figma/themes/dark.json +145 -65
  35. package/dist/figma/themes/light-colorblind.json +139 -59
  36. package/dist/figma/themes/light-high-contrast.json +135 -55
  37. package/dist/figma/themes/light-tritanopia.json +139 -59
  38. package/dist/figma/themes/light.json +134 -54
  39. package/dist/figma/typography/typography.json +370 -0
  40. package/dist/internalCss/dark-colorblind.css +30 -20
  41. package/dist/internalCss/dark-dimmed.css +30 -20
  42. package/dist/internalCss/dark-high-contrast.css +30 -20
  43. package/dist/internalCss/dark-tritanopia.css +30 -20
  44. package/dist/internalCss/dark.css +30 -20
  45. package/dist/internalCss/light-colorblind.css +30 -20
  46. package/dist/internalCss/light-high-contrast.css +30 -20
  47. package/dist/internalCss/light-tritanopia.css +30 -20
  48. package/dist/internalCss/light.css +30 -20
  49. package/dist/styleLint/base/typography/typography.json +52 -4
  50. package/dist/styleLint/functional/themes/dark-colorblind.json +223 -78
  51. package/dist/styleLint/functional/themes/dark-dimmed.json +218 -73
  52. package/dist/styleLint/functional/themes/dark-high-contrast.json +220 -75
  53. package/dist/styleLint/functional/themes/dark-tritanopia.json +223 -78
  54. package/dist/styleLint/functional/themes/dark.json +218 -73
  55. package/dist/styleLint/functional/themes/light-colorblind.json +221 -76
  56. package/dist/styleLint/functional/themes/light-high-contrast.json +215 -70
  57. package/dist/styleLint/functional/themes/light-tritanopia.json +221 -76
  58. package/dist/styleLint/functional/themes/light.json +216 -71
  59. package/dist/styleLint/functional/typography/typography.json +494 -36
  60. package/package.json +4 -11
  61. package/src/PrimerStyleDictionary.ts +12 -0
  62. package/src/filters/index.ts +1 -0
  63. package/src/filters/isNumber.ts +8 -0
  64. package/src/formats/jsonFigma.ts +1 -0
  65. package/src/platforms/figma.ts +10 -1
  66. package/src/schemas/numberToken.ts +17 -0
  67. package/src/tokens/base/color/dark/dark.dimmed.json5 +1 -1
  68. package/src/tokens/base/color/dark/dark.high-contrast.json5 +1 -1
  69. package/src/tokens/base/color/dark/dark.json5 +1 -1
  70. package/src/tokens/base/color/light/light.high-contrast.json5 +1 -1
  71. package/src/tokens/base/color/light/light.json5 +1 -1
  72. package/src/tokens/base/typography/typography.json +28 -4
  73. package/src/tokens/functional/color/dark/app-dark.json5 +5 -5
  74. package/src/tokens/functional/color/dark/display-dark.json5 +66 -0
  75. package/src/tokens/functional/color/dark/overrides/dark.high-contrast.json5 +17 -17
  76. package/src/tokens/functional/color/dark/overrides/dark.protanopia-deuteranopia.json5 +5 -5
  77. package/src/tokens/functional/color/dark/overrides/dark.tritanopia.json5 +5 -5
  78. package/src/tokens/functional/color/dark/patterns-dark.json5 +42 -42
  79. package/src/tokens/functional/color/dark/primitives-dark.json5 +16 -16
  80. package/src/tokens/functional/color/dark/syntax-dark.json5 +13 -13
  81. package/src/tokens/functional/color/light/app-light.json5 +5 -5
  82. package/src/tokens/functional/color/light/display-light.json5 +66 -0
  83. package/src/tokens/functional/color/light/overrides/light.high-contrast.json5 +25 -25
  84. package/src/tokens/functional/color/light/overrides/light.protanopia-deuteranopia.json5 +5 -5
  85. package/src/tokens/functional/color/light/overrides/light.tritanopia.json5 +5 -5
  86. package/src/tokens/functional/color/light/patterns-light.json5 +30 -30
  87. package/src/tokens/functional/color/light/primitives-light.json5 +15 -15
  88. package/src/tokens/functional/color/light/syntax-light.json5 +14 -14
  89. package/src/tokens/functional/shadow/dark.json5 +1 -1
  90. package/src/tokens/functional/shadow/light.json5 +11 -11
  91. package/src/tokens/functional/typography/typography.json +263 -34
  92. package/src/transformers/floatToPixel.ts +33 -0
  93. package/src/transformers/fontFamilyToCss.ts +3 -4
  94. package/src/transformers/fontFamilyToFigma.ts +47 -0
  95. package/src/transformers/index.ts +2 -0
  96. package/src/transformers/utilities/hasSpaceInString.ts +6 -0
  97. package/build.js +0 -578
  98. package/tokens/base/size/size.json +0 -57
  99. package/tokens/base/typography/typography.json +0 -20
  100. package/tokens/functional/size/border.json +0 -48
  101. package/tokens/functional/size/breakpoints.json +0 -24
  102. package/tokens/functional/size/size-coarse.json +0 -27
  103. package/tokens/functional/size/size-fine.json +0 -27
  104. package/tokens/functional/size/size.json +0 -189
  105. package/tokens/functional/size/viewport.json +0 -24
  106. package/tokens/functional/typography/typography.json +0 -213
  107. package/tokens/removed/color.json +0 -421
  108. package/tokens-v2-private/css/tokens/base/size/size.css +0 -24
  109. package/tokens-v2-private/css/tokens/base/typography/typography.css +0 -11
  110. package/tokens-v2-private/css/tokens/functional/size/border.css +0 -17
  111. package/tokens-v2-private/css/tokens/functional/size/breakpoints.css +0 -13
  112. package/tokens-v2-private/css/tokens/functional/size/size-coarse.css +0 -11
  113. package/tokens-v2-private/css/tokens/functional/size/size-fine.css +0 -11
  114. package/tokens-v2-private/css/tokens/functional/size/size.css +0 -54
  115. package/tokens-v2-private/css/tokens/functional/size/viewport.css +0 -6
  116. package/tokens-v2-private/css/tokens/functional/typography/typography.css +0 -47
  117. package/tokens-v2-private/docs/docValues.json +0 -2209
  118. package/tokens-v2-private/js/module/tokens/base/size/size.js +0 -266
  119. package/tokens-v2-private/js/module/tokens/base/typography/typography.js +0 -77
  120. package/tokens-v2-private/js/module/tokens/functional/size/border.js +0 -165
  121. package/tokens-v2-private/js/module/tokens/functional/size/breakpoints.js +0 -101
  122. package/tokens-v2-private/js/module/tokens/functional/size/size-coarse.js +0 -73
  123. package/tokens-v2-private/js/module/tokens/functional/size/size-fine.js +0 -73
  124. package/tokens-v2-private/js/module/tokens/functional/size/size.js +0 -824
  125. package/tokens-v2-private/js/module/tokens/functional/size/viewport.js +0 -101
  126. package/tokens-v2-private/js/module/tokens/functional/typography/typography.js +0 -809
  127. package/tokens-v2-private/js/tokens/base/size/size.js +0 -22
  128. package/tokens-v2-private/js/tokens/base/typography/typography.js +0 -9
  129. package/tokens-v2-private/js/tokens/functional/size/border.js +0 -15
  130. package/tokens-v2-private/js/tokens/functional/size/breakpoints.js +0 -11
  131. package/tokens-v2-private/js/tokens/functional/size/size-coarse.js +0 -8
  132. package/tokens-v2-private/js/tokens/functional/size/size-fine.js +0 -8
  133. package/tokens-v2-private/js/tokens/functional/size/size.js +0 -52
  134. package/tokens-v2-private/js/tokens/functional/size/viewport.js +0 -11
  135. package/tokens-v2-private/js/tokens/functional/typography/typography.js +0 -45
  136. package/tokens-v2-private/scss/tokens/base/size/size.scss +0 -21
  137. package/tokens-v2-private/scss/tokens/base/typography/typography.scss +0 -8
  138. package/tokens-v2-private/scss/tokens/functional/size/border.scss +0 -14
  139. package/tokens-v2-private/scss/tokens/functional/size/breakpoints.scss +0 -10
  140. package/tokens-v2-private/scss/tokens/functional/size/size-coarse.scss +0 -7
  141. package/tokens-v2-private/scss/tokens/functional/size/size-fine.scss +0 -7
  142. package/tokens-v2-private/scss/tokens/functional/size/size.scss +0 -51
  143. package/tokens-v2-private/scss/tokens/functional/size/viewport.scss +0 -10
  144. package/tokens-v2-private/scss/tokens/functional/typography/typography.scss +0 -44
  145. package/tokens-v2-private/ts/tokens/base/size/size.d.ts +0 -31
  146. package/tokens-v2-private/ts/tokens/base/size/size.js +0 -30
  147. package/tokens-v2-private/ts/tokens/base/typography/typography.d.ts +0 -20
  148. package/tokens-v2-private/ts/tokens/base/typography/typography.js +0 -19
  149. package/tokens-v2-private/ts/tokens/functional/size/border.d.ts +0 -28
  150. package/tokens-v2-private/ts/tokens/functional/size/border.js +0 -27
  151. package/tokens-v2-private/ts/tokens/functional/size/breakpoints.d.ts +0 -20
  152. package/tokens-v2-private/ts/tokens/functional/size/breakpoints.js +0 -19
  153. package/tokens-v2-private/ts/tokens/functional/size/size-coarse.d.ts +0 -29
  154. package/tokens-v2-private/ts/tokens/functional/size/size-coarse.js +0 -28
  155. package/tokens-v2-private/ts/tokens/functional/size/size-fine.d.ts +0 -29
  156. package/tokens-v2-private/ts/tokens/functional/size/size-fine.js +0 -28
  157. package/tokens-v2-private/ts/tokens/functional/size/size.d.ts +0 -103
  158. package/tokens-v2-private/ts/tokens/functional/size/size.js +0 -102
  159. package/tokens-v2-private/ts/tokens/functional/size/viewport.d.ts +0 -20
  160. package/tokens-v2-private/ts/tokens/functional/size/viewport.js +0 -19
  161. package/tokens-v2-private/ts/tokens/functional/typography/typography.d.ts +0 -131
  162. package/tokens-v2-private/ts/tokens/functional/typography/typography.js +0 -130
@@ -1,8 +1,7 @@
1
1
  import type StyleDictionary from 'style-dictionary'
2
2
  import {isFontFamily} from '../filters'
3
3
  import {getTokenValue} from './utilities/getTokenValue'
4
-
5
- const hasSpaceInName = (string: string) => /\s/g.test(string)
4
+ import {hasSpaceInString} from './utilities/hasSpaceInString'
6
5
  /**
7
6
  * takes a value and returns it if its a string or concats strings in an array quoting strings with spaces
8
7
  * @param value
@@ -18,7 +17,7 @@ export const parseFontFamily = (value: unknown): string => {
18
17
  if (typeof string !== 'string') {
19
18
  throw new Error(`Invalid value in array ${string}, must be a string`)
20
19
  }
21
- return hasSpaceInName(string) ? `'${string}'` : string
20
+ return hasSpaceInString(string) ? `'${string}'` : string
22
21
  })
23
22
  .join(', ')
24
23
  }
@@ -28,7 +27,7 @@ export const parseFontFamily = (value: unknown): string => {
28
27
  * @description converts fontFamily tokens value to string
29
28
  * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
30
29
  * @matcher matches all tokens of $type `fontFamily`
31
- * @transformer returns a number
30
+ * @transformer returns a string
32
31
  */
33
32
  export const fontFamilyToCss: StyleDictionary.Transform = {
34
33
  type: `value`,
@@ -0,0 +1,47 @@
1
+ import type StyleDictionary from 'style-dictionary'
2
+ import {isFontFamily} from '../filters'
3
+ import {hasSpaceInString} from './utilities/hasSpaceInString'
4
+ /**
5
+ * takes a value and returns it if its a string or concats strings in an array quoting strings with spaces
6
+ * @param value
7
+ * @returns string
8
+ */
9
+ export const parseFontFamily = (
10
+ token: StyleDictionary.TransformedToken,
11
+ fontFamilies: Record<string, string> = {},
12
+ ): string => {
13
+ // return value from fontFamilies
14
+ if (token.name in fontFamilies) {
15
+ return fontFamilies[token.name]
16
+ }
17
+ // return string
18
+ if (typeof token.value === 'string') {
19
+ return token.value
20
+ }
21
+ // return stringified array
22
+ if (Array.isArray(token.value)) {
23
+ return token.value
24
+ .map((string: string) => {
25
+ if (typeof string !== 'string') {
26
+ throw new Error(`Invalid value in array ${string}, must be a string`)
27
+ }
28
+ return hasSpaceInString(string) ? `'${string}'` : string
29
+ })
30
+ .join(', ')
31
+ }
32
+ // invalid value
33
+ throw new Error(`Invalid value ${token.value}, should be a string or array of strings`)
34
+ }
35
+ /**
36
+ * @description converts fontFamily tokens value to string
37
+ * @type value transformer — [StyleDictionary.ValueTransform](https://github.com/amzn/style-dictionary/blob/main/types/Transform.d.ts)
38
+ * @matcher matches all tokens of $type `fontFamily`
39
+ * @transformer returns a string
40
+ */
41
+ export const fontFamilyToFigma: StyleDictionary.Transform = {
42
+ type: `value`,
43
+ transitive: true,
44
+ matcher: isFontFamily,
45
+ transformer: (token: StyleDictionary.TransformedToken, platform: StyleDictionary.Platform): string =>
46
+ parseFontFamily(token, platform.options?.fontFamilies),
47
+ }
@@ -4,12 +4,14 @@ export {colorToHexAlpha} from './colorToHexAlpha'
4
4
  export {colorToHexMix} from './colorToHexMix'
5
5
  export {colorToRgbAlpha} from './colorToRgbAlpha'
6
6
  export {colorToRgbaFloat} from './colorToRgbaFloat'
7
+ export {floatToPixel} from './floatToPixel'
7
8
  export {dimensionToRem} from './dimensionToRem'
8
9
  export {dimensionToRemPxArray} from './dimensionToRemPxArray'
9
10
  export {dimensionToPixelUnitless} from './dimensionToPixelUnitless'
10
11
  export {durationToCss} from './durationToCss'
11
12
  export {figmaAttributes} from './figmaAttributes'
12
13
  export {fontFamilyToCss} from './fontFamilyToCss'
14
+ export {fontFamilyToFigma} from './fontFamilyToFigma'
13
15
  export {fontWeightToNumber} from './fontWeightToNumber'
14
16
  export {jsonDeprecated} from './jsonDeprecated'
15
17
  export {namePathToCamelCase} from './namePathToCamelCase'
@@ -0,0 +1,6 @@
1
+ export const hasSpaceInString = (string: string): boolean => {
2
+ if (typeof string !== 'string') {
3
+ throw new TypeError(`Invalid input. Input must be a string, ${typeof string} provided`)
4
+ }
5
+ return /\s/g.test(string)
6
+ }
package/build.js DELETED
@@ -1,578 +0,0 @@
1
- const glob = require('fast-glob')
2
- const StyleDictionary = require('style-dictionary')
3
-
4
- const {fileHeader, formattedVariables} = StyleDictionary.formatHelpers
5
-
6
- //-----
7
- // functions to be extracted
8
- // TODO: extract to a separate files
9
-
10
- const pathToKebabCase = token => token.path.join('-')
11
-
12
- const pathToDotNotation = token => token.path.join('.')
13
-
14
- const capitalize = string => string[0].toUpperCase() + string.slice(1)
15
-
16
- const pathToPascalCase = token => token.path.map(tokenPathItems => capitalize(tokenPathItems)).join('')
17
-
18
- // REGISTER THE CUSTOM TRANFORMS
19
-
20
- /**
21
- * transform: scss variable names
22
- * example: `$namespace-item-variant-property-modifier`
23
- */
24
- StyleDictionary.registerTransform({
25
- name: 'name/scss',
26
- type: 'name',
27
- transformer: pathToKebabCase,
28
- })
29
-
30
- /**
31
- * transform: css variable names
32
- * example: `--namespace-item-variant-property-modifier`
33
- */
34
- StyleDictionary.registerTransform({
35
- name: 'name/css',
36
- type: 'name',
37
- transformer: pathToKebabCase,
38
- })
39
-
40
- /**
41
- * transform: js variable names
42
- * example: `namespace.item.variant.property.modifier`
43
- */
44
- StyleDictionary.registerTransform({
45
- name: 'name/js',
46
- type: 'name',
47
- transformer: pathToDotNotation,
48
- })
49
-
50
- /**
51
- * transform: js es6 variable names
52
- * example: `NamespaceItemVariantPropertyModifier`
53
- */
54
- StyleDictionary.registerTransform({
55
- name: 'name/js/es6',
56
- type: 'name',
57
- transformer: pathToPascalCase,
58
- })
59
-
60
- // find values with px unit
61
- function isPx(value) {
62
- return /[\d.]+px$/.test(value)
63
- }
64
-
65
- // transform: px to rem
66
- StyleDictionary.registerTransform({
67
- name: 'pxToRem',
68
- type: 'value',
69
- transformer: token => {
70
- if (isPx(token.value)) {
71
- const baseFontSize = 16
72
- const floatValue = parseFloat(token.value.replace('px', ''))
73
-
74
- if (isNaN(floatValue)) {
75
- return token.value
76
- }
77
-
78
- if (floatValue === 0) {
79
- return '0'
80
- }
81
-
82
- if (token.name.includes('lineHeight')) {
83
- return `${floatValue / baseFontSize}`
84
- }
85
-
86
- return `${floatValue / baseFontSize}rem`
87
- }
88
- return token.value
89
- },
90
- })
91
-
92
- //-----
93
-
94
- // ts output
95
- StyleDictionary.registerTransform({
96
- name: 'attribute/typescript',
97
- type: 'attribute',
98
- transformer: token => {
99
- return {
100
- typescript: {
101
- // these transforms will need to match the ones you use for typescript
102
- // or you can "chain" the transforms and use token.name and token.value
103
- // like scss and less transforms do.
104
- name: token.path.slice(1).join('.'),
105
- value: token.value,
106
- },
107
- }
108
- },
109
- })
110
-
111
- // css output
112
- StyleDictionary.registerTransform({
113
- name: 'attribute/css',
114
- type: 'attribute',
115
- transformer: token => {
116
- const tokenName = token.path.slice(1).join('-')
117
- return {
118
- css: {
119
- name: `--${tokenName}`,
120
- value: token.value,
121
- },
122
- }
123
- },
124
- })
125
-
126
- // transform: composite typography to shorthands
127
- StyleDictionary.registerTransform({
128
- name: 'typography/shorthand',
129
- type: 'value',
130
- transitive: true,
131
- matcher: token => token.type === 'typography',
132
- transformer: token => {
133
- const {value} = token
134
-
135
- // if lineHeight has value, include in shorthand
136
- if (value.lineHeight) {
137
- return `${value.fontWeight} ${value.fontSize}/${value.lineHeight} ${value.fontFamily}`
138
- }
139
-
140
- return `${value.fontWeight} ${value.fontSize} ${value.fontFamily}`
141
- },
142
- })
143
-
144
- // REGISTER THE CUSTOM TRANFORM GROUPS
145
-
146
- StyleDictionary.registerTransformGroup({
147
- name: 'css',
148
- transforms: ['name/css', 'pxToRem', 'typography/shorthand'],
149
- })
150
-
151
- StyleDictionary.registerTransformGroup({
152
- name: 'scss',
153
- transforms: ['name/scss', 'pxToRem', 'typography/shorthand'],
154
- })
155
-
156
- // REGISTER A CUSTOM FORMAT
157
-
158
- // wrap mobile tokens in media query
159
- StyleDictionary.registerFormat({
160
- name: 'css/touch-target-mobile',
161
- formatter({dictionary, file, options}) {
162
- const {outputReferences} = options
163
- return `${fileHeader({file})}
164
- @media (pointer: coarse) { :root {\n${formattedVariables({
165
- format: 'css',
166
- dictionary,
167
- outputReferences,
168
- })}\n}}\n`
169
- },
170
- })
171
-
172
- // wrap desktop tokens in media query
173
- StyleDictionary.registerFormat({
174
- name: 'css/touch-target-desktop',
175
- formatter({dictionary, file, options}) {
176
- const {outputReferences} = options
177
- return `${fileHeader({file})}
178
- @media (pointer: fine) { :root {\n${formattedVariables({
179
- format: 'css',
180
- dictionary,
181
- outputReferences,
182
- })}\n}}\n`
183
- },
184
- })
185
-
186
- StyleDictionary.registerFormat({
187
- name: 'custom/format/custom-media',
188
- formatter({dictionary}) {
189
- return dictionary.allProperties
190
- .map(prop => {
191
- const {value, name} = prop
192
-
193
- return `@custom-media --${name}-viewport ${value};`
194
- })
195
- .join('\n')
196
- },
197
- })
198
-
199
- // format docs
200
- StyleDictionary.registerFormat({
201
- name: 'json/docs',
202
- formatter({dictionary}) {
203
- const groupedTokens = groupBy(dictionary.allProperties, 'filePath')
204
-
205
- return JSON.stringify(groupedTokens, null, 2)
206
- },
207
- })
208
-
209
- /**
210
- * Replacement format for javascript/module
211
- */
212
- StyleDictionary.registerFormat({
213
- name: 'javascript/module-v2',
214
- formatter({dictionary, file}) {
215
- const recursiveleyFlattenDictionary = obj => {
216
- const tree = {}
217
- if (typeof obj !== 'object' || Array.isArray(obj)) {
218
- return obj
219
- }
220
-
221
- if (obj.hasOwnProperty('value')) {
222
- return obj.value
223
- } else {
224
- for (const name in obj) {
225
- if (obj.hasOwnProperty(name)) {
226
- tree[name] = recursiveleyFlattenDictionary(obj[name])
227
- }
228
- }
229
- }
230
- return tree
231
- }
232
-
233
- return `${fileHeader({file})}
234
-
235
- module.exports = ${JSON.stringify(recursiveleyFlattenDictionary(dictionary.tokens), null, 2)}`
236
- },
237
- })
238
-
239
- /**
240
- * Replacement format for typescript/module-declarations
241
- * Type schema corresponds to javascript/module-v2 format
242
- */
243
- StyleDictionary.registerFormat({
244
- name: 'typescript/module-declarations-v2',
245
- formatter({dictionary, options, file}) {
246
- const {moduleName = `tokens`} = options
247
-
248
- const getType = value => {
249
- switch (typeof value) {
250
- case 'string':
251
- return 'string'
252
- case 'number':
253
- return 'number'
254
- default:
255
- return 'any'
256
- }
257
- }
258
-
259
- const recursiveTypeGeneration = obj => {
260
- const tree = {}
261
- const shortHandSizes = ['large', 'medium', 'small']
262
- if (typeof obj !== 'object' || Array.isArray(obj)) {
263
- return obj
264
- }
265
-
266
- if (obj.hasOwnProperty('value') && typeof obj.value === 'string') {
267
- return getType(obj.value)
268
- } else {
269
- for (const name in obj) {
270
- if ((obj.hasOwnProperty(name) && obj.name === 'shorthand') || shortHandSizes.includes(obj.name)) {
271
- for (const shorthandKey in obj.value) {
272
- tree[shorthandKey] = getType(obj.value[shorthandKey])
273
- }
274
- return tree
275
- } else if (obj.hasOwnProperty(name)) {
276
- tree[name] = recursiveTypeGeneration(obj[name])
277
- }
278
- }
279
- }
280
- return tree
281
- }
282
-
283
- const output = `${fileHeader({file})}
284
-
285
- declare const ${moduleName}: ${JSON.stringify(recursiveTypeGeneration(dictionary.tokens), null, 2)}
286
- export default ${moduleName};`
287
-
288
- return output
289
- .replace(/"any"/g, 'any')
290
- .replace(/"string"/g, 'string')
291
- .replace(/"number"/g, 'number')
292
- },
293
- })
294
-
295
- /**
296
- * @name groupBy
297
- * @description Equivalent to lodash _.groupBy, to avoid creating another package dependency for users
298
- * @param {Array|Object} The collection to iterate over.
299
- * @param {Function} The iteratee to transform keys.
300
- * @returns {Object} Returns the composed aggregate object.
301
- */
302
- function groupBy(collection, iteratee = x => x) {
303
- const current = typeof iteratee === 'function' ? iteratee : ({[iteratee]: prop}) => prop
304
-
305
- const array = Array.isArray(collection) ? collection : Object.values(collection)
306
-
307
- return array.reduce((acc, item) => {
308
- const value = current(item)
309
-
310
- acc[value] = acc[value] || []
311
-
312
- acc[value].push(item)
313
-
314
- return acc
315
- }, {})
316
- }
317
-
318
- /**
319
- * @name build
320
- * @description
321
- * Used to generate design tokens programmatically using StyleDictionary
322
- * Called internally to build primitives and exported for self-serve use
323
- *
324
- * @param {Object} options
325
- * @param {string} options.source glob or file path to a JSON object of tokens
326
- * @param {string} options.outputPath location to write the output files
327
- * @param {string} options.namespace a custom namespace to use for the output files
328
- * @param {Platform} options.platforms add custom platform configurations to style-dictionary
329
- * @example
330
- * buildPrimitives({
331
- * source: [`src/colors.json`],
332
- * outputPath: 'dist',
333
- * namespace: 'primer',
334
- * platforms: {...}
335
- * })
336
- */
337
- function buildPrimitives(
338
- {source, outputPath = 'tokens-v2-private', include, platforms, namespace = 'primer'},
339
- _StyleDictionary = StyleDictionary,
340
- ) {
341
- // eslint-disable-next-line no-console
342
- console.log('Build started...')
343
- // eslint-disable-next-line no-console
344
- console.log('\n==============================================')
345
-
346
- const customParseConfig = {
347
- parsers: [
348
- {
349
- pattern: /\.json$/,
350
- parse: ({contents, filePath}) => {
351
- try {
352
- let mutableContent = JSON.stringify(contents)
353
-
354
- if (filePath.includes('/functional/')) {
355
- mutableContent = mutableContent.replace(/<namespace>/g, namespace)
356
- }
357
-
358
- const parsed = JSON.parse(mutableContent)
359
-
360
- return JSON.parse(parsed)
361
- } catch (error) {
362
- // eslint-disable-next-line no-console
363
- console.log(error)
364
- }
365
- },
366
- },
367
- ],
368
- }
369
-
370
- const getConfig = files => {
371
- const defaultPlatformConfig = {
372
- css: {
373
- buildPath: `${outputPath}/css/`,
374
- transformGroup: 'css',
375
- // map the array of token file paths to style dictionary output files
376
- files: files.map(filePath => {
377
- return {
378
- format: `css/variables`,
379
- destination: filePath.replace(`.json`, `.css`),
380
- filter: token => token.filePath === filePath,
381
- options: {
382
- outputReferences: true,
383
- },
384
- }
385
- }),
386
- },
387
- cssViewport: {
388
- buildPath: `${outputPath}/css/tokens/functional/size/`,
389
- transformGroup: 'css',
390
- files: [
391
- {
392
- filter: token => token.filePath.includes('viewport'),
393
- format: 'custom/format/custom-media',
394
- destination: 'viewport.css',
395
- },
396
- ],
397
- },
398
- scss: {
399
- buildPath: `${outputPath}/scss/`,
400
- transformGroup: 'scss',
401
- // map the array of token file paths to style dictionary output files
402
- files: files.map(filePath => {
403
- return {
404
- format: `scss/variables`,
405
- destination: filePath.replace(`.json`, `.scss`),
406
- filter: token => token.filePath === filePath,
407
- options: {
408
- outputReferences: true,
409
- },
410
- }
411
- }),
412
- },
413
- js: {
414
- buildPath: `${outputPath}/js/`,
415
- transforms: ['name/js/es6', 'pxToRem'],
416
- // map the array of token file paths to style dictionary output files
417
- files: files.map(filePath => {
418
- return {
419
- format: `javascript/es6`,
420
- destination: filePath.replace(`.json`, `.js`),
421
- filter: token => token.filePath === filePath,
422
- }
423
- }),
424
- },
425
- jsModule: {
426
- buildPath: `${outputPath}/js/module/`,
427
- transforms: ['pxToRem'],
428
- // map the array of token file paths to style dictionary output files
429
- files: files.map(filePath => {
430
- return {
431
- format: `javascript/module`,
432
- destination: filePath.replace(`.json`, `.js`),
433
- filter: token => token.filePath === filePath,
434
- }
435
- }),
436
- },
437
- tsTypes: {
438
- buildPath: `${outputPath}/ts/`,
439
- transforms: ['pxToRem'],
440
- // map the array of token file paths to style dictionary output files
441
- files: files.map(filePath => {
442
- return {
443
- format: `typescript/module-declarations-v2`,
444
- destination: filePath.replace(`.json`, `.d.ts`),
445
- filter: token => token.filePath === filePath,
446
- }
447
- }),
448
- },
449
- ts: {
450
- buildPath: `${outputPath}/ts/`,
451
- transforms: ['pxToRem'],
452
- // map the array of token file paths to style dictionary output files
453
- files: files.map(filePath => {
454
- return {
455
- format: `javascript/module-v2`,
456
- destination: filePath.replace(`.json`, `.js`),
457
- filter: token => token.filePath === filePath,
458
- }
459
- }),
460
- },
461
- }
462
-
463
- const config = {
464
- ...{include: include ? [include] : undefined},
465
- source: files,
466
- ...customParseConfig,
467
- platforms: platforms ? platforms : defaultPlatformConfig,
468
- }
469
- return config
470
- }
471
-
472
- //build all tokens
473
- _StyleDictionary
474
- .extend({
475
- ...getConfig(glob.sync([...source])),
476
- })
477
- .buildAllPlatforms()
478
- }
479
-
480
- /**
481
- * @name init
482
- * @description
483
- * Triggers the build for @primer/primitive default tokens
484
- * from an npm script. Internal use only. Use `build` for self-serve.
485
- * @private
486
- */
487
- function _init() {
488
- const outputPath = 'tokens-v2-private'
489
- //build all tokens
490
- buildPrimitives({
491
- source: [`tokens/**/*.json`, `!tokens/**/size-*.json`, `!tokens/**/color/*.json`, `!tokens/**/shadow/*.json`],
492
- outputPath,
493
- })
494
-
495
- //build size fine
496
- buildPrimitives({
497
- source: [`tokens/functional/size/size-fine.json`, `tokens/base/size/size.json`],
498
- outputPath,
499
- })
500
-
501
- //build size coarse
502
- buildPrimitives({
503
- source: [`tokens/functional/size/size-coarse.json`, `tokens/base/size/size.json`],
504
- outputPath,
505
- })
506
-
507
- buildPrimitives({
508
- source: [`tokens/base/size/size.json`, `tokens/functional/size/size-fine.json`],
509
- outputPath,
510
- platforms: {
511
- css: {
512
- buildPath: `${outputPath}/css/`,
513
- transformGroup: 'css',
514
- files: [
515
- {
516
- destination: `tokens/functional/size/size-fine.css`,
517
- format: `css/touch-target-desktop`,
518
- filter: token => token.filePath.includes('fine'),
519
- options: {
520
- outputReferences: true,
521
- },
522
- },
523
- ],
524
- },
525
- },
526
- })
527
-
528
- buildPrimitives({
529
- source: [`tokens/base/size/size.json`, `tokens/functional/size/size-coarse.json`],
530
- platforms: {
531
- css: {
532
- buildPath: `${outputPath}/css/`,
533
- transformGroup: 'css',
534
- files: [
535
- {
536
- destination: `tokens/functional/size/size-coarse.css`,
537
- format: `css/touch-target-mobile`,
538
- filter: token => token.filePath.includes('coarse'),
539
- options: {
540
- outputReferences: true,
541
- },
542
- },
543
- ],
544
- },
545
- },
546
- })
547
-
548
- //build docs data
549
- buildPrimitives({
550
- source: [`tokens/**/*.json`, `!tokens/**/color/*.json`, `!tokens/**/shadow/*.json`],
551
- outputPath,
552
- platforms: {
553
- docs: {
554
- buildPath: `${outputPath}/docs/`,
555
- transformGroup: 'css',
556
- files: [
557
- {
558
- format: 'json/docs',
559
- destination: 'docValues.json',
560
- options: {
561
- outputReferences: false,
562
- },
563
- },
564
- ],
565
- },
566
- },
567
- })
568
- }
569
-
570
- module.exports = {
571
- buildPrimitives,
572
- _init,
573
- StyleDictionary,
574
- pathToKebabCase,
575
- pathToDotNotation,
576
- capitalize,
577
- pathToPascalCase,
578
- }