TailwindCSS配合TDesign实现暗黑模式DarkMode

因为TDesign是在html上加了个theme-mode="dark"的属性,所以Tailwind需要适配一下:

tailwind.config.js

/** @type {import('tailwindcss').Config} */
export default {
  darkMode: ['selector', '[theme-mode="dark"]'],
  content: [
    "./index.html",
    "./src/**/*.{vue,js,ts,jsx,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}

 

加一个切换按钮,切换主题模式:

app.vue

<template>
    <t-radio-group
        v-model="theme"
        variant="default-filled"
        @change="changeTheme"
    >
        <t-radio-button value="light">
            <i class="ri-sun-fill"></i>
        </t-radio-button>
        <t-radio-button value="dark">
            <i class="ri-moon-fill"></i>
        </t-radio-button>
    </t-radio-group>
</template>

<script setup>
    import { ref } from "vue";

    const theme = ref("light");
    const changeTheme = (value) => {
        document.documentElement.setAttribute("theme-mode", value);
    };
</script>

这样Tailwind就可以根据TD的暗黑模式进行转换了:

<div class="text-blue-500 dark:text-white">
    text
</div>

//light下使用text-blue-500
//dark下使用text-white

 

posted @ 2025-05-10 10:54  deajax  阅读(103)  评论(0)    收藏  举报