import { ReactElement } from "react";
import { useTheme } from "../../hooks/useTheme";
import { Sun, Moon } from "lucide-react";
function ThemeToggler(): ReactElement {
const { theme, setTheme } = useTheme();
function toggleTheme() {
setTheme((prevTheme) => (prevTheme === "light" ? "dark" : "light"));
}
return theme === "light" ? (
<Moon onClick={toggleTheme} />
) : (
<Sun onClick={toggleTheme} />
);
}
export default ThemeToggler;
uusetheme.tsx
import { useContext } from "react";
import { ThemeContext } from "../Contexts/ThemeContext";
export const useTheme = () => {
const context = useContext(ThemeContext);
if (!context) {
throw new Error("useTheme deve ser usado dentro de um ThemeProvider");
}
return context;
};
header.tsx
import { ReactElement } from "react";
import ThemeToggler from "../ui/ThemeToggler";
function Header(): ReactElement {
return (
<header
className={
"flex h-20 w-svw items-center justify-between px-4 text-xl"
}
>
<div>
<h1>CashierApp</h1>
</div>
<div>
<nav>
<ul className="flex flex-row items-center gap-8">
<li>Home</li>
<li>Controle de caixa</li>
<li>Cadastros</li>
</ul>
</nav>
</div>
<div>
<ThemeToggler />
</div>
</header>
);
}
export default Header;
tailwind.config.ts
/** @type {import('tailwindcss').Config} */
export default {
content: ["./index.html", "./src/**/*.{html,js,jsx,ts,tsx}"],
theme: {
extend: {
colors: {
primary: "var(--color-bg-primary)",
secondary: "var(--color-bg-secondary)",
textPrimary: "var(--color-text-primary)",
textSecondary: "var(--color-text-secondary)",
textAccent: "var(--color-text-accent)",
},
},
},
plugins: [],
};
Edit:我正在使用parwindcss v4,我的CSS文件具有以下语句:
@导入“ tailwindcss”;
@tailwind实用程序;
tailwindcssv4
的CSS优先配置由于您正在使用V4,因此您不需要
tailwind.config.js
CSS-优先配置
-TailWindcss V4Blogv4中的新配置选项 - stackoverflow
@theme
@import "tailwindcss";
@theme {
--font-display: "Satoshi", "sans-serif";
--breakpoint-3xl: 120rem;
--color-avocado-100: oklch(0.99 0 0);
--color-avocado-200: oklch(0.98 0.04 113.22);
--color-avocado-300: oklch(0.94 0.11 115.03);
--color-avocado-400: oklch(0.92 0.19 114.08);
--color-avocado-500: oklch(0.84 0.18 117.33);
--color-avocado-600: oklch(0.53 0.12 118.34);
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
--ease-snappy: cubic-bezier(0.2, 0, 0, 1);
/* ... */
}
@theme
指令
theme变量在命名空间中定义,每个命名空间对应于一个或多个实用程序类或变体API。theme变量命名空间 - tailwindcss v4docs
default主题变量参考 - tailwindcss v4docs
,但是,仍然可以继续通过
tailwind.config.js
指令继续使用@config
tailwindcss v4向后兼容V3-StackoverFlow