Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(components): add ThemeToggle component #6544

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

M0HAMEDHANY
Copy link

Property Value
Title Theme Toggle
Description A customizable dark mode toggle component.
Component true
Documentation Link docs
API Link next-themes

Installation

CLI Manual npx shadcn@latest add theme-toggle Install the necessary dependencies: npm install @shadcn/theme-toggle Copy and paste the following code into your project. Update the import paths to match your project setup.

Usage

Wrapping the Application with

install next-themes

npm install next-themes

To use the ThemeToggle component, make sure your app is wrapped with the <ThemeProvider> to handle theme changes globally.

import { ThemeProvider } from "next-themes";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        <ThemeProvider attribute="class" defaultTheme="system" enableSystem>
          {children}
        </ThemeProvider>
      </body>
    </html>
  );
}

Import the Component

import { ThemeToggle } from "@/components/ui/theme-toggle"

Add it to the Navbar for example

import { ThemeToggle } from "@/components/ui/theme-toggle";

export default function Header() {
    return (
        <nav className="flex justify-between p-4 bg-background">
            <h1 className="text-2xl font-bold dark:text-white">Shadcn ui</h1>
            <ThemeToggle />
        </nav>
    );
}

Examples

Default

Small

With Custom Label

Disabled

Copy link

vercel bot commented Feb 2, 2025

@M0HAMEDHANY is attempting to deploy a commit to the shadcn-pro Team on Vercel.

A member of the Team first needs to authorize it.

@shadcn
Copy link
Collaborator

shadcn commented Feb 5, 2025

@M0HAMEDHANY heads up, this should probably be under blocks (not ui). We can have different variants then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants