2021-09-01 17:43:25 +08:00
|
|
|
import { watch } from 'vue';
|
2021-09-07 17:03:59 +08:00
|
|
|
import { useThemeStore } from '@/store';
|
2021-09-01 17:43:25 +08:00
|
|
|
|
|
|
|
|
export default function setupWindicssDarkMode() {
|
2021-09-07 17:03:59 +08:00
|
|
|
const theme = useThemeStore();
|
2021-09-01 17:43:25 +08:00
|
|
|
|
|
|
|
|
const DARK_CLASS = 'dark';
|
2021-09-01 18:45:28 +08:00
|
|
|
|
2021-09-01 17:43:25 +08:00
|
|
|
function getHtmlElement() {
|
|
|
|
|
return document.querySelector('html')!;
|
|
|
|
|
}
|
|
|
|
|
function addDarkClass() {
|
|
|
|
|
const html = getHtmlElement();
|
|
|
|
|
html.classList.add(DARK_CLASS);
|
|
|
|
|
}
|
|
|
|
|
function removeDarkClass() {
|
|
|
|
|
const html = getHtmlElement();
|
|
|
|
|
html.classList.remove(DARK_CLASS);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
watch(
|
2021-09-07 17:03:59 +08:00
|
|
|
() => theme.darkMode,
|
2021-09-01 17:43:25 +08:00
|
|
|
newValue => {
|
|
|
|
|
if (newValue) {
|
|
|
|
|
addDarkClass();
|
|
|
|
|
} else {
|
|
|
|
|
removeDarkClass();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|