Skip to content

Commit

Permalink
feat: Added support for tailwindcss night mode mechanism (#998)
Browse files Browse the repository at this point in the history
* feat: 新增支持 tailwindcss 的夜间模式支持

(cherry picked from commit 1de8704b61c38c92bc6877d0bec9e6f67766b3c8)

* docs: update changelog
  • Loading branch information
troyzhxu committed Jul 28, 2021
1 parent a544dd3 commit 189bc6f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### ✨ Features

- **Preview** 添加新的属性及事件
- **Dark Theme** 新增对 tailwindcss 夜间模式的支持

### 🐛 Bug Fixes

Expand Down
15 changes: 13 additions & 2 deletions src/logics/theme/dark.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { darkCssIsReady, loadDarkThemeCss } from 'vite-plugin-theme/es/client';
import { addClass, hasClass, removeClass } from '/@/utils/domUtils';

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 (import.meta.env.PROD && !darkCssIsReady) {
await loadDarkThemeCss();
}
htmlRoot?.setAttribute('data-theme', 'dark');
htmlRoot.setAttribute('data-theme', 'dark');
if (!hasDarkClass) {
addClass(htmlRoot, 'dark');
}
} else {
htmlRoot?.setAttribute('data-theme', 'light');
htmlRoot.setAttribute('data-theme', 'light');
if (hasDarkClass) {
removeClass(htmlRoot, 'dark');
}
}
}
2 changes: 1 addition & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = {
mode: 'jit',
// darkMode: 'class',
darkMode: 'class',
plugins: [createEnterPlugin()],
purge: {
enable: process.env.NODE_ENV === 'production',
Expand Down

0 comments on commit 189bc6f

Please sign in to comment.