Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/trueberryless/ai-disclosure. A practical guide for transparent AI usage disclosure across software ai-disclosure.trueberryless.org
Something went wrong. Try again.
1.4 kB · 58 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859export const THEME_STORAGE_KEY = "theme-preference";
export const THEME_PREFERENCES = ["system", "light", "dark"] as const;
export type ThemePreference = (typeof THEME_PREFERENCES)[number];
const CODE_THEMES = { dark: "catppuccin-mocha", light: "catppuccin-latte",} as const;
export function parseThemePreference(value: unknown): ThemePreference { return ( THEME_PREFERENCES.find((preference) => preference === value) ?? "system" );}
export function getThemeAttributes(preference: ThemePreference) { if (preference === "system") { return { codeTheme: undefined, colorScheme: "light dark" }; }
return { codeTheme: CODE_THEMES[preference], colorScheme: preference };}
export function applyThemePreference( root: HTMLElement, preference: ThemePreference) { const { codeTheme, colorScheme } = getThemeAttributes(preference);
if (codeTheme) { root.dataset["theme"] = codeTheme; } else { delete root.dataset["theme"]; }
root.style.colorScheme = colorScheme;}
export function readThemePreference(storage: Pick<Storage, "getItem">) { try { return parseThemePreference(storage.getItem(THEME_STORAGE_KEY)); } catch { return "system"; }}
export function storeThemePreference( storage: Pick<Storage, "setItem">, preference: ThemePreference) { try { storage.setItem(THEME_STORAGE_KEY, preference); } catch { return; }}