forked from adevinta/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tailwind.config.viewer.cjs
65 lines (58 loc) · 1.62 KB
/
tailwind.config.viewer.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const sparkPlugins = require('@spark-ui/tailwind-plugins')
const themeUtils = require('@spark-ui/theme-utils')
const utils = {
isObject(value) {
return !!value && value.constructor === Object
},
isStringOrNumber(value) {
return typeof value === 'string' || typeof value === 'number'
},
toKebabCase(value) {
if (value.toUpperCase() === value) return value.toLowerCase()
return value.replace(/[A-Z0-9]/g, e => `-${e.toLocaleLowerCase()}`)
},
doubleHyphensRegex: /(?<!var\()--+/g,
}
function toTailwindConfigViewer(defaultTheme) {
const result = {}
function flatten(theme, paths = []) {
Object.entries(theme).forEach(([key, value]) => {
if (utils.isObject(value)) return flatten(value, paths.concat(key))
if (utils.isStringOrNumber(value)) {
if (paths.includes('colors')) {
result[
`rgb(var(--${[...paths, key]
.map(utils.toKebabCase)
.join('-')
.replace(utils.doubleHyphensRegex, '-')}) / <alpha-value>)`
] = value
}
result[
`var(--${[...paths, key]
.map(utils.toKebabCase)
.join('-')
.replace(utils.doubleHyphensRegex, '-')})`
] = value
}
})
}
flatten(defaultTheme)
return result
}
module.exports = {
theme: {
configViewer: {
baseFontSize: 16,
themeReplacements: toTailwindConfigViewer(themeUtils.defaultTheme),
misc: sparkPlugins.tailwindConfigViewerMisc,
},
},
plugins: [
...sparkPlugins.sparkConfig({
themes: {
default: themeUtils.defaultTheme,
},
}),
],
content: [],
}