2024-02-17 13:52:03 +08:00

26 lines
592 B
TypeScript

import { addClass, hasClass, removeClass } from '@/utils/domUtils'
/**
* html 根标签上挂载 暗/亮 属性标识
*/
export function updateDarkSign(mode: 'light' | 'dark') {
const htmlRoot = document.getElementById('htmlRoot')
if (!htmlRoot) {
return
}
const hasDarkClass = hasClass(htmlRoot, 'dark')
if (mode === 'dark') {
htmlRoot.setAttribute('data-theme', 'dark')
if (!hasDarkClass) {
addClass(htmlRoot, 'dark')
}
}
else {
htmlRoot.setAttribute('data-theme', 'light')
if (hasDarkClass) {
removeClass(htmlRoot, 'dark')
}
}
}