-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreset.shadcn.ts
111 lines (102 loc) · 4.76 KB
/
preset.shadcn.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
import type { Preset } from "unocss";
import type { PresetMiniOptions, Theme } from "unocss/preset-mini";
import { presetShadcn as themeShadcn } from "@bernankez/theme";
import { h } from "@unocss/preset-mini/utils";
export interface PresetShadcnOptions extends PresetMiniOptions {}
function handleMatchNumber(v: string, defaultVal = "0") {
return h.bracket.cssvar.global.auto.fraction.number(v || defaultVal)?.toString().replace("%", "");
}
const handleMatchRem = (v: string, defaultVal = "full") => h.bracket.cssvar.global.auto.fraction.rem(v || defaultVal);
function getCSS(style: { light: Record<string, string>; dark: Record<string, string> }) {
return `:root {
${Object.entries((style?.light || {})).map(([key, value]) => {
return ` ${key}: ${value};`;
}).join("\n")}
}
.dark {
${Object.entries((style?.dark || {})).map(([key, value]) => {
return ` ${key}: ${value};`;
}).join("\n")}
}`;
}
export function presetShadcn(_options: PresetShadcnOptions = {}): Preset<Theme> {
return {
name: "unocss-preset-shadcn",
preflights: [
{
getCSS: () => `
@keyframes shadcn-down { from{ height: 0 } to { height: var(--radix-accordion-content-height)} }
@keyframes shadcn-up { from{ height: var(--radix-accordion-content-height)} to { height: 0 } }
@keyframes shadcn-enter { from{ opacity: var(--un-enter-opacity, 1); transform: translate3d(var(--un-enter-translate-x, 0), var(--un-enter-translate-y, 0), 0) scale3d(var(--un-enter-scale, 1), var(--un-enter-scale, 1), var(--un-enter-scale, 1)) rotate(var(--un-enter-rotate, 0)) } }
@keyframes shadcn-exit { to{ opacity: var(--un-exit-opacity, 1); transform: translate3d(var(--un-exit-translate-x, 0), var(--un-exit-translate-y, 0), 0) scale3d(var(--un-exit-scale, 1), var(--un-exit-scale, 1), var(--un-exit-scale, 1)) rotate(var(--un-exit-rotate, 0)) } }
${getCSS(themeShadcn.style)}
* {
border-color: hsl(var(--border));
}
body {
color: hsl(var(--foreground));
background: hsl(var(--background));
}
`,
},
],
rules: [
[
"accordion-down",
{
animation: "shadcn-down 0.2s ease-out",
},
],
[
"accordion-up",
{
animation: "shadcn-up 0.2s ease-out",
},
],
[
"animate-in",
{
"animation-name": "shadcn-enter",
"animation-duration": "var(--un-animate-duration)",
"--un-animate-duration": "150ms",
"--un-enter-opacity": "initial",
"--un-enter-scale": "initial",
"--un-enter-rotate": "initial",
"--un-enter-translate-x": "initial",
"--un-enter-translate-y": "initial",
},
],
[
"animate-out",
{
"animation-name": "shadcn-exit",
"animation-duration": "var(--un-animate-duration)",
"--un-animate-duration": "150ms",
"--un-exit-opacity": "initial",
"--un-exit-scale": "initial",
"--un-exit-rotate": "initial",
"--un-exit-translate-x": "initial",
"--un-exit-translate-y": "initial",
},
],
[/^fade-in-?(.+)?$/, ([, d]) => ({ "--un-enter-opacity": `${Number(handleMatchNumber(d) || 0) / 100}` })],
[/^fade-out-?(.+)?$/, ([, d]) => ({ "--un-exit-opacity": `${Number(handleMatchNumber(d) || 0) / 100}` })],
[/^zoom-in-?(.+)?$/, ([, d]) => ({ "--un-enter-scale": `${Number(handleMatchNumber(d) || 0) / 100}` })],
[/^zoom-out-?(.+)?$/, ([, d]) => ({ "--un-exit-scale": `${Number(handleMatchNumber(d) || 0) / 100}` })],
[/^spin-in-?(.+)?$/, ([, d]) => ({ "--un-enter-rotate": `${Number(handleMatchNumber(d) || 0)}deg` })],
[/^spin-out-?(.+)?$/, ([, d]) => ({ "--un-exit-rotate": `${Number(handleMatchNumber(d) || 0)}deg` })],
[/^slide-in-from-top-?(.+)?$/, ([, d]) => ({ "--un-enter-translate-y": `-${handleMatchRem(d)}` })],
[/^slide-in-from-bottom-?(.+)?$/, ([, d]) => ({ "--un-enter-translate-y": handleMatchRem(d) })],
[/^slide-in-from-left-?(.+)?$/, ([, d]) => ({ "--un-enter-translate-x": `-${handleMatchRem(d)}` })],
[/^slide-in-from-right-?(.+)?$/, ([, d]) => ({ "--un-enter-translate-x": handleMatchRem(d) })],
[/^slide-out-to-top-?(.+)?$/, ([, d]) => ({ "--un-exit-translate-y": `-${handleMatchRem(d)}` })],
[/^slide-out-to-bottom-?(.+)?$/, ([, d]) => ({ "--un-exit-translate-y": handleMatchRem(d) })],
[/^slide-out-to-left-?(.+)?$/, ([, d]) => ({ "--un-exit-translate-x": `-${handleMatchRem(d)}` })],
[/^slide-out-to-right-?(.+)?$/, ([, d]) => ({ "--un-exit-translate-x": handleMatchRem(d) })],
],
theme: {
...themeShadcn.unocss,
},
};
}
export default presetShadcn;