-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtailwind.plugin.js
78 lines (66 loc) · 1.76 KB
/
tailwind.plugin.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
const pick = require('lodash/pick')
const twColors = require('tailwindcss/colors')
const colors = {
primary: {
50: "#f5f3ff",
100: "#ede9fe",
200: "#ddd6fe",
300: "#c4b5fd",
400: "#a78bfa",
500: "#8b5cf6",
600: "#7c3aed",
700: "#6d28d9",
800: "#5b21b6",
900: "#4c1d95"
},
...twColors
}
const colorToConvert = pick(colors, ['primary'])
function generateRootCSSVariables() {
return Object.fromEntries(
Object.entries(colorToConvert)
.map(([key, value]) => {
if (typeof value === 'string') {
return [[`--colors-${key}`, toRGBString(value)]]
}
return Object.entries(value).map(([shade, color]) => {
return [`--colors-${key}-${shade}`, toRGBString(color)]
})
})
.flat(1)
)
}
function generateTailwindColors() {
return Object.fromEntries(
Object.entries(colorToConvert).map(([key, value]) => {
if (typeof value === 'string') {
return [`${key}`, value]
}
return [
key,
Object.fromEntries(
Object.entries(value).map(([shade]) => {
return [`${shade}`, `rgba(var(--colors-${key}-${shade}))`]
})
),
]
})
)
}
const toRGBString = hexCode => {
if (hexCode.startsWith('#')) {
let hex = hexCode.replace('#', '')
if (hex.length === 3) {
hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`
}
const r = parseInt(hex.substring(0, 2), 16)
const g = parseInt(hex.substring(2, 4), 16)
const b = parseInt(hex.substring(4, 6), 16)
return `${r}, ${g}, ${b}`
}
return hexCode
}
module.exports = {
generateRootCSSVariables,
generateTailwindColors,
}