color scheme: Use the users prefered mode by default (#12227)

* color scheme: Use the users prefered colour scheme if one isn't defined in the store

* fix style

---------

Co-authored-by: Matthias Mair <code@mjmair.com>
This commit is contained in:
Phil 2026-06-23 14:26:06 +08:00 committed by GitHub
parent ec845c61cd
commit 3bf410c313
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 5 deletions

View File

@ -1,5 +1,4 @@
import {
type MantineColorScheme,
type MantineColorSchemeManager,
isMantineColorScheme
} from '@mantine/core';
@ -21,10 +20,18 @@ export function localStorageColorSchemeManager({
}
try {
return (
(window.localStorage.getItem(key) as MantineColorScheme) ||
defaultValue
);
const storedValue = window.localStorage.getItem(key);
// If a value exists in storage, return it
if (storedValue && isMantineColorScheme(storedValue)) {
return storedValue;
}
// If no value, check the system preference
const systemPrefersDark = window.matchMedia(
'(prefers-color-scheme: dark)'
).matches;
return systemPrefersDark ? 'dark' : 'light';
} catch {
return defaultValue;
}