实战引入:CSS变量 + 类名切换

通过arco-design组件库的使用来了解如何切换主题

主要思路:CSS变量 + 类名切换(组件库提供/自己定义) + 将当前主题模式设置到localStorage + vuex状态管理theme

  1. 定义CSS变量:因为CSS变量组件库已经配好一套黑白变量了,直接拿来用arco.design/vue/docs/to…

  2. 为body标签添加相关的属性,参考arcodesign官网:

设置到localStorage防止刷新之后主题恢复成默认的配置

 1const isLight = ref();
 2const theme = ref();
 3const toggleLight = () => {
 4  isLight.value = true;
 5  
 6  document.body.removeAttribute("arco-theme");
 7  localStorage.setItem("theme", "light");
 8};
 9const toggleDark = () => {
10  isLight.value = false;
11  
12  document.body.setAttribute("arco-theme", "dark");
13  localStorage.setItem("theme", "dark");
14};
15onMounted(() => {
16  theme.value = localStorage.getItem("theme");
17  if (theme.value === "light") {
18    toggleLight();
19  } else {
20    toggleDark();
21  }
22});
  1. 将一些写死的样式改为变量:

如果觉得官网设计的变量不够,想自己加,可以参考: arco.design/vue/docs/th…
利用组件库平台提供去配置主题: arco.design/docs/design…

Tailwind颜色主题切换

html 代码

 1<div class="relative flex flex-col justify-center overflow-hidden bg-gray-50 py-6 sm:py-12">
 2  <div class="relative px-6 pt-10 pb-8 shadow-xl ring-1 ring-gray-900/5 sm:mx-auto sm:max-w-lg sm:rounded-lg sm:px-10">
 3    <div class="mx-auto max-w-md">
 4      <div class="divide-y divide-gray-300/50">
 5        <div class="space-y-6 py-8 text-base leading-7 text-gray-600">
 6          <div class="font-medium text-xl"></div>
 7          <p class="text-xl font-bold text-gray-90 text-center">Thank you 🙏</p>
 8          <div class="text-sm text-gray-500">We appreciate your support.</div>
 9        </div>
10      </div>
11    </div>
12  </div>
13</div>
14</div>

更多颜色: https://tailwindcss.com/docs/customizing-colors 配置文件中进行配置:style.css,将一些基础样式添加到 Tailwind 的基础层,定义颜色变量

 1@tailwind base;
 2@tailwind components;
 3@tailwind utilities;
 4
 5
 6使用@layer 指令将这些样式添加到 Tailwind 的基础层*/
 7@layer base {
 8  .theme-light {
 9    --color-base: theme('colors.white'); 
10    --color-text-base: theme('colors.black'); 
11    --color-off-base: theme('colors.gray.50');
12    --color-text-muted: theme('colors.gray.600');
13    --color-text-muted-hover: theme('colors.gray.500'); 
14    --color-primary: theme('colors.blue.600'); 
15    --color-secondary:theme('colors.blue.300'); 
16  }
17
18  .theme-dark {
19    --color-base: theme('colors.gray.900');
20    --color-text-base: theme('colors.gray.100'); 
21    --color-off-base: theme('colors.gray.800'); 
22    --color-text-muted:theme('colors.gray.300'); 
23    --color-text-muted-hover: theme('colors.gray.200');
24    --color-primary: theme('colors.blue.500'); 
25    --color-secondary: theme('colors.blue.200'); 
26  }
27}

配置文件中进行配置:tailwind.config.js,定义了一些背景色和文本颜色的 utility classes

 1module.exports = {
 2  mode: 'jit',
 3  theme: {
 4    extend: {},
 5    backgroundColor: {
 6      
 7      base: 'var(--color-base)',
 8      'off-base': 'var(--color-off-base)',
 9      primary: 'var(--color-primary)',
10      secondary: 'var(--color-secondary)',
11      muted: 'var(--color-text-muted)',
12    },
13    textColor: {
14      
15      base: 'var(--color-text-base)',
16      muted: 'var(--color-text-muted)',
17      'muted-hover': 'var(--color-text-muted-hover)',
18      primary: 'var(--color-primary)',
19      secondary: 'var(--color-secondary)',
20    },
21  },
22  variants: {},
23  plugins: [],
24}

在html相关的地方添加上相关的class就行了:
例如,
在需要应用相应主题样式的地方的父元素或自身元素上添加主题的标签: theme-light
在需要改变背景的地方添加上:bg-base

代码:Tailwind Play

unocss颜色主题切换

html代码

 1<div class="relative flex flex-col justify-center overflow-hidden bg-gray-50 py-6 sm:py-12">
 2  <div class="relative px-6 pt-10 pb-8 shadow-xl ring-1 ring-gray-900/5 sm:mx-auto sm:max-w-lg sm:rounded-lg sm:px-10">
 3    <div class="mx-auto max-w-md">
 4      <div class="divide-y divide-gray-300/50">
 5        <div class="space-y-6 py-8 text-base leading-7 text-gray-600">
 6          <div class="font-medium text-xl"></div>
 7          <p class="text-xl font-bold text-gray-90 text-center">Thank you 🙏</p>
 8          <div class="text-sm text-base">We appreciate your support.</div>
 9        </div>
10      </div>
11    </div>
12  </div>
13</div>
14</div>

配置文件中unocss.config.ts进行配置shortcut

 1export default defineConfig({
 2    ... ...
 3    shortcuts:{
 4        'bg-base': 'bg-[#ffffff] dark:bg-[#20202a]',
 5        'card-base': 'bg-[#ffffff] dark:bg-[#10101a]',
 6        'text-base': 'text-[#20202a] dark:text-[#f0f0f0]',
 7    },
 8    ... ...
 9})

在html添加相关标签就行 代码:UnoCSS Playground

总结

上面的三个切换主题都是是基于CSS变量 + 类名切换

拓展

推荐文章:前端主题切换方案 - 掘金
6种方案:

个人笔记记录 2021 ~ 2025