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';
|
import { ThemeColor } from '@/portainer/users/types';
|
||||||
|
|
||||||
|
let removeAutoThemeListener: (() => void) | null = null;
|
||||||
|
|
||||||
export function applyTheme(color: ThemeColor) {
|
export function applyTheme(color: ThemeColor) {
|
||||||
|
removeAutoThemeListener?.();
|
||||||
|
removeAutoThemeListener = null;
|
||||||
|
|
||||||
if (color === 'auto' || !color) {
|
if (color === 'auto' || !color) {
|
||||||
const isDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
||||||
if (isDark) {
|
|
||||||
document.documentElement.setAttribute('theme', 'dark');
|
function handleChange() {
|
||||||
} else {
|
applyAutoTheme(mediaQuery);
|
||||||
document.documentElement.removeAttribute('theme');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
applyAutoTheme(mediaQuery);
|
||||||
|
mediaQuery.addEventListener('change', handleChange);
|
||||||
|
removeAutoThemeListener = () =>
|
||||||
|
mediaQuery.removeEventListener('change', handleChange);
|
||||||
} else {
|
} else {
|
||||||
document.documentElement.setAttribute('theme', color);
|
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