A CLI tool that instantly sets up dark/light theme toggling in your Next.js projects + Tailwind CSS v4 with zero configuration hassle.
- One command setup - Get theme switching working in seconds
- Tailwind CSS v4 - Automatically installs/upgrades to latest Tailwind
- TypeScript & JavaScript - Full support for both
- Smooth transitions - Elegant animations between themes
- Smart theme toggle - Uses
next-themes
for system preference detection - Responsive design - Works perfectly on mobile and desktop
- Non-destructive - Only adds new files and overwrite few existing files
- Smart detection - Automatically detects your project structure
- Custom components - Includes Header, ThemeToggle, and ExampleCard
- Modern styling - Uses system preferences with manual override
- Zero configuration - Works out of the box
- Node.js 16+
- A Next.js project (App Router - recommended)
- npm, yarn, or pnpm
next-themes
- Theme switching logiclucide-react
- Icons for sun/moontailwindcss@latest
- Styling (v4+)
Option 1: Direct execution (Recommended)
npx next-theme-toggle
Option 2: Install then run
npm i next-theme-toggle
next-theme-toggle
next-theme-toggle-demo-video.mp4
Scenario 1: Fresh Tailwind CSS Installation
🚀 Setting up Next.js theme toggle...
√ Next.js project detected
[i] Using npm package manager
[i] Installing Tailwind CSS dependencies:
- tailwindcss
- @tailwindcss/postcss
- postcss
√ Packages installed: tailwindcss, @tailwindcss/postcss, postcss
√ Created postcss.config.mjs with Tailwind CSS v4 configuration
√ Complete globals.css copied from template to src\app\globals.css
[i] Installing dependencies:
- lucide-react
- next-themes
√ Packages installed: lucide-react, next-themes
√ Dependencies installed successfully
[i] Using app directory: src/app
√ Successfully created component 'ThemeToggle.tsx' in src/app/components
√ Successfully created component 'Header.tsx' in src/app/components/layout
√ Successfully created component 'ExampleCard.tsx' in src/app/components
√ Successfully overwritten layout.tsx in src/app with ThemeProvider
√ Successfully overwritten page.tsx in src/app
Theme toggle setup complete!
Scenario 2: Existing Tailwind CSS Setup
🚀 Setting up Next.js theme toggle...
√ Next.js project detected
[i] Using npm package manager
√ Tailwind CSS v4+ already installed
√ Custom theme styles appended to src\app\globals.css
[i] Installing dependencies:
- lucide-react
- next-themes
√ Packages installed: lucide-react, next-themes
√ Dependencies installed successfully
[i] Using app directory: src/app
√ Successfully created component 'ThemeToggle.tsx' in src/app/components
√ Successfully created component 'Header.tsx' in src/app/components/layout
√ Successfully created component 'ExampleCard.tsx' in src/app/components
√ Successfully overwritten layout.tsx in src/app with ThemeProvider
√ Successfully overwritten page.tsx in src/app
Theme toggle setup complete!
That's it! The tool will automatically:
- Detect your package manager (npm/yarn/pnpm)
- Verify you're in a Next.js project
- Install/upgrade Tailwind CSS to v4
- Inject Tailwind directives into your CSS
- Install required dependencies (
next-themes
,lucide-react
) - Create theme toggle components
- Set up your layout with ThemeProvider
- Update your layout and page files
- Configure CSS with smooth transitions
src/app/components/ # or app/components/
├── layout/
│ └── Header.tsx # Navigation with theme toggle
├── ExampleCard.tsx # Sample card component
└── ThemeToggle.tsx # The magic toggle button
components/ThemeToggle.tsx
- The theme toggle buttoncomponents/layout/Header.tsx
- Header component with the togglecomponents/ExampleCard.tsx
- Example Card component
layout.tsx
- Wrapped with ThemeProviderpage.tsx
- Updated with new componentsglobals.css
- add custom css varient and smooth transition effectpostcss.config.mjs
- Configured for Tailwind v4
After running the command, your components are ready to use:
// In your layout.tsx (automatically done)
import { ThemeProvider } from "next-themes";
export default function RootLayout({ children }) {
return (
<html lang="en" suppressHydrationWarning>
<body>
<ThemeProvider
attribute="data-theme"
defaultTheme="system"
enableSystem
>
{children}
</ThemeProvider>
</body>
</html>
);
}
// In your page.tsx or any component
import Header from "@/app/components/layout/Header";
import ExampleCard from "@/app/components/ExampleCard";
export default function HomePage() {
return (
<div>
<Header />
<main className="container mx-auto px-4 py-8">
<ExampleCard />
</main>
</div>
);
}
# Default installation
npx next-theme-toggle
# Skip Tailwind CSS setup (if you have custom setup)
npx next-theme-toggle --no-tailwind
# or
npx next-theme-toggle -T
The tool creates CSS variables you can customize in globals.css
:
:root {
--background: #ffffff;
--foreground: #171717;
}
@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}
All components use Tailwind classes and can be easily customized:
// Example: Customize the theme toggle button
<button className="your-custom-classes">{/* Theme toggle content */}</button>
Theme not working?
- Make sure Tailwind's
dark:
classes are in your CSS - Check that ThemeProvider wraps your app in layout.tsx
Components not found?
- Verify the components are copied to your components folder
- Check your import paths match your project structure
"No package.json found"
- Make sure you're in your Next.js project root directory
"This doesn't appear to be a Next.js project"
- Ensure Next.js is installed in dependencies or devDependencies
Tailwind styles not working
- Make sure your
postcss.config.mjs
includes@tailwindcss/postcss
- Restart your dev server after installation
Still having issues?
- Make sure you're running
npm run dev
after setup - Check console for any import/export errors
If path aliases don't work, update your imports:
// Instead of @/app/components/...
import ThemeToggle from "./components/ThemeToggle";
import Header from "./components/layout/Header";
We love contributions! Issues and PRs welcome! This tool is designed to be simple and focused - let's keep it that way. Here's how to get started:
-
Fork & Clone
git clone https://github.com/megh-bari/next-theme-toggle.git cd next-theme-toggle
-
Install Dependencies
npm install
-
Test Locally
# Link for local testing npm link (try running this command in the terminal as an administrator) # Test in a Next.js project cd /path/to/your-nextjs-project npm link next-theme-toggle next-theme-toggle
-
Create a Feature Branch
git checkout -b feature/amazing-feature
-
Make Your Changes
- Update the main script in
index.js
- Add/modify templates in
templates/
directory - Update tests if applicable
- Update the main script in
-
Test Your Changes
# Test with different Next.js setups npm run test # Test with TypeScript projects # Test with JavaScript projects # Test with different app structures (src/app vs app)
-
Commit & Push
git add . git commit -m "feat: add amazing feature" git push origin feature/amazing-feature
-
Create Pull Request
- Provide clear description of changes
- Include screenshots if UI changes
- Link any related issues
- Use the bug report template
- Include Node.js and Next.js versions
- Provide minimal reproduction steps
- Include error messages and logs
- Use the feature request template
- Explain the use case and benefits
- Consider backward compatibility
- Provide implementation ideas if possible
- Follow existing code style
- Add comments for complex logic
- Update documentation for new features
- Include error handling
- Test with both TypeScript and JavaScript projects
Before submitting:
- Test with fresh Next.js project
- Test with existing Tailwind setup
- Test with both
src/app
andapp
structures - Test with npm, yarn, and pnpm
- Verify all generated files work correctly
next-theme-toggle/
├── templates/ # Component templates
│ ├── app/
│ │ ├── layout.tsx
│ │ ├── layout.js
│ │ ├── page.tsx
│ │ └── page.js
│ ├── components/
│ │ ├── layout/
│ │ │ ├── Header.tsx
│ │ │ └── Header.jsx
│ │ ├── ExampleCard.tsx
│ │ ├── ExampleCard.jsx
│ │ ├── ThemeToggle.tsx
│ │ └── ThemeToggle.jsx
│ └── globals.css
├── index.js # Main CLI script
├── package.json
└── README.md
- Update version in
package.json
- Update CHANGELOG.md
- Create release tag
- Publish to npm
This project is licensed under the MIT License - see the LICENSE file for details.
- Next.js for the amazing React framework
- Tailwind CSS for utility-first styling
- next-themes for theme management
- Lucide for beautiful icons
- ⭐ Star this repo if it helped you!
- 🐛 Report issues on GitHub Issues
- 💡 Request features on GitHub Discussions
- Twitter: @meghtrix
- GitHub: @megh-bari
Happy theming!