From 0fd2107473c35334cc8ff2a548021469dadbbeb4 Mon Sep 17 00:00:00 2001 From: pylixonly <82711525+pylixonly@users.noreply.github.com> Date: Mon, 21 Oct 2024 22:17:23 +0800 Subject: [PATCH] fix: readd semantic fallback for spec 2 themes --- src/lib/addons/themes/colors/parser.ts | 2 ++ .../addons/themes/colors/patches/resolver.ts | 19 ++++++++++++++++++- src/lib/addons/themes/colors/types.ts | 3 ++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/lib/addons/themes/colors/parser.ts b/src/lib/addons/themes/colors/parser.ts index 4e25978..c8c1bb4 100644 --- a/src/lib/addons/themes/colors/parser.ts +++ b/src/lib/addons/themes/colors/parser.ts @@ -47,6 +47,7 @@ export function parseColorManifest(manifest: ColorManifest): InternalColorDefini } return { + spec: 3, reference: resolveType(manifest.type), semantic: semanticColorDefinitions, raw: manifest.raw ?? {}, @@ -83,6 +84,7 @@ export function parseColorManifest(manifest: ColorManifest): InternalColorDefini return { + spec: 2, reference: resolveType(), semantic: semanticDefinitions, raw: manifest.rawColors ?? {}, diff --git a/src/lib/addons/themes/colors/patches/resolver.ts b/src/lib/addons/themes/colors/patches/resolver.ts index e5ef8b1..7af7940 100644 --- a/src/lib/addons/themes/colors/patches/resolver.ts +++ b/src/lib/addons/themes/colors/patches/resolver.ts @@ -9,6 +9,19 @@ import chroma from "chroma-js"; const tokenReference = findByProps("SemanticColor"); const isThemeModule = createLazyModule(byMutableProp("isThemeDark")); +const SEMANTIC_FALLBACK_MAP: Record = { + "BG_BACKDROP": "BACKGROUND_FLOATING", + "BG_BASE_PRIMARY": "BACKGROUND_PRIMARY", + "BG_BASE_SECONDARY": "BACKGROUND_SECONDARY", + "BG_BASE_TERTIARY": "BACKGROUND_SECONDARY_ALT", + "BG_MOD_FAINT": "BACKGROUND_MODIFIER_ACCENT", + "BG_MOD_STRONG": "BACKGROUND_MODIFIER_ACCENT", + "BG_MOD_SUBTLE": "BACKGROUND_MODIFIER_ACCENT", + "BG_SURFACE_OVERLAY": "BACKGROUND_FLOATING", + "BG_SURFACE_OVERLAY_TMP": "BACKGROUND_FLOATING", + "BG_SURFACE_RAISED": "BACKGROUND_MOBILE_PRIMARY" +}; + export default function patchDefinitionAndResolver() { const callback = ([theme]: any[]) => theme === _colorRef.key ? [_colorRef.current!.reference] : void 0; @@ -35,7 +48,11 @@ export default function patchDefinitionAndResolver() { const [name, colorDef] = extractInfo(_colorRef.current!.reference, args[1]); - const semanticDef = _colorRef.current.semantic[name]; + let semanticDef = _colorRef.current.semantic[name]; + if (!semanticDef && _colorRef.current?.spec === 2 && name in SEMANTIC_FALLBACK_MAP) { + semanticDef = _colorRef.current.semantic[SEMANTIC_FALLBACK_MAP[name]]; + } + if (semanticDef?.value) { return chroma(semanticDef.value).alpha(semanticDef.opacity).hex(); } diff --git a/src/lib/addons/themes/colors/types.ts b/src/lib/addons/themes/colors/types.ts index 03a3801..be87a53 100644 --- a/src/lib/addons/themes/colors/types.ts +++ b/src/lib/addons/themes/colors/types.ts @@ -3,7 +3,7 @@ import { Author, BunnyManifest } from "@lib/addons/types"; interface SemanticReference { type: "color" | "raw"; value: string; - opacity?: number + opacity?: number; } interface BackgroundDefinition { @@ -36,6 +36,7 @@ export interface VendettaThemeManifest { /** @internal */ export interface InternalColorDefinition { + spec: 2 | 3; reference: "darker" | "light"; semantic: Record