chore: set native color scheme for dark themes (#5840)

This commit is contained in:
boojack 2026-04-15 20:55:41 +08:00 committed by GitHub
parent a3a5b52d60
commit 411d8335e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -182,6 +182,17 @@ const updateThemeColorMeta = (theme: ResolvedTheme): void => {
} }
}; };
const isDarkTheme = (theme: ResolvedTheme): boolean => {
return theme.endsWith("-dark") || theme.endsWith(".dark");
};
/**
* Updates the browser native control color scheme to match the current theme.
*/
const updateColorScheme = (theme: ResolvedTheme): void => {
document.documentElement.style.colorScheme = isDarkTheme(theme) ? "dark" : "light";
};
// ============================================================================ // ============================================================================
// Main Theme Loading // Main Theme Loading
// ============================================================================ // ============================================================================
@ -193,7 +204,8 @@ const updateThemeColorMeta = (theme: ResolvedTheme): void => {
* 2. Resolves "system" to actual theme * 2. Resolves "system" to actual theme
* 3. Injects theme CSS * 3. Injects theme CSS
* 4. Sets data-theme attribute * 4. Sets data-theme attribute
* 5. Persists to localStorage * 5. Updates browser native UI colors
* 6. Persists to localStorage
*/ */
export const loadTheme = (themeName: string): void => { export const loadTheme = (themeName: string): void => {
const validTheme = validateTheme(themeName); const validTheme = validateTheme(themeName);
@ -202,6 +214,7 @@ export const loadTheme = (themeName: string): void => {
injectThemeStyle(resolvedTheme); injectThemeStyle(resolvedTheme);
setThemeAttribute(resolvedTheme); setThemeAttribute(resolvedTheme);
updateThemeColorMeta(resolvedTheme); updateThemeColorMeta(resolvedTheme);
updateColorScheme(resolvedTheme);
setStoredTheme(validTheme); // Store original theme preference (not resolved) setStoredTheme(validTheme); // Store original theme preference (not resolved)
}; };