Spaces:
Running
Running
Download src/utils/theme.ts from zelk12/ShareGPT_Dialogue_Studio: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/zelk12/ShareGPT_Dialogue_Studio/resolve/main/src/utils/theme.ts
- Command line
-
hf download hf://spaces/zelk12/ShareGPT_Dialogue_Studio/src/utils/theme.ts
-
curl -L -o theme.ts https://huggingface.co/spaces/zelk12/ShareGPT_Dialogue_Studio/resolve/main/src/utils/theme.ts
1.45 kB
| import { ResolvedTheme, ThemePreference } from '../types'; | |
| export const THEME_STORAGE_KEY = 'sharegpt_dialogue_studio_theme_v1'; | |
| export function getStoredThemePreference(): ThemePreference { | |
| try { | |
| const saved = localStorage.getItem(THEME_STORAGE_KEY); | |
| if (saved === 'system' || saved === 'light' || saved === 'dark') { | |
| return saved; | |
| } | |
| } catch (e) { | |
| console.error('Failed to load theme preference:', e); | |
| } | |
| return 'system'; | |
| } | |
| export function saveThemePreference(preference: ThemePreference): void { | |
| try { | |
| localStorage.setItem(THEME_STORAGE_KEY, preference); | |
| } catch (e) { | |
| console.error('Failed to save theme preference:', e); | |
| } | |
| } | |
| export function getSystemTheme(): ResolvedTheme { | |
| if (typeof window === 'undefined' || !window.matchMedia) { | |
| return 'light'; | |
| } | |
| return window.matchMedia('(prefers-color-scheme: dark)').matches | |
| ? 'dark' | |
| : 'light'; | |
| } | |
| export function resolveTheme(preference: ThemePreference): ResolvedTheme { | |
| return preference === 'system' ? getSystemTheme() : preference; | |
| } | |
| export function applyThemeToDocument(preference: ThemePreference): ResolvedTheme { | |
| const resolved = resolveTheme(preference); | |
| if (typeof document !== 'undefined') { | |
| const root = document.documentElement; | |
| if (resolved === 'dark') { | |
| root.classList.add('dark'); | |
| } else { | |
| root.classList.remove('dark'); | |
| } | |
| root.style.colorScheme = resolved; | |
| } | |
| return resolved; | |
| } | |