-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy-preset.ts
241 lines (225 loc) · 5.97 KB
/
my-preset.ts
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// my-preset.ts
import type { Preset } from "unocss";
import { handler as h, variantGetParameter } from "@unocss/preset-mini/utils";
export const myPreset: Preset = {
name: "my-preset",
rules: [["abs", { position: "absolute" }]],
shortcuts: [
[
// flex-s stands for flex-shortcut
// to avoid mixups with default flex utilities like flex-wrap
/^flex-s-(start|center|between|evenly|around|end)(-(start|center|baseline|end))?$/,
([, justify, align]) =>
`flex justify-${justify} items${align || "-center"}`,
{ layer: "default" },
],
// use when width and height values are the same
[/^square-(.*)$/, ([, v]) => `h-${v} w-${v}`, { layer: "utilities" }],
[
/^br(-\w+(-\w+)*)?$/, // h - hyphen | v - value
([, hAndV]) => {
const [, v1, v2] = hAndV?.split("-") || [];
// const vJoined = v.join("-");
return v2
? `rounded-${v1 + "-" + v2 || v2}`
: v1
? `rounded-${v1}`
: "rounded";
},
{ layer: "default" },
],
[
/^scrollbar-f-(thin)-(.*)$/,
([, size, colors]) =>
`[scrollbar-width:${size}] [scrollbar-color:${colors}]`,
{ layer: "utilities" },
],
],
variants: [
{
// adds support for "@min-[width]:class" and "@min-h-[width]:class"
// or
// "@min-width:class" and "@min-h-width:class"
name: "arbitrary-media-query",
match(matcher, { theme }) {
// prefix with @ to specify that it's a media query
const minVariant = variantGetParameter("@min-", matcher, [":", "-"]);
const maxVariant = variantGetParameter("@max-", matcher, [":", "-"]);
const minHeightVariant = variantGetParameter("@min-h-", matcher, [
":",
"-",
]);
const maxHeightVariant = variantGetParameter("@max-h-", matcher, [
":",
"-",
]);
// the order that we check the variants is important
// because we want to match the most specific one
const matched =
(minHeightVariant && {
type: "min-h",
variant: minHeightVariant,
}) ||
(maxHeightVariant && {
type: "max-h",
variant: maxHeightVariant,
}) ||
(minVariant && {
type: "min",
variant: minVariant,
}) ||
(maxVariant && {
type: "max",
variant: maxVariant,
});
if (matched?.variant) {
const [match, rest] = matched.variant;
// this is for extracting the value from the match and
// makes sure it either has no brackets or has brackets
const extractedValue =
h.bracket(match) ||
(!match.startsWith("[") && !match.endsWith("]") && match) ||
"";
const endsWithUnit = /^\d+(em|px|rem)$/.test(extractedValue);
const isOnlyNum = /^\d+$/.test(extractedValue);
if (
endsWithUnit ||
isOnlyNum ||
theme["breakpoints"][extractedValue]
) {
return {
matcher: rest,
layer: "utilities",
handle: (input, next) =>
next({
...input,
parent: `${
input.parent ? `${input.parent} $$ ` : ""
}@media (${
matched.type == "min"
? "min-width"
: matched.type == "max"
? "max-width"
: matched.type == "min-h"
? "min-height"
: "max-height"
}:${
endsWithUnit
? extractedValue
: isOnlyNum
? extractedValue + "px"
: theme["breakpoints"][extractedValue]
})`,
}),
};
}
}
},
},
{
name: "firefox-only",
match(matcher) {
const ffVariant = variantGetParameter("@ff", matcher, [":"]);
if (ffVariant) {
const [, rest] = ffVariant;
return {
matcher: rest,
handle: (input, next) =>
next({
...input,
parent: `${
input.parent ? `${input.parent} $$ ` : ""
}@-moz-document url-prefix()`,
}),
};
}
},
},
matcher => {
const [m1, m2, m3] = [
"scrollbar:",
"scrollbar-track:",
"scrollbar-thumb:",
];
let matchedStr = "";
if (matcher.startsWith(m1)) {
matchedStr = m1;
} else if (matcher.startsWith(m2)) {
matchedStr = m2;
} else if (matcher.startsWith(m3)) {
matchedStr = m3;
} else {
return matcher;
}
return {
matcher: matcher.slice(matchedStr.length),
selector: s =>
`${s}::-webkit-scrollbar${
matchedStr == m2 ? "-track" : matchedStr == m3 ? "-thumb" : ""
}`,
layer: "default",
};
},
],
};
export function convertPalleteToHSL<
T extends Record<string, Record<string, string>>
>(obj: T) {
const temp: Record<string, Record<string, string>> = {};
for (const colorKey in obj) {
for (const colorShadeKey in obj[colorKey]) {
if (!temp[colorKey]) {
temp[colorKey] = {
[colorShadeKey]: hexToHSL(obj[colorKey][colorShadeKey]),
};
} else {
temp[colorKey][colorShadeKey] = hexToHSL(obj[colorKey][colorShadeKey]);
}
}
}
return temp as T;
}
export function hexToHSL(
hex: string,
options?: { justNums: boolean; satAndLight?: { s?: number; l?: number } }
) {
const { satAndLight, justNums } = options || {
satAndLight: undefined,
justNums: false,
};
// convert hex to rgb
let r = 0,
g = 0,
b = 0;
if (hex.length === 4) {
r = +("0x" + hex[1] + hex[1]);
g = +("0x" + hex[2] + hex[2]);
b = +("0x" + hex[3] + hex[3]);
} else if (hex.length === 7) {
r = +("0x" + hex[1] + hex[2]);
g = +("0x" + hex[3] + hex[4]);
b = +("0x" + hex[5] + hex[6]);
}
// then to HSL
r /= 255;
g /= 255;
b /= 255;
const cmin = Math.min(r, g, b);
const cmax = Math.max(r, g, b);
const delta = cmax - cmin;
let h = 0;
let s = 0;
let l = 0;
if (delta === 0) h = 0;
else if (cmax === r) h = ((g - b) / delta) % 6;
else if (cmax === g) h = (b - r) / delta + 2;
else h = (r - g) / delta + 4;
h = Math.round(h * 60);
if (h < 0) h += 360;
l = (cmax + cmin) / 2;
s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
s = +(s * 100).toFixed(1);
l = +(l * 100).toFixed(1);
if (justNums) return `${h}, ${satAndLight?.s || s}%, ${satAndLight?.l || l}%`;
return `hsl(${h}, ${satAndLight?.s || s}%, ${satAndLight?.l || l}%)`;
}