Skip to content

Commit

Permalink
docs(www): update dark-mode for vite (shadcn-ui#1118)
Browse files Browse the repository at this point in the history
Co-authored-by: sanka <sanka.s@fidenz.com>
Co-authored-by: shadcn <m@shadcn.com>
  • Loading branch information
3 people authored Aug 26, 2023
1 parent 170d3c0 commit 613ec35
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions apps/www/content/docs/dark-mode/vite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ description: Adding dark mode to your vite app.
```tsx title="components/theme-provider.tsx"
import { createContext, useContext, useEffect, useState } from "react"

type Theme = "dark" | "light" | "system"

type ThemeProviderProps = {
children: React.ReactNode
defaultTheme?: string
defaultTheme?: Theme
storageKey?: string
}

type ThemeProviderState = {
theme: string
setTheme: (theme: string) => void
theme: Theme
setTheme: (theme: Theme) => void
}

const initialState = {
const initialState: ThemeProviderState = {
theme: "system",
setTheme: () => null,
}
Expand All @@ -36,8 +38,8 @@ export function ThemeProvider({
storageKey = "vite-ui-theme",
...props
}: ThemeProviderProps) {
const [theme, setTheme] = useState(
() => localStorage.getItem(storageKey) || defaultTheme
const [theme, setTheme] = useState<Theme>(
() => (localStorage.getItem(storageKey) as Theme) || defaultTheme
)

useEffect(() => {
Expand All @@ -60,7 +62,7 @@ export function ThemeProvider({

const value = {
theme,
setTheme: (theme: string) => {
setTheme: (theme: Theme) => {
localStorage.setItem(storageKey, theme)
setTheme(theme)
},
Expand Down

0 comments on commit 613ec35

Please sign in to comment.