Commit 189bc6feb3f2860be8c531dd1ca996f3a2cff018
Committed by
GitHub
1 parent
a544dd3e
feat: Added support for tailwindcss night mode mechanism (#998)
* feat: 新增支持 tailwindcss 的夜间模式支持 (cherry picked from commit 1de8704b61c38c92bc6877d0bec9e6f67766b3c8) * docs: update changelog
Showing
3 changed files
with
15 additions
and
3 deletions
CHANGELOG.zh_CN.md
src/logics/theme/dark.ts
1 | 1 | import { darkCssIsReady, loadDarkThemeCss } from 'vite-plugin-theme/es/client'; |
2 | +import { addClass, hasClass, removeClass } from '/@/utils/domUtils'; | |
2 | 3 | |
3 | 4 | export async function updateDarkTheme(mode: string | null = 'light') { |
4 | 5 | const htmlRoot = document.getElementById('htmlRoot'); |
6 | + if (!htmlRoot) { | |
7 | + return; | |
8 | + } | |
9 | + const hasDarkClass = hasClass(htmlRoot, 'dark'); | |
5 | 10 | if (mode === 'dark') { |
6 | 11 | if (import.meta.env.PROD && !darkCssIsReady) { |
7 | 12 | await loadDarkThemeCss(); |
8 | 13 | } |
9 | - htmlRoot?.setAttribute('data-theme', 'dark'); | |
14 | + htmlRoot.setAttribute('data-theme', 'dark'); | |
15 | + if (!hasDarkClass) { | |
16 | + addClass(htmlRoot, 'dark'); | |
17 | + } | |
10 | 18 | } else { |
11 | - htmlRoot?.setAttribute('data-theme', 'light'); | |
19 | + htmlRoot.setAttribute('data-theme', 'light'); | |
20 | + if (hasDarkClass) { | |
21 | + removeClass(htmlRoot, 'dark'); | |
22 | + } | |
12 | 23 | } |
13 | 24 | } | ... | ... |