26 lines
788 B
TypeScript
26 lines
788 B
TypeScript
import { darkCssIsReady, loadDarkThemeCss } from 'vite-plugin-theme-vite3/es/client';
|
|
import { addClass, hasClass, removeClass } from '@jeesite/core/utils/domUtils';
|
|
import { isProdMode } from '@jeesite/core/utils/env';
|
|
|
|
export async function updateDarkTheme(mode: string | null = 'light') {
|
|
const htmlRoot = document.getElementById('htmlRoot');
|
|
if (!htmlRoot) {
|
|
return;
|
|
}
|
|
const hasDarkClass = hasClass(htmlRoot, 'dark');
|
|
if (mode === 'dark') {
|
|
if (isProdMode() && !darkCssIsReady) {
|
|
await loadDarkThemeCss();
|
|
}
|
|
htmlRoot.setAttribute('data-theme', 'dark');
|
|
if (!hasDarkClass) {
|
|
addClass(htmlRoot, 'dark');
|
|
}
|
|
} else {
|
|
htmlRoot.setAttribute('data-theme', 'light');
|
|
if (hasDarkClass) {
|
|
removeClass(htmlRoot, 'dark');
|
|
}
|
|
}
|
|
}
|