/
AlexPrime
/
mpt
Обзор
Документация
Войти
/
AlexPrime
/
mpt
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
client/src/components/theme-toggle.tsx
43 строки
994 B
AlexPrime
upload files
04 дек 2025, 08:13
04 дек 2025, 08:13
b3c018d
Код
Авторство
О чём код?
import { Moon, Sun } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useState, useEffect } from "react"; export function ThemeToggle() { const [isDark, setIsDark] = useState(false); useEffect(() => { const root = document.documentElement; const isDarkMode = root.classList.contains("dark"); setIsDark(isDarkMode); }, []); const toggleTheme = () => { const root = document.documentElement; const newIsDark = !isDark; if (newIsDark) { root.classList.add("dark"); } else { root.classList.remove("dark"); } localStorage.setItem("mpt-theme", newIsDark ? "dark" : "light"); setIsDark(newIsDark); }; return ( <Button variant="ghost" size="icon" onClick={toggleTheme} className="gap-2" data-testid="button-theme-toggle" > {isDark ? ( <Sun className="h-4 w-4" /> ) : ( <Moon className="h-4 w-4" /> )} </Button> ); }