颜色变量计算&颜色主题迁移

This commit is contained in:
lutinglt
2025-06-27 22:55:59 +08:00
parent 16298f2b11
commit 5ae1f2f634
14 changed files with 514 additions and 412 deletions

View File

@@ -2,7 +2,7 @@ import { createGlobalTheme, globalStyle } from "@vanilla-extract/css";
import { otherThemeVars, themeVars } from "src/types/vars";
import type { MapLeafNodes, WithOptionalLayer } from "./types";
type Theme = WithOptionalLayer<MapLeafNodes<typeof themeVars, string>>;
export type Theme = WithOptionalLayer<MapLeafNodes<typeof themeVars, string>>;
function stringToBoolean(str: string, name: string): boolean {
try {
@@ -13,27 +13,6 @@ function stringToBoolean(str: string, name: string): boolean {
}
}
/** 定义主题, 用于生成颜色主题
* @example
* 文件名: `color-dark.css.ts`
* import type { Primary } from "src/types";
* import { defineTheme, themeVars } from "src";
*
* const primary: Primary = {
* self: "#000000",
* contrast: themeVars.color.white,
* ...
* }
*
* export default defineTheme({
* isDarkTheme: "true",
* color: {
* primary,
* ...
* }
* })
*/
export const defineTheme = (theme: Theme) => theme;
export function createTheme(theme: Theme): void {
createGlobalTheme(":root", themeVars, theme);
createGlobalTheme(":root", otherThemeVars, {
@@ -42,7 +21,7 @@ export function createTheme(theme: Theme): void {
},
});
globalStyle(":root", {
accentColor: themeVars.color.blue,
accentColor: themeVars.color.accent,
colorScheme: stringToBoolean(theme.isDarkTheme, "isDarkTheme") ? "dark" : "light",
});
}