winduum 0.2.1 → 0.2.2-next.1

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