文章归档:https://www.yuque.com/u27599042/coding_star/vzkgy6gvcnpl3u2y
https://www.yuque.com/u27599042/coding_star/ogu2bhefy1fvahfv
https://www.yuque.com/u27599042/coding_star/yqzi9olphko9ity1
tailwind.config.js
/** @type {import('tailwindcss').Config} */
export default {
// 配置 Tailwind CSS 使用暗黑模式的形式(类名 class="dark")
darkMode: 'class',
// ...
}
https://www.yuque.com/u27599042/coding_star/gumgmgfgi2gzkgpl
src/components/toolbar/js/ToggleTheme.js
import {computed, ref} from 'vue'
// ***************
// * 是否为暗色主题 *
// ***************
const isDark = ref(false)
// **********
// * 主题图标 *
// **********
const lightIcon = 'icon-sunbaitian-taiyang'
const darkIcon = 'icon-yueliangxingxing'
export const themeIcon = computed(() => isDark.value ? lightIcon : darkIcon)
// **********
// * 主题类名 *
// **********
const lightClass = 'light'
const darkClass = 'dark'
export const themeClass = computed(() => isDark.value ? darkClass : lightClass)
// **********
// * 切换主题 *
// **********
export const toggleTheme = () => {
const htmlClassList = document.documentElement.classList
if (isDark.value) {
isDark.value = !isDark.value
htmlClassList.remove(darkClass)
htmlClassList.add(lightClass)
return
}
isDark.value = !isDark.value
htmlClassList.remove(lightClass)
htmlClassList.add(darkClass)
}
src/components/toolbar/Toolbar.vue
<script setup>
import {ref} from 'vue'
import {RectLeft} from '@nutui/icons-vue'
import {themeIcon, toggleTheme} from "@/components/toolbar/js/ToggleTheme.js";
// ************
// * 工具栏状态 *
// ************
const toolbarActive = ref(false)
// *****************
// * 切换主题处理函数 *
// *****************
function toggleThemeHandler() {
toggleTheme()
toolbarActive.value = false
}
script>
<template>
<nut-drag direction="y" :style="{ right: '0px', bottom: '100px' }">
<nut-fixed-nav class="toolbar" v-model:visible="toolbarActive">
<template #btn>
<RectLeft color="#fff"/>
<span class="iconfont icon-gongjuxiang3 ml-2 text-xl text-gray-100">span>
template>
<template #list>
<ul class="nut-fixed-nav__list">
<li class="nut-fixed-nav__list-item">
<div class="toggle-theme w-full h-full flex-center" @click="toggleThemeHandler">
<span class="iconfont" :class="themeIcon">span>
div>
li>
ul>
template>
nut-fixed-nav>
nut-drag>
template>
<style scoped lang="scss">
.flex-center {
display: flex;
justify-content: center;
align-items: center;
}
style>
src/App.vue
<script setup>
import Toolbar from "@/components/toolbar/Toolbar.vue";
import {themeClass} from "@/components/toolbar/js/ToggleTheme.js";
script>
<template>
<div class="app w-screen h-screen bg-gray-100 dark:bg-neutral-800">
<nut-config-provider :theme="themeClass">
<div class="h-4">div>
<nut-cell title="我是标题" sub-title="副标题描述" desc="描述文字">nut-cell>
nut-config-provider>
div>
<Toolbar>Toolbar>
template>
<style scoped>
style>