Command Palette

Search for a command to run...
HQ UI

Vite

Got a Vite project? Let's flip the switch to dark mode and get that cool, sleek look in a snap. Here is how to do it.

Theme Provider

First, you need to create a provider called providers.tsx in your components folder.

providers.tsx
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)

function ThemeProvider({ children, defaultTheme = 'system', storageKey = 'iut', ...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
    }

    const metaThemeColor = document.getElementById('theme-color-meta')
    if (metaThemeColor) {
      if (theme === 'dark') {
        metaThemeColor.setAttribute('content', '#000000')
      } else if (theme === 'light') {
        metaThemeColor.setAttribute('content', '#ffffff')
      } else if (theme === 'system') {
        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
          metaThemeColor.setAttribute('content', '#000000')
        } else {
          metaThemeColor.setAttribute('content', '#ffffff')
        }
      } else {
        metaThemeColor.setAttribute('content', '#ffffff')
      }
    }

    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>
  )
}

const useTheme = () => {
  const context = useContext(ThemeProviderContext)

  if (context === undefined) throw new Error('useTheme must be used within a ThemeProvider')

  return context
}

export { ThemeProvider as Providers, useTheme, type Theme, type ThemeProviderProps, type ThemeProviderState }

Usage

Then you can wrap your app with the provider. For example if you are using Laravel with Inertia.js, you can put it inside your app.tsx like so:

app.tsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'
import { Providers } from './components/providers.tsx'

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <Providers>
      <App />
    </Providers>
  </React.StrictMode>,
)

Theme Switcher

Then for the switcher, you can use the useTheme hook to get the current theme.

npm i @tabler/icons-react

Next create a component theme-switcher.tsx

theme-switcher.tsx
import { IconSun, IconMoon } from '@tabler/icons-react'
import { Button } from './ui/button'
import { useTheme } from './theme-provider'

export function ThemeSwitcher({ variant = 'outline' }: { variant?: 'outline' | 'ghost' }) {
  const { theme, setTheme } = useTheme()
  return (
    <Button
      variant={variant}
      icon
      aria-label={'Switch to ' + theme === 'light' ? 'dark' : 'light' + 'mode'}
      onPress={() => setTheme(theme === 'light' ? 'dark' : 'light')}
    >
      <IconSun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
      <IconMoon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
    </Button>
    )
}