-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (100 loc) · 2.84 KB
/
index.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const plugin = require("tailwindcss/plugin");
const pSBC = require("./pSBC");
module.exports = plugin.withOptions(function (options) {
const intensityMap = new Map([
[50, 0.95],
[100, 0.9],
[200, 0.75],
[300, 0.6],
[400, 0.3],
[600, -0.3],
[700, -0.6],
[800, -0.75],
[900, -0.9],
]);
const ALL_LEVELS = [50, 100, 200, 300, 400, 600, 700, 800, 900];
const prefix = options.prefix == null ? "" : `${options.prefix}-`;
const levels =
options.levels == null
? ALL_LEVELS
: options.levels.filter((level) => ALL_LEVELS.includes(level));
return function ({ theme, e, addUtilities }) {
const colors = theme("shades", {});
function forEachShade(cb) {
const exclude = ["inherit", "current", "transparent", "black", "white"];
return Object.entries(colors).reduce((acc, [name, value]) => {
if (!exclude.includes(name)) {
if (typeof value === "string") {
acc[name] = cb(value);
} else if (Array.isArray(value)) {
acc[name] = cb(`rgb(${value.join(",")})`);
}
}
return acc;
}, {});
}
function forEachLevel(shade, cb) {
return levels.reduce(
(acc, level) => {
acc[level] = cb(shade, intensityMap.get(level));
return acc;
},
{ 500: shade }
);
}
const utilities = {
bg: (value) => ({
"background-color": value,
}),
text: (value) => ({
color: value,
}),
border: (value) => ({
"border-color": value,
}),
outline: (value) => ({
"outline-color": value,
}),
ring: (value) => ({
"--tw-ring-opacity": 1,
"--tw-ring-color": value,
}),
"ring-offset": (value) => ({
"--tw-ring-offset-color": value,
}),
shadow: (value) => ({
"--tw-shadow-color": value,
"--tw-shadow": "var(--tw-shadow-colored)",
}),
accent: (value) => ({
"accent-color": value,
}),
caret: (value) => ({
"caret-color": value,
}),
fill: (value) => ({
fill: value,
}),
stroke: (value) => ({
stroke: value,
}),
};
const shades = forEachShade((shade) => {
return forEachLevel(shade, (val, intensity) => {
return pSBC(intensity, val);
});
});
const acc = [];
Object.entries(shades).forEach(([name, shades]) => {
Object.entries(utilities).forEach(([utility, fn]) => {
acc.push([`.${e(`${prefix}${utility}-${name}`)}`, fn(shades[500])]);
});
return Object.entries(shades).forEach(([level, val]) => {
Object.entries(utilities).forEach(([utility, fn]) => {
acc.push([`.${e(`${prefix}${utility}-${name}-${level}`)}`, fn(val)]);
});
});
});
addUtilities(Object.fromEntries(acc));
};
});