-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme.ts
158 lines (138 loc) · 3.96 KB
/
theme.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
import { colord, extend } from "colord";
import a11yPlugin from "colord/plugins/a11y";
import mixPlugin from "colord/plugins/mix";
import namesPlugin from "colord/plugins/names";
import {
coerce,
defaulted,
enums,
object,
optional,
string,
union,
type Infer,
} from "superstruct";
import { type BaseConfig } from "./config";
extend([mixPlugin, a11yPlugin, namesPlugin]);
export function buildPalette(base: string, count = 5) {
const color = colord(
base.match(/^([0-9A-Fa-f]{3}){1,2}$/) ? `#${base}` : base
);
const res = (
color.isDark()
? color.tints(count).reverse()
: color.shades(count).reverse()
).reduce(
(a, c, i) => ({
...a,
[(i + 1) * 100]: c.toHex(),
}),
{}
) as {
100: string;
200: string;
300: string;
400: string;
500: string;
};
return res;
}
const color = coerce(string(), string(), (value) => colord(value).toHex());
const ColorSchemeSchema = object({
100: color,
200: color,
300: color,
400: color,
500: color,
});
const ColorPaletteSchema = coerce(
ColorSchemeSchema,
union([string(), ColorSchemeSchema]),
(value) => {
if (typeof value === "string") {
return buildPalette(value);
}
return value;
}
);
const ThemeModeSchema = enums(["light", "dark", "system", "system-dark"]);
export type ThemeMode = Infer<typeof ThemeModeSchema>;
export const ThemeSchema = object({
radius: defaulted(string(), "0.5rem"),
mode: defaulted(ThemeModeSchema, "system-dark"),
light: defaulted(
object({
fg: ColorPaletteSchema,
bg: ColorPaletteSchema,
}),
{ fg: buildPalette("#000000"), bg: buildPalette("#FFFFFF") }
),
dark: defaulted(
object({
fg: ColorPaletteSchema,
bg: ColorPaletteSchema,
}),
{ fg: buildPalette("#FFFFFF"), bg: buildPalette("#000000") }
),
fg: optional(ColorPaletteSchema),
bg: optional(ColorPaletteSchema),
});
export const PartialThemeSchema = object({
radius: optional(string()),
mode: optional(ThemeModeSchema),
light: optional(
object({
fg: ColorPaletteSchema,
bg: ColorPaletteSchema,
})
),
dark: optional(
object({
fg: ColorPaletteSchema,
bg: ColorPaletteSchema,
})
),
fg: optional(ColorPaletteSchema),
bg: optional(ColorPaletteSchema),
});
const getHsl = (color: string) => {
const { h, s, l } = colord(color).toHsl();
return `${h}deg ${s}% ${l}%`;
};
export function buildTheme(config: BaseConfig) {
return {
"--theme-radius": config.theme.radius,
"--theme-fg-100": getHsl(config.theme.light.fg[100]),
"--theme-fg-200": getHsl(config.theme.light.fg[200]),
"--theme-fg-300": getHsl(config.theme.light.fg[300]),
"--theme-fg-400": getHsl(config.theme.light.fg[400]),
"--theme-fg-500": getHsl(config.theme.light.fg[500]),
"--theme-bg-100": getHsl(config.theme.light.bg[100]),
"--theme-bg-200": getHsl(config.theme.light.bg[200]),
"--theme-bg-300": getHsl(config.theme.light.bg[300]),
"--theme-bg-400": getHsl(config.theme.light.bg[400]),
"--theme-bg-500": getHsl(config.theme.light.bg[500]),
"--theme-fg-dark-100": getHsl(config.theme.dark.fg[100]),
"--theme-fg-dark-200": getHsl(config.theme.dark.fg[200]),
"--theme-fg-dark-300": getHsl(config.theme.dark.fg[300]),
"--theme-fg-dark-400": getHsl(config.theme.dark.fg[400]),
"--theme-fg-dark-500": getHsl(config.theme.dark.fg[500]),
"--theme-bg-dark-100": getHsl(config.theme.dark.bg[100]),
"--theme-bg-dark-200": getHsl(config.theme.dark.bg[200]),
"--theme-bg-dark-300": getHsl(config.theme.dark.bg[300]),
"--theme-bg-dark-400": getHsl(config.theme.dark.bg[400]),
"--theme-bg-dark-500": getHsl(config.theme.dark.bg[500]),
};
}
export const cycleTheme = (mode: ThemeMode): ThemeMode => {
switch (mode) {
case "dark":
return "light";
case "light":
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "system-dark"
: "system";
default:
return "dark";
}
};