@nycds/variables 0.2.2 → 0.3.0-prerelease.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.
package/scripts/build.js DELETED
@@ -1,72 +0,0 @@
1
- import StyleDictionary from 'style-dictionary'
2
- import { resolve } from 'path'
3
-
4
- const SRCDIR = resolve(process.cwd(), './tokens/dist')
5
- const DESTDIR = resolve(process.cwd(), './dist')
6
-
7
- const baseTokens = ['ref', 'light', 'cmp']
8
- const themes = ['dark']
9
-
10
- StyleDictionary.registerTransform({
11
- name: 'nyc/length/px',
12
- type: 'value',
13
- matcher: (token) => token.path.includes('length'),
14
- transformer: (token) => `${token.value}px`,
15
- })
16
-
17
- StyleDictionary.registerTransform({
18
- name: 'nyc/size/clamp',
19
- type: 'value',
20
- matcher: (token) => token.path.includes('length'),
21
- transformer: (token) => `${token.value}px`,
22
- })
23
-
24
- const cssConfig = {
25
- prefix: 'nyc',
26
- transforms: ['attribute/cti', 'name/cti/kebab', 'nyc/length/px', 'color/hsl'],
27
- buildPath: `${DESTDIR}/`,
28
- }
29
-
30
- const getThemeConfig = (theme) => ({
31
- include: [`${SRCDIR}/nyc.ref.tokens.json`],
32
- source: [`${SRCDIR}/**/nyc.${theme}.tokens.json`],
33
- platforms: {
34
- css: {
35
- ...cssConfig,
36
- files: [
37
- {
38
- destination: `${theme}.css`,
39
- format: 'css/variables',
40
- options: {
41
- outputReferences: true,
42
- selector: `:root${theme != 'light' ? '[data-theme="' + theme + '"]' : ''}`,
43
- },
44
- },
45
- ],
46
- },
47
- },
48
- })
49
-
50
- const baseVariables = StyleDictionary.extend({
51
- include: baseTokens.map((t) => `${SRCDIR}/nyc.${t}.tokens.json`),
52
- platforms: {
53
- css: {
54
- ...cssConfig,
55
- files: [
56
- {
57
- destination: 'variables.css',
58
- format: 'css/variables',
59
- options: {
60
- outputReferences: true,
61
- },
62
- },
63
- ],
64
- },
65
- },
66
- })
67
-
68
- baseVariables.buildAllPlatforms()
69
-
70
- themes.map((theme) => {
71
- StyleDictionary.extend(getThemeConfig(theme)).buildAllPlatforms()
72
- })
@@ -1,144 +0,0 @@
1
- import { readFileSync, writeFile, existsSync, mkdirSync } from 'fs'
2
- import { resolve } from 'path'
3
-
4
- const roundTo4 = (num) => roundToX(num, 4)
5
-
6
- const roundToX = (num, decimals) => {
7
- return +(Math.round(num + 'e' + decimals) + 'e-' + decimals)
8
- }
9
-
10
- const SRCDIR = resolve(process.cwd(), './tokens/src')
11
- const DESTDIR = resolve(process.cwd(), './tokens/dist')
12
-
13
- if (!existsSync(DESTDIR)) {
14
- mkdirSync(DESTDIR)
15
- }
16
-
17
- const data = readFileSync(`${SRCDIR}/nyc.tokens.json`, {})
18
-
19
- const tokens = JSON.parse(data)
20
-
21
- const getThemes = (obj) => {
22
- return Object.keys(obj.sys)
23
- }
24
-
25
- const themes = getThemes(tokens)
26
-
27
- // console.log(data, themes);
28
-
29
- const writeTokenFile = (obj, filename) => {
30
- const objStr = JSON.stringify(obj, null, 2)
31
- writeFile(filename, objStr, (error) => {
32
- if (error) {
33
- console.log('An error has occurred ', error)
34
- return
35
- }
36
- console.log('Data written successfully to disk')
37
- })
38
- }
39
-
40
- const removeThemeRefs = (obj, themes) => {
41
- const regex = new RegExp(
42
- `\\.(${themes.toString().replaceAll(',', '|')})`,
43
- 'g',
44
- )
45
- return JSON.parse(JSON.stringify(obj).replace(regex, ''))
46
- }
47
-
48
- const removeClampRefs = (obj) => {
49
- // const regex = new RegExp(`\\.(${themes.toString().replace(',','|')})`, 'g');
50
- const tokensStr = JSON.stringify(obj)
51
- const matches = [
52
- ...tokensStr.matchAll(new RegExp('fluid\\.[A-Za-z0-9.-]+\\.max', 'g')),
53
- ]
54
- // console.log(matches);
55
- const newStr = matches.reduce((acc, cur) => {
56
- return acc.replaceAll(cur[0], cur[0].substring(0, cur[0].length - 4))
57
- }, tokensStr)
58
- // console.log(newStr)
59
- const tokens = JSON.parse(newStr)
60
- // console.log(tokens);
61
- return tokens
62
- }
63
-
64
- const getTokenValue = (reference, tokens) => {
65
- //console.log(reference, reference.replace(/[{}]/g, '').split('.'))
66
- let ref = reference
67
- if (typeof ref === 'string' && ref.includes('sys')) {
68
- ref = `${ref.slice(0, 5)}light.${ref.slice(5)}`
69
- }
70
- //console.log(ref, ref.replace(/[{}]/g, '').split('.'))
71
- const val = ref
72
- .replace(/[{}]/g, '')
73
- .split('.')
74
- .reduce((acc, cur) => acc[cur], tokens).value
75
- if (typeof val === 'string' && val.includes('ref')) {
76
- return getTokenValue(val, tokens)
77
- }
78
- return val
79
- }
80
-
81
- const getClampVal = (pageMin, pageMax, minVal, maxVal) => {
82
- const factor = (1 / (pageMax - pageMin)) * (maxVal - minVal)
83
- const calc1 = `${roundTo4(minVal - pageMin * factor)}rem`
84
- const calc2 = `${roundTo4(100 * factor)}vw`
85
- const calcV = `${calc1} + ${calc2}`
86
- const clamp = `clamp(${minVal}rem, ${calcV}, ${maxVal}rem)`
87
- return clamp
88
- }
89
-
90
- const get = (t, path) => path.split('.').reduce((r, k) => r?.[k], t)
91
-
92
- const set = (t, path, value) => {
93
- if (typeof t != 'object') throw Error('non-object')
94
- if (path == '') throw Error('empty path')
95
- const pos = path.indexOf('.')
96
- return pos == -1
97
- ? ((t[path] = value), value)
98
- : set(t[path.slice(0, pos)], path.slice(pos + 1), value)
99
- }
100
-
101
- const handleFluidTokens = (path, tokens, theme, pageMin, pageMax) => {}
102
-
103
- const clampTokens = (tokens, theme) => {
104
- const pageMin =
105
- getTokenValue(tokens.sys[theme].size.width.page.min.value, tokens) / 16
106
- const pageMax =
107
- getTokenValue(tokens.sys[theme].size.width.page.max.value, tokens) / 16
108
-
109
- const paths = ['size.text.fluid', 'space.fluid', 'radius.fluid']
110
- paths.forEach((path) => {
111
- const o = get(tokens.sys[theme], path)
112
- if (!o) return
113
- Object.keys(o).map((k) => {
114
- const minV = getTokenValue(o[k].min.value, tokens) / 16
115
- const maxV = getTokenValue(o[k].max.value, tokens) / 16
116
- const clamp = getClampVal(pageMin, pageMax, minV, maxV)
117
- set(tokens.sys[theme], `${path}.${k}`, {
118
- type: 'dimension',
119
- value: clamp,
120
- })
121
- })
122
- })
123
- //console.log(tokens.sys[theme].size.text.fluid)
124
- return removeClampRefs(tokens)
125
- }
126
-
127
- // The token file includes references to Figma variable modes which we
128
- // dont want, like 'dark' in {sys.dark.space.lg}, so we strip them out.
129
- // Include "value" here to remove mode references in cmp tier.
130
- const cleanTokens = removeThemeRefs(tokens, [...themes, 'value'])
131
-
132
- themes.forEach((theme) => {
133
- const filename = `${DESTDIR}/nyc.${theme}.tokens.json`
134
- const transformedTokens = clampTokens(cleanTokens, theme)
135
- writeTokenFile({ sys: transformedTokens.sys[theme] }, filename)
136
- })
137
-
138
- const tiers = ['ref', 'cmp']
139
-
140
- tiers.forEach((tier) => {
141
- const filename = `${DESTDIR}/nyc.${tier}.tokens.json`
142
- const transformedTokens = removeClampRefs(cleanTokens)
143
- writeTokenFile({ [tier]: transformedTokens[tier] }, filename)
144
- })