Tailwind CSS v4 移除 tailwindcss.config.js 后如何配置并进行主题切换

在 Tailwind CSS v4 中,tailwindcss.config.js 配置文件已被移除,取而代之的是 postcss.config.js。由于此次更新涉及较大变动,本文将记录如何在 Next.js 项目中结合 Tailwind CSS v4 实现可切换的自定义主题。

配置文件变更

Content 配置

变更前 (Tailwind CSS v3 及之前): 需要在 tailwindcss.config.js 中手动指定扫描的文件路径,例如:

ts 复制代码
module.exports = {
  content: ['./src/**/*.{html,js}'],
}

变更后 (Tailwind CSS v4): Tailwind CSS 现在会自动扫描相关文件,因此无需再手动配置 content 选项。


Plugin 配置

变更前 : 插件需要在 tailwindcss.config.js 中手动引入,例如:

ts 复制代码
module.exports = {
  plugins: [
    require('daisyui'),
  ],
}

变更后 : 可以直接在 CSS 文件中使用 @plugin 语法引入插件:

css 复制代码
@plugin "daisyui";

Prefix 配置

变更前 : 如果需要为 Tailwind CSS 的所有实用类添加前缀(如 tw-),需要在配置文件中定义:

ts 复制代码
module.exports = {
  prefix: 'tw-'
}

变更后: 可以在 CSS 文件中引入 Tailwind CSS 时直接指定前缀,例如:

css 复制代码
@import "tailwindcss" prefix(tw);

e.g. 在项目中使用 Tailwind CSS 类名时,需按如下方式调用:

tsx 复制代码
const App = () => {
  return (
    <div className="tw:text-gray-500">app</div>
  )
}

Theme 主题配置

变更前 : 自定义主题颜色需要在 tailwindcss.config.js 中定义:

ts 复制代码
module.exports = {
  theme: {
    screens: {
      sm: '480px'
    },
    extend: {
      colors: {
        "twitter-blue": "#1DA1F2"
      }
    },
  },
}

变更后 : 可直接在 CSS 文件中使用 @theme 语法定义主题变量:

css 复制代码
@theme {
  --breakpoint-*: initial;
  --breakpoint-sm: 480px;

  --color-twitter-blue: #1DA1F2;
  --color-orange-500: #ffa500;
}

更多关于 Tailwind CSS 主题变量命名的信息,请参考官方文档:Theme Variable Namespaces

主题切换

根据官方文档的 referencing-other-variables,Tailwind CSS 4 引入了 @theme inline 关键字,可以对 CSS 变量进行引用。

以下是一个结合 zustand 对 theme 主题色进行状态管理并持久化的例子:

配置全局主题变量

global.css 中,我们使用 @theme inline 定义主题变量,并通过 data-theme 属性指定主题

css 复制代码
@import "tailwindcss";

@theme inline {
  --color-primary: var(--color-brand);
  --color-background: var(--color-bg);
  --color-text: var(--color-tx);
}

/* 亮色模式 */
[data-theme="light"] {
  --color-brand: #1DA1F2;
  --color-bg: #ffffff;
  --color-tx: #333333;
}

/* 深色模式 */
[data-theme="dark"] {
  --color-brand: #ff9800;
  --color-bg: #1a1a1a;
  --color-tx: #f5f5f5;
}

使用 zustand 进行状态管理

store/themeStore.ts 中创建 Zustand 存储,确保主题状态能在页面刷新后保持。

tsx 复制代码
import { create } from "zustand";
import { persist } from "zustand/middleware";

export const useThemeStore = create(
  persist(
    (set) => ({
      theme: "light",
      setTheme: (newTheme: "light" | "dark") => set({ theme: newTheme }),
    }),
    {
      name: "theme-storage",
    }
  )
);

创建 ThemeProvider 组件

ThemeProvider.tsx 中初始化当前 theme,确保主题状态在应用启动时正确应用。

tsx 复制代码
"use client";

import { useEffect } from "react";
import { useThemeStore } from "@/store/themeStore";

export default function ThemeProvider({ children }: { children: React.ReactNode }) {
  const { theme } = useThemeStore();

  useEffect(() => {
    document.documentElement.setAttribute("data-theme", theme);
  }, [theme]);

  return <>{children}</>;
}

主题切换按钮

ThemeToggle.tsx 组件用于测试是否可以在亮色模式和深色模式之间切换

tsx 复制代码
"use client";

import { useThemeStore } from "@/store/themeStore";
import { useEffect } from "react";

export default function ThemeToggle() {
  const { theme, setTheme } = useThemeStore();

  useEffect(() => {
    document.documentElement.setAttribute("data-theme", theme);
  }, [theme]);

  return (
    <button
      className="px-4 py-2 rounded-lg transition-all"
      onClick={() => setTheme(theme === "light" ? "dark" : "light")}
    >
      {theme === "light" ? "🌙 深色模式" : "☀️ 亮色模式"}
    </button>
  );
}

layout.tsx 中引入主题

app/layout.tsx 文件中引入 ThemeProviderThemeToggle,确保主题切换在整个应用中生效

tsx 复制代码
import ThemeProvider from "@/components/ThemeProvider";
import ThemeToggle from "@/components/ThemeToggle";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="zh-CN">
      <body>
        <ThemeProvider>
          {children}
          <ThemeToggle />
          <div className="text-primary">test</div>
        </ThemeProvider>
      </body>
    </html>
  );
}

现在点击切换按钮,可以发现文字 test 已经可以随着按钮点击而变化颜色了。

v4 配置介绍相关内容来自:The NEW CSS-first configuration with Tailwind CSS v4 (No more tailwind.config.js)

相关推荐
HjhIron13 小时前
手把手教你用 Next.js 14 + Redis 从零搭建一个全栈 Markdown 笔记系统
前端·全栈·next.js
To_OC1 天前
写了三天 Next.js,我的 React 世界观被拆了又装回去
前端·全栈·next.js
名字还没想好☜1 天前
Next.js 用 Server Components 直连数据库:去掉 API 层的边界,和三条别踩的安全红线
前端·javascript·数据库·安全·react·next.js
不知疲倦的老鸟1 天前
Next.js 15 多语言站点的 6 个坑:从 query string 路由到 URL 路径
typescript·next.js
JieE2121 天前
Next.js App Router 全栈实战:从零构建一个 Markdown 笔记系统
全栈·next.js
DsirNg2 天前
React Server Components 在真实项目中的边界:哪些组件该放在服务端
性能优化·react·next.js·app router·前端架构·rsc·react server components
Asize3 天前
为什么写代码前要先规划组件树?Next.js + Redis 笔记系统实战
前端·javascript·next.js
小林ixn3 天前
用 Next.js 和 Redis 撸一个 Markdown 笔记系统:RSC 实战与组件化拆解
前端·redis·next.js
嘟嘟07175 天前
Next.js 里 SSR、CSR 和水合到底差在哪?从一段待办代码说起
前端·后端·next.js
触底反弹5 天前
🔥 从 SPA 到 SSR:5 个核心差异搞懂 Next.js 为什么是前端 SEO 的终极方案
前端·react.js·next.js