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 | import { darkCssIsReady, loadDarkThemeCss } from 'vite-plugin-theme/es/client'; | 1 | import { darkCssIsReady, loadDarkThemeCss } from 'vite-plugin-theme/es/client'; |
2 | +import { addClass, hasClass, removeClass } from '/@/utils/domUtils'; | ||
2 | 3 | ||
3 | export async function updateDarkTheme(mode: string | null = 'light') { | 4 | export async function updateDarkTheme(mode: string | null = 'light') { |
4 | const htmlRoot = document.getElementById('htmlRoot'); | 5 | const htmlRoot = document.getElementById('htmlRoot'); |
6 | + if (!htmlRoot) { | ||
7 | + return; | ||
8 | + } | ||
9 | + const hasDarkClass = hasClass(htmlRoot, 'dark'); | ||
5 | if (mode === 'dark') { | 10 | if (mode === 'dark') { |
6 | if (import.meta.env.PROD && !darkCssIsReady) { | 11 | if (import.meta.env.PROD && !darkCssIsReady) { |
7 | await loadDarkThemeCss(); | 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 | } else { | 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 | } |
tailwind.config.js
1 | module.exports = { | 1 | module.exports = { |
2 | mode: 'jit', | 2 | mode: 'jit', |
3 | - // darkMode: 'class', | 3 | + darkMode: 'class', |
4 | plugins: [createEnterPlugin()], | 4 | plugins: [createEnterPlugin()], |
5 | purge: { | 5 | purge: { |
6 | enable: process.env.NODE_ENV === 'production', | 6 | enable: process.env.NODE_ENV === 'production', |