next-theme-toggle
Version:
A CLI tool that instantly sets up dark/light theme toggling in your Next.js projects + Tailwind CSS v4 with zero configuration hassle.
30 lines (24 loc) • 805 B
JSX
'use client';
import { useTheme } from 'next-themes';
import { useEffect, useState } from 'react';
import { Sun, Moon } from 'lucide-react';
export default function ThemeToggle() {
const { resolvedTheme, setTheme } = useTheme();
const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
if (!mounted) return null;
const isDark = resolvedTheme === 'dark';
return (
<button
onClick={() => setTheme(isDark ? 'light' : 'dark')}
className={`p-1 transition-colors duration-200 rounded-md ${
isDark
? 'text-gray-400 hover:text-white'
: 'text-gray-600 hover:text-black'
}`}
aria-label="Toggle theme"
>
{isDark ? <Sun size={18} /> : <Moon size={18} />}
</button>
);
}