snapshot of https://github.com/shadcn-ui/ui.git (main, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
114
apps/v4/content/docs/dark-mode/astro.mdx
Normal file
114
apps/v4/content/docs/dark-mode/astro.mdx
Normal file
@@ -0,0 +1,114 @@
|
||||
---
|
||||
title: Astro
|
||||
description: Adding dark mode to your astro app.
|
||||
---
|
||||
|
||||
## Create an inline theme script
|
||||
|
||||
```astro title="src/pages/index.astro"
|
||||
---
|
||||
import '../styles/globals.css'
|
||||
---
|
||||
|
||||
<script is:inline>
|
||||
const getThemePreference = () => {
|
||||
if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
|
||||
return localStorage.getItem('theme');
|
||||
}
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
};
|
||||
const isDark = getThemePreference() === 'dark';
|
||||
document.documentElement.classList[isDark ? 'add' : 'remove']('dark');
|
||||
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
const observer = new MutationObserver(() => {
|
||||
const isDark = document.documentElement.classList.contains('dark');
|
||||
localStorage.setItem('theme', isDark ? 'dark' : 'light');
|
||||
});
|
||||
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
|
||||
}
|
||||
</script>
|
||||
|
||||
<html lang="en">
|
||||
<body>
|
||||
<h1>Astro</h1>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
## Add a mode toggle
|
||||
|
||||
```tsx title="src/components/ModeToggle.tsx"
|
||||
import * as React from "react"
|
||||
import { Moon, Sun } from "lucide-react"
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
|
||||
export function ModeToggle() {
|
||||
const [theme, setThemeState] = React.useState<
|
||||
"theme-light" | "dark" | "system"
|
||||
>("theme-light")
|
||||
|
||||
React.useEffect(() => {
|
||||
const isDarkMode = document.documentElement.classList.contains("dark")
|
||||
setThemeState(isDarkMode ? "dark" : "theme-light")
|
||||
}, [])
|
||||
|
||||
React.useEffect(() => {
|
||||
const isDark =
|
||||
theme === "dark" ||
|
||||
(theme === "system" &&
|
||||
window.matchMedia("(prefers-color-scheme: dark)").matches)
|
||||
document.documentElement.classList[isDark ? "add" : "remove"]("dark")
|
||||
}, [theme])
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="outline" size="icon">
|
||||
<Sun className="h-[1.2rem] w-[1.2rem] scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90" />
|
||||
<Moon className="absolute h-[1.2rem] w-[1.2rem] scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0" />
|
||||
<span className="sr-only">Toggle theme</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => setThemeState("theme-light")}>
|
||||
Light
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setThemeState("dark")}>
|
||||
Dark
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setThemeState("system")}>
|
||||
System
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## Display the mode toggle
|
||||
|
||||
Place a mode toggle on your site to toggle between light and dark mode.
|
||||
|
||||
```astro title="src/pages/index.astro"
|
||||
---
|
||||
import '../styles/globals.css'
|
||||
import { ModeToggle } from '@/components/ModeToggle';
|
||||
---
|
||||
|
||||
<!-- Inline script -->
|
||||
|
||||
<html lang="en">
|
||||
<body>
|
||||
<h1>Astro</h1>
|
||||
<ModeToggle client:load />
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
75
apps/v4/content/docs/dark-mode/index.mdx
Normal file
75
apps/v4/content/docs/dark-mode/index.mdx
Normal file
File diff suppressed because one or more lines are too long
4
apps/v4/content/docs/dark-mode/meta.json
Normal file
4
apps/v4/content/docs/dark-mode/meta.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"title": "Dark mode",
|
||||
"pages": ["index", "next", "vite", "astro", "remix", "tanstack-start"]
|
||||
}
|
||||
70
apps/v4/content/docs/dark-mode/next.mdx
Normal file
70
apps/v4/content/docs/dark-mode/next.mdx
Normal file
@@ -0,0 +1,70 @@
|
||||
---
|
||||
title: Next.js
|
||||
description: Adding dark mode to your Next.js app.
|
||||
---
|
||||
|
||||
<Steps>
|
||||
|
||||
## Install next-themes
|
||||
|
||||
Start by installing `next-themes`:
|
||||
|
||||
```bash
|
||||
npm install next-themes
|
||||
```
|
||||
|
||||
## Create a theme provider
|
||||
|
||||
```tsx title="components/theme-provider.tsx" showLineNumbers
|
||||
"use client"
|
||||
|
||||
import * as React from "react"
|
||||
import { ThemeProvider as NextThemesProvider } from "next-themes"
|
||||
|
||||
export function ThemeProvider({
|
||||
children,
|
||||
...props
|
||||
}: React.ComponentProps<typeof NextThemesProvider>) {
|
||||
return <NextThemesProvider {...props}>{children}</NextThemesProvider>
|
||||
}
|
||||
```
|
||||
|
||||
## Wrap your root layout
|
||||
|
||||
Add the `ThemeProvider` to your root layout and add the `suppressHydrationWarning` prop to the `html` tag.
|
||||
|
||||
```tsx {1,6,9-14,16} title="app/layout.tsx" showLineNumbers
|
||||
import { ThemeProvider } from "@/components/theme-provider"
|
||||
|
||||
export default function RootLayout({ children }: RootLayoutProps) {
|
||||
return (
|
||||
<>
|
||||
<html lang="en" suppressHydrationWarning>
|
||||
<head />
|
||||
<body>
|
||||
<ThemeProvider
|
||||
attribute="class"
|
||||
defaultTheme="system"
|
||||
enableSystem
|
||||
disableTransitionOnChange
|
||||
>
|
||||
{children}
|
||||
</ThemeProvider>
|
||||
</body>
|
||||
</html>
|
||||
</>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## Add a mode toggle
|
||||
|
||||
Place a mode toggle on your site to toggle between light and dark mode.
|
||||
|
||||
<ComponentPreview
|
||||
styleName="new-york-v4"
|
||||
name="mode-toggle"
|
||||
className="[&_.preview]:items-start"
|
||||
/>
|
||||
|
||||
</Steps>
|
||||
157
apps/v4/content/docs/dark-mode/remix.mdx
Normal file
157
apps/v4/content/docs/dark-mode/remix.mdx
Normal file
@@ -0,0 +1,157 @@
|
||||
---
|
||||
title: Remix
|
||||
description: Adding dark mode to your Remix app.
|
||||
---
|
||||
|
||||
<Steps>
|
||||
|
||||
## Modify your tailwind.css file
|
||||
|
||||
Add `:root[class~="dark"]` to your tailwind.css file. This will allow you to use the `dark` class on your html element to apply dark mode styles.
|
||||
|
||||
```css {2} title="app/tailwind.css" showLineNumbers
|
||||
.dark,
|
||||
:root[class~="dark"] {
|
||||
...;
|
||||
}
|
||||
```
|
||||
|
||||
## Install remix-themes
|
||||
|
||||
Start by installing `remix-themes`:
|
||||
|
||||
```bash
|
||||
npm install remix-themes
|
||||
```
|
||||
|
||||
## Create a session storage and theme session resolver
|
||||
|
||||
```tsx title="app/sessions.server.tsx" showLineNumbers
|
||||
import { createThemeSessionResolver } from "remix-themes"
|
||||
|
||||
// You can default to 'development' if process.env.NODE_ENV is not set
|
||||
const isProduction = process.env.NODE_ENV === "production"
|
||||
|
||||
const sessionStorage = createCookieSessionStorage({
|
||||
cookie: {
|
||||
name: "theme",
|
||||
path: "/",
|
||||
httpOnly: true,
|
||||
sameSite: "lax",
|
||||
secrets: ["s3cr3t"],
|
||||
// Set domain and secure only if in production
|
||||
...(isProduction
|
||||
? { domain: "your-production-domain.com", secure: true }
|
||||
: {}),
|
||||
},
|
||||
})
|
||||
|
||||
export const themeSessionResolver = createThemeSessionResolver(sessionStorage)
|
||||
```
|
||||
|
||||
## Set up Remix Themes
|
||||
|
||||
Add the `ThemeProvider` to your root layout.
|
||||
|
||||
```tsx {1-3,6-11,15-22,25-26,28,33} title="app/root.tsx" showLineNumbers
|
||||
import clsx from "clsx"
|
||||
import { PreventFlashOnWrongTheme, ThemeProvider, useTheme } from "remix-themes"
|
||||
|
||||
import { themeSessionResolver } from "./sessions.server"
|
||||
|
||||
// Return the theme from the session storage using the loader
|
||||
export async function loader({ request }: LoaderFunctionArgs) {
|
||||
const { getTheme } = await themeSessionResolver(request)
|
||||
return {
|
||||
theme: getTheme(),
|
||||
}
|
||||
}
|
||||
// Wrap your app with ThemeProvider.
|
||||
// `specifiedTheme` is the stored theme in the session storage.
|
||||
// `themeAction` is the action name that's used to change the theme in the session storage.
|
||||
export default function AppWithProviders() {
|
||||
const data = useLoaderData<typeof loader>()
|
||||
return (
|
||||
<ThemeProvider specifiedTheme={data.theme} themeAction="/action/set-theme">
|
||||
<App />
|
||||
</ThemeProvider>
|
||||
)
|
||||
}
|
||||
|
||||
export function App() {
|
||||
const data = useLoaderData<typeof loader>()
|
||||
const [theme] = useTheme()
|
||||
return (
|
||||
<html lang="en" className={clsx(theme)}>
|
||||
<head>
|
||||
<meta charSet="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<Meta />
|
||||
<PreventFlashOnWrongTheme ssrTheme={Boolean(data.theme)} />
|
||||
<Links />
|
||||
</head>
|
||||
<body>
|
||||
<Outlet />
|
||||
<ScrollRestoration />
|
||||
<Scripts />
|
||||
<LiveReload />
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## Add an action route
|
||||
|
||||
Create a file in `/routes/action.set-theme.ts`. Ensure that you pass the filename to the ThemeProvider component. This route is used to store the preferred theme in the session storage when the user changes it.
|
||||
|
||||
```tsx title="app/routes/action.set-theme.ts" showLineNumbers
|
||||
import { createThemeAction } from "remix-themes"
|
||||
|
||||
import { themeSessionResolver } from "./sessions.server"
|
||||
|
||||
export const action = createThemeAction(themeSessionResolver)
|
||||
```
|
||||
|
||||
## Add a mode toggle
|
||||
|
||||
Place a mode toggle on your site to toggle between light and dark mode.
|
||||
|
||||
```tsx title="components/mode-toggle.tsx" showLineNumbers
|
||||
import { Moon, Sun } from "lucide-react"
|
||||
import { Theme, useTheme } from "remix-themes"
|
||||
|
||||
import { Button } from "./ui/button"
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "./ui/dropdown-menu"
|
||||
|
||||
export function ModeToggle() {
|
||||
const [, setTheme] = useTheme()
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="ghost" size="icon">
|
||||
<Sun className="h-[1.2rem] w-[1.2rem] scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90" />
|
||||
<Moon className="absolute h-[1.2rem] w-[1.2rem] scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0" />
|
||||
<span className="sr-only">Toggle theme</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => setTheme(Theme.LIGHT)}>
|
||||
Light
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme(Theme.DARK)}>
|
||||
Dark
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
</Steps>
|
||||
191
apps/v4/content/docs/dark-mode/tanstack-start.mdx
Normal file
191
apps/v4/content/docs/dark-mode/tanstack-start.mdx
Normal file
@@ -0,0 +1,191 @@
|
||||
---
|
||||
title: TanStack Start
|
||||
description: Adding dark mode to your TanStack Start app.
|
||||
---
|
||||
|
||||
<Steps>
|
||||
|
||||
### Create a theme provider
|
||||
|
||||
TanStack Start uses `ScriptOnce` from `@tanstack/react-router` to inject a script that runs before React hydrates, preventing flash of unstyled content (FOUC).
|
||||
|
||||
```tsx title="components/theme-provider.tsx" showLineNumbers
|
||||
import { createContext, useContext, useEffect, useState } from "react"
|
||||
import { ScriptOnce } from "@tanstack/react-router"
|
||||
|
||||
type Theme = "dark" | "light" | "system"
|
||||
|
||||
type ThemeProviderProps = {
|
||||
children: React.ReactNode
|
||||
defaultTheme?: Theme
|
||||
storageKey?: string
|
||||
}
|
||||
|
||||
type ThemeProviderState = {
|
||||
theme: Theme
|
||||
setTheme: (theme: Theme) => void
|
||||
}
|
||||
|
||||
function getThemeScript(storageKey: string, defaultTheme: Theme) {
|
||||
const key = JSON.stringify(storageKey)
|
||||
const fallback = JSON.stringify(defaultTheme)
|
||||
|
||||
return `(function(){try{var t=localStorage.getItem(${key});if(t!=='light'&&t!=='dark'&&t!=='system'){t=${fallback}}var d=matchMedia('(prefers-color-scheme: dark)').matches;var r=t==='system'?(d?'dark':'light'):t;var e=document.documentElement;e.classList.add(r);e.style.colorScheme=r}catch(e){}})();`
|
||||
}
|
||||
|
||||
const ThemeProviderContext = createContext<ThemeProviderState>({
|
||||
theme: "system",
|
||||
setTheme: () => {},
|
||||
})
|
||||
|
||||
function applyTheme(theme: Theme) {
|
||||
const root = document.documentElement
|
||||
root.classList.remove("light", "dark")
|
||||
|
||||
const resolved =
|
||||
theme === "system"
|
||||
? window.matchMedia("(prefers-color-scheme: dark)").matches
|
||||
? "dark"
|
||||
: "light"
|
||||
: theme
|
||||
|
||||
root.classList.add(resolved)
|
||||
root.style.colorScheme = resolved
|
||||
}
|
||||
|
||||
export function ThemeProvider({
|
||||
children,
|
||||
defaultTheme = "system",
|
||||
storageKey = "theme",
|
||||
}: ThemeProviderProps) {
|
||||
const [theme, setThemeState] = useState<Theme>(defaultTheme)
|
||||
const [mounted, setMounted] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
const stored = localStorage.getItem(storageKey)
|
||||
setThemeState(
|
||||
stored === "light" || stored === "dark" || stored === "system"
|
||||
? stored
|
||||
: defaultTheme
|
||||
)
|
||||
setMounted(true)
|
||||
}, [defaultTheme, storageKey])
|
||||
|
||||
useEffect(() => {
|
||||
if (!mounted) return
|
||||
applyTheme(theme)
|
||||
}, [theme, mounted])
|
||||
|
||||
useEffect(() => {
|
||||
if (!mounted || theme !== "system") return
|
||||
|
||||
const media = window.matchMedia("(prefers-color-scheme: dark)")
|
||||
const onChange = () => applyTheme("system")
|
||||
media.addEventListener("change", onChange)
|
||||
return () => media.removeEventListener("change", onChange)
|
||||
}, [theme, mounted])
|
||||
|
||||
const setTheme = (next: Theme) => {
|
||||
localStorage.setItem(storageKey, next)
|
||||
setThemeState(next)
|
||||
}
|
||||
|
||||
return (
|
||||
<ThemeProviderContext value={{ theme, setTheme }}>
|
||||
<ScriptOnce>{getThemeScript(storageKey, defaultTheme)}</ScriptOnce>
|
||||
{children}
|
||||
</ThemeProviderContext>
|
||||
)
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
const context = useContext(ThemeProviderContext)
|
||||
if (context === undefined)
|
||||
throw new Error("useTheme must be used within a ThemeProvider")
|
||||
return context
|
||||
}
|
||||
```
|
||||
|
||||
### Wrap your root layout
|
||||
|
||||
Add the `ThemeProvider` to your root layout and add the `suppressHydrationWarning` prop to the `html` tag.
|
||||
|
||||
```tsx {8,19,24-26} title="src/routes/__root.tsx" showLineNumbers
|
||||
import {
|
||||
createRootRoute,
|
||||
HeadContent,
|
||||
Outlet,
|
||||
Scripts,
|
||||
} from "@tanstack/react-router"
|
||||
|
||||
import { ThemeProvider } from "@/components/theme-provider"
|
||||
|
||||
export const Route = createRootRoute({
|
||||
head: () => ({
|
||||
// ...
|
||||
}),
|
||||
component: RootComponent,
|
||||
})
|
||||
|
||||
function RootComponent() {
|
||||
return (
|
||||
<html lang="en" suppressHydrationWarning>
|
||||
<head>
|
||||
<HeadContent />
|
||||
</head>
|
||||
<body>
|
||||
<ThemeProvider defaultTheme="system" storageKey="theme">
|
||||
<Outlet />
|
||||
</ThemeProvider>
|
||||
<Scripts />
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
### Add a mode toggle
|
||||
|
||||
Place a mode toggle on your site to toggle between light and dark mode.
|
||||
|
||||
```tsx title="components/mode-toggle.tsx" showLineNumbers
|
||||
import { Moon, Sun } from "lucide-react"
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
import { useTheme } from "@/components/theme-provider"
|
||||
|
||||
export function ModeToggle() {
|
||||
const { setTheme } = useTheme()
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="outline" size="icon">
|
||||
<Sun className="h-[1.2rem] w-[1.2rem] scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90" />
|
||||
<Moon className="absolute h-[1.2rem] w-[1.2rem] scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0" />
|
||||
<span className="sr-only">Toggle theme</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => setTheme("light")}>
|
||||
Light
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme("dark")}>
|
||||
Dark
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme("system")}>
|
||||
System
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
</Steps>
|
||||
144
apps/v4/content/docs/dark-mode/vite.mdx
Normal file
144
apps/v4/content/docs/dark-mode/vite.mdx
Normal file
@@ -0,0 +1,144 @@
|
||||
---
|
||||
title: Vite
|
||||
description: Adding dark mode to your Vite app.
|
||||
---
|
||||
|
||||
## Create a theme provider
|
||||
|
||||
```tsx title="components/theme-provider.tsx" showLineNumbers
|
||||
import { createContext, useContext, useEffect, useState } from "react"
|
||||
|
||||
type Theme = "dark" | "light" | "system"
|
||||
|
||||
type ThemeProviderProps = {
|
||||
children: React.ReactNode
|
||||
defaultTheme?: Theme
|
||||
storageKey?: string
|
||||
}
|
||||
|
||||
type ThemeProviderState = {
|
||||
theme: Theme
|
||||
setTheme: (theme: Theme) => void
|
||||
}
|
||||
|
||||
const initialState: ThemeProviderState = {
|
||||
theme: "system",
|
||||
setTheme: () => null,
|
||||
}
|
||||
|
||||
const ThemeProviderContext = createContext<ThemeProviderState>(initialState)
|
||||
|
||||
export function ThemeProvider({
|
||||
children,
|
||||
defaultTheme = "system",
|
||||
storageKey = "vite-ui-theme",
|
||||
...props
|
||||
}: ThemeProviderProps) {
|
||||
const [theme, setTheme] = useState<Theme>(
|
||||
() => (localStorage.getItem(storageKey) as Theme) || defaultTheme
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
const root = window.document.documentElement
|
||||
|
||||
root.classList.remove("light", "dark")
|
||||
|
||||
if (theme === "system") {
|
||||
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)")
|
||||
.matches
|
||||
? "dark"
|
||||
: "light"
|
||||
|
||||
root.classList.add(systemTheme)
|
||||
return
|
||||
}
|
||||
|
||||
root.classList.add(theme)
|
||||
}, [theme])
|
||||
|
||||
const value = {
|
||||
theme,
|
||||
setTheme: (theme: Theme) => {
|
||||
localStorage.setItem(storageKey, theme)
|
||||
setTheme(theme)
|
||||
},
|
||||
}
|
||||
|
||||
return (
|
||||
<ThemeProviderContext.Provider {...props} value={value}>
|
||||
{children}
|
||||
</ThemeProviderContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
export const useTheme = () => {
|
||||
const context = useContext(ThemeProviderContext)
|
||||
|
||||
if (context === undefined)
|
||||
throw new Error("useTheme must be used within a ThemeProvider")
|
||||
|
||||
return context
|
||||
}
|
||||
```
|
||||
|
||||
## Wrap your root layout
|
||||
|
||||
Add the `ThemeProvider` to your root layout.
|
||||
|
||||
```tsx {1,5-7} title="App.tsx" showLineNumbers
|
||||
import { ThemeProvider } from "@/components/theme-provider"
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<ThemeProvider defaultTheme="dark" storageKey="vite-ui-theme">
|
||||
{children}
|
||||
</ThemeProvider>
|
||||
)
|
||||
}
|
||||
|
||||
export default App
|
||||
```
|
||||
|
||||
## Add a mode toggle
|
||||
|
||||
Place a mode toggle on your site to toggle between light and dark mode.
|
||||
|
||||
```tsx title="components/mode-toggle.tsx" showLineNumbers
|
||||
import { Moon, Sun } from "lucide-react"
|
||||
|
||||
import { Button } from "@/components/ui/button"
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu"
|
||||
import { useTheme } from "@/components/theme-provider"
|
||||
|
||||
export function ModeToggle() {
|
||||
const { setTheme } = useTheme()
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="outline" size="icon">
|
||||
<Sun className="h-[1.2rem] w-[1.2rem] scale-100 rotate-0 transition-all dark:scale-0 dark:-rotate-90" />
|
||||
<Moon className="absolute h-[1.2rem] w-[1.2rem] scale-0 rotate-90 transition-all dark:scale-100 dark:rotate-0" />
|
||||
<span className="sr-only">Toggle theme</span>
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end">
|
||||
<DropdownMenuItem onClick={() => setTheme("light")}>
|
||||
Light
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme("dark")}>
|
||||
Dark
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem onClick={() => setTheme("system")}>
|
||||
System
|
||||
</DropdownMenuItem>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
)
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user