winduum 0.2.2 → 0.2.3-next.2

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/utils/tailwind.js CHANGED
@@ -1,19 +1,54 @@
1
1
  import plugin from 'tailwindcss/plugin'
2
- import twColors from 'tailwindcss/colors'
2
+ import flattenColorPalette from 'tailwindcss/src/util/flattenColorPalette'
3
+ import toColorValue from 'tailwindcss/src/util/toColorValue'
4
+ import { parseColor, formatColor } from 'tailwindcss/src/util/color'
3
5
  import lodash from 'lodash'
4
6
 
7
+ function withAlphaVariable ({ color, property, variable }) {
8
+ const properties = [].concat(property)
9
+ if (typeof color === 'function') {
10
+ return {
11
+ ...Object.fromEntries(
12
+ properties.map((p) => {
13
+ return [p, color({ opacityVariable: variable, opacityValue: `var(${variable}, 1)` })]
14
+ })
15
+ )
16
+ }
17
+ }
18
+
19
+ const parsed = parseColor(color)
20
+
21
+ if (parsed === null) {
22
+ return Object.fromEntries(properties.map((p) => [p, color]))
23
+ }
24
+
25
+ if (parsed.alpha !== undefined) {
26
+ // Has an alpha value, return color as-is
27
+ return Object.fromEntries(properties.map((p) => [p, color]))
28
+ }
29
+
30
+ return {
31
+ ...Object.fromEntries(
32
+ properties.map((p) => {
33
+ return [p, formatColor({ ...parsed, alpha: `var(${variable}, 1)` })]
34
+ })
35
+ )
36
+ }
37
+ }
38
+
5
39
  export const defaultConfig = {
6
40
  colors: [
7
- 'light', 'dark', 'primary',
8
- 'warning', 'error', 'info', 'success', 'accent', 'current',
9
- 'base', 'base-primary', 'base-secondary', 'base-tertiary',
41
+ 'primary', 'accent', 'current',
42
+ 'warning', 'error', 'info', 'success', 'light', 'dark',
43
+ 'main', 'main-primary', 'main-secondary', 'main-tertiary',
10
44
  'body', 'body-primary', 'body-secondary', 'body-tertiary'
11
45
  ],
12
46
  fontFamily: ['primary', 'secondary'],
13
47
  fontWeight: ['light', 'normal', 'medium', 'semibold', 'bold', 'extrabold'],
14
48
  zIndex: [10, 20, 30, 40, 50, 60],
15
- spacing: ['xs', 'sm', 'base', 'md', 'lg', 'xl', '2xl', '3xl', 'section'],
16
- borderRadius: ['xs', 'sm', 'base', 'md', 'lg', 'xl', '2xl', '3xl', 'full'],
49
+ fontSize: ['xs', 'sm', 'base', 'md', 'lg', 'xl', '2xl', '3xl', '3xl', '4xl', '5xl', '6xl', '7xl', '7xl', '8xl', '9xl'],
50
+ spacing: ['xs', 'sm', 'md', 'lg', 'xl', '2xl', '3xl', '4xl', '5xl', '6xl', 'section'],
51
+ borderRadius: ['xs', 'sm', 'base', 'md', 'lg', 'xl', '2xl', '3xl', '4xl', '5xl', '6xl', 'full'],
17
52
  animations: ['fade-in', 'fade-out', 'fade-in-down', 'fade-out-up', 'ripple', 'spin', 'move-indeterminate'],
18
53
  screens: {
19
54
  xs: '22.5em',
@@ -26,101 +61,35 @@ export const defaultConfig = {
26
61
  '4xl': '100em',
27
62
  xxl: '126em',
28
63
  '2xxl': '158em'
64
+ },
65
+ settings: {
66
+ rgb: true
29
67
  }
30
68
  }
31
69
 
32
- export const hexToRgb = hex => hex.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (m, r, g, b) => '#' + r + r + g + g + b + b)
33
- .substring(1).match(/.{2}/g)
34
- .map(x => parseInt(x, 16))
35
-
36
- export const getTailwindColors = (twColors) => {
37
- const accentColors = []
38
-
39
- Object.keys(twColors).forEach(color => {
40
- if (color.match(/(lightBlue|warmGray|trueGray|coolGray|blueGray)/)) {
41
- return
42
- }
43
-
44
- if (typeof twColors[color] === 'object') {
45
- Object.keys(twColors[color]).forEach(variant => {
46
- accentColors.push([`${color.replace(/[A-Z]/g, m => '-' + m.toLowerCase())}-${variant}`, twColors[color][variant]])
47
- })
48
- } else {
49
- accentColors.push([color, twColors[color]])
50
- }
51
- })
52
-
53
- return accentColors
54
- }
55
-
56
70
  export const tailwindColors = (colors = []) => {
57
71
  colors.forEach(name => {
58
- colors[name + '-rgb'] = ({ opacityValue }) => {
59
- if (opacityValue === undefined) {
60
- return `rgb(var(--color-${name}-rgb))`
61
- }
62
- return `rgb(var(--color-${name}-rgb) / ${opacityValue})`
72
+ if (defaultConfig.settings.rgb) {
73
+ colors[name + '-rgb'] = `rgb(var(--color-${name}-rgb) / <alpha-value>)`
63
74
  }
64
75
 
65
- colors[name] = ({ opacityValue }) => {
66
- if (opacityValue === undefined) {
67
- return `var(--color-${name})`
68
- }
69
-
70
- return `color-mix(in sRGB, var(--color-${name}) calc(${opacityValue} * 100%), transparent)`
71
- }
76
+ colors[name] = `color-mix(in sRGB, var(--color-${name}) calc(<alpha-value> * 100%), transparent)`
72
77
  })
73
78
 
74
79
  return colors
75
80
  }
76
81
 
77
- export const tailwindColorsAccent = (colors = []) => {
78
- const result = {}
79
-
80
- colors.forEach(color => {
81
- if (Array.isArray(color)) {
82
- const rgb = hexToRgb(color[1])
83
-
84
- result[`.accent-${color[0]}`] = {
85
- '--color-accent-rgb': `${rgb[0]} ${rgb[1]} ${rgb[2]}`,
86
- '--color-accent': `rgb(${rgb[0]} ${rgb[1]} ${rgb[2]})`,
87
- '--color-accent-current': `var(--color-${color}-current, var(--color-light))`
88
- }
89
- } else {
90
- result[`.accent-${color}`] = {
91
- '--color-accent-rgb': `var(--color-${color}-rgb)`,
92
- '--color-accent': `var(--color-${color})`,
93
- '--color-accent-current': `var(--color-${color}-current, var(--color-light))`
94
- }
95
- }
96
- })
97
-
98
- return result
99
- }
100
-
101
- export const tailwindColorsCurrent = (colors = []) => {
102
- const result = {}
103
-
104
- colors.forEach(color => {
105
- if (Array.isArray(color)) {
106
- const rgb = hexToRgb(color[1])
107
-
108
- result[`.text-${color[0]}`] = {
109
- '--color-current': `${rgb[0]} ${rgb[1]} ${rgb[2]}`
110
- }
111
- } else {
112
- result[`.text-${color}`] = {
113
- '--color-current': `var(--color-${color})`
114
- }
115
- }
82
+ export const tailwindVariables = (type, variables = [], values = {}) => {
83
+ variables.forEach(name => {
84
+ values[name] = `var(--${type}-${name})`
116
85
  })
117
86
 
118
- return result
87
+ return values
119
88
  }
120
89
 
121
- export const tailwindVariables = (type, variables = [], values = {}) => {
90
+ export const tailwindVariablesFont = (type, variables = [], values = {}) => {
122
91
  variables.forEach(name => {
123
- values[name] = `var(--${type}-${name})`
92
+ values[name] = [`var(--${type}-${name})`, `calc(var(--${type}-${name}) + 0.5rem)`]
124
93
  })
125
94
 
126
95
  return values
@@ -141,9 +110,104 @@ export const tailwindAnimations = (values) => {
141
110
  export const createPlugin = (userConfig = {}) => {
142
111
  userConfig = lodash.merge(defaultConfig, userConfig)
143
112
 
144
- return plugin(({ addUtilities, theme, variants, e }) => {
145
- addUtilities(Object.assign(tailwindColorsAccent(getTailwindColors(twColors)), tailwindColorsAccent(userConfig.colors)))
146
- addUtilities(Object.assign(tailwindColorsCurrent(getTailwindColors(twColors)), tailwindColorsCurrent(userConfig.colors)))
113
+ return plugin(({ addUtilities, matchUtilities, theme, variants, e, corePlugins }) => {
114
+ matchUtilities(
115
+ {
116
+ accent: (value) => {
117
+ const matchValue = toColorValue(value).match(/var\((.*?)\)/)
118
+ const fallbackRgb = matchValue && matchValue[0].includes('-rgb')
119
+
120
+ const colorProperties = {}
121
+
122
+ if (fallbackRgb) {
123
+ colorProperties['--color-accent-rgb'] = matchValue[0]
124
+ }
125
+
126
+ if ((matchValue && matchValue[0] === 'var(--color-accent)') || (matchValue && matchValue[0] === 'var(--color-accent-rgb)')) {
127
+ return {
128
+ 'accent-color': matchValue && toColorValue(value).includes('calc(1 * 100%)') ? matchValue[0] : toColorValue(value)
129
+ }
130
+ }
131
+
132
+ if (matchValue) {
133
+ if (fallbackRgb) {
134
+ colorProperties['--color-accent-fg-rgb'] = `var(${matchValue[1].replace('-rgb', '-fg-rgb')})`
135
+ }
136
+
137
+ if (toColorValue(value).includes('calc(1 * 100%)') || toColorValue(value).includes(' / 1')) {
138
+ return {
139
+ ...colorProperties,
140
+ '--color-accent': fallbackRgb ? toColorValue(value) : matchValue[0],
141
+ '--color-accent-fg': fallbackRgb ? `rgb(var(${matchValue[1].replace('-rgb', '-fg-rgb')}, var(--color-light-rgb)))` : `var(${matchValue[1]}-fg, var(--color-light))`,
142
+ 'accent-color': 'var(--color-accent)'
143
+ }
144
+ } else {
145
+ return {
146
+ ...colorProperties,
147
+ '--color-accent': toColorValue(value),
148
+ '--color-accent-fg': fallbackRgb ? `rgb(var(${matchValue[1].replace('-rgb', '-fg-rgb')}, var(--color-light-rgb)))` : `var(${matchValue[1]}-fg, var(--color-light))`,
149
+ 'accent-color': 'var(--color-accent)'
150
+ }
151
+ }
152
+ }
153
+
154
+ return {
155
+ '--color-accent': toColorValue(value),
156
+ 'accent-color': toColorValue(value)
157
+ }
158
+ }
159
+ },
160
+ { values: flattenColorPalette(theme('accentColor')), type: ['color', 'any'] }
161
+ )
162
+ matchUtilities(
163
+ {
164
+ text: (value) => {
165
+ const matchValue = toColorValue(value).match(/var\((.*?)\)/)
166
+ const fallbackRgb = matchValue && matchValue[0].includes('-rgb')
167
+
168
+ const colorProperties = {}
169
+
170
+ if (fallbackRgb) {
171
+ colorProperties['--color-current-rgb'] = matchValue[0]
172
+ }
173
+
174
+ if ((matchValue && matchValue[0] === 'var(--color-current)') || (matchValue && matchValue[0] === 'var(--color-current-rgb)')) {
175
+ return {
176
+ color: toColorValue(value)
177
+ }
178
+ }
179
+
180
+ const color = matchValue
181
+ ? { color: 'var(--color-current)' }
182
+ : {
183
+ ...withAlphaVariable({
184
+ color: value,
185
+ property: 'color',
186
+ variable: '--tw-text-opacity'
187
+ })
188
+ }
189
+
190
+ if (!corePlugins('textOpacity')) {
191
+ return {
192
+ ...colorProperties,
193
+ '--color-current': toColorValue(value),
194
+ ...color
195
+ }
196
+ }
197
+
198
+ return {
199
+ ...colorProperties,
200
+ ...withAlphaVariable({
201
+ color: value,
202
+ property: '--color-current',
203
+ variable: '--tw-text-opacity'
204
+ }),
205
+ ...color
206
+ }
207
+ }
208
+ },
209
+ { values: flattenColorPalette(theme('textColor')), type: ['color', 'any'] }
210
+ )
147
211
  addUtilities(tailwindAnimations(userConfig.animations))
148
212
  addUtilities([
149
213
  Object.entries(theme('spacing')).map(([key, value]) => {
@@ -158,11 +222,15 @@ export const createPlugin = (userConfig = {}) => {
158
222
  }, {
159
223
  corePlugins: {
160
224
  preflight: false,
161
- container: false
225
+ container: false,
226
+ textColor: false,
227
+ accentColor: false
162
228
  },
163
229
  theme: {
230
+ accentOpacity: ({ theme }) => theme('opacity'),
164
231
  extend: {
165
232
  colors: tailwindColors(userConfig.colors),
233
+ fontSize: tailwindVariablesFont('text', userConfig.fontSize),
166
234
  fontFamily: tailwindVariables('font', userConfig.fontFamily),
167
235
  fontWeight: tailwindVariables('font', userConfig.fontWeight),
168
236
  zIndex: tailwindVariables('z', userConfig.zIndex, {