mirror of
https://github.com/portainer/portainer.git
synced 2026-08-07 10:04:49 +00:00
feat(theme): listen for system theme changes [R8S-1190] (#3265)
This commit is contained in:
@@ -1,14 +1,31 @@
|
||||
import { ThemeColor } from '@/portainer/users/types';
|
||||
|
||||
let removeAutoThemeListener: (() => void) | null = null;
|
||||
|
||||
export function applyTheme(color: ThemeColor) {
|
||||
removeAutoThemeListener?.();
|
||||
removeAutoThemeListener = null;
|
||||
|
||||
if (color === 'auto' || !color) {
|
||||
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
||||
if (isDark) {
|
||||
document.documentElement.setAttribute('theme', 'dark');
|
||||
} else {
|
||||
document.documentElement.removeAttribute('theme');
|
||||
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
|
||||
function handleChange() {
|
||||
applyAutoTheme(mediaQuery);
|
||||
}
|
||||
|
||||
applyAutoTheme(mediaQuery);
|
||||
mediaQuery.addEventListener('change', handleChange);
|
||||
removeAutoThemeListener = () =>
|
||||
mediaQuery.removeEventListener('change', handleChange);
|
||||
} else {
|
||||
document.documentElement.setAttribute('theme', color);
|
||||
}
|
||||
}
|
||||
|
||||
function applyAutoTheme(mediaQuery: MediaQueryList) {
|
||||
if (mediaQuery.matches) {
|
||||
document.documentElement.setAttribute('theme', 'dark');
|
||||
} else {
|
||||
document.documentElement.removeAttribute('theme');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user