Skip to content
X GitHub

Mode Toggle

---
import { ModeToggle } from "@/components/ui/mode-toggle"
---
<ModeToggle />

To install the mode toggle component, run the following command:

Terminal window
pnpm dlx shadcn@latest add @fulldev-ui/mode-toggle
import { ModeToggle } from "@/components/ui/mode-toggle"
<ModeToggle />
  • Automatically detects system preference
  • Persists user preference in localStorage
  • Smooth transitions between themes
  • Accessible with proper aria labels

The ModeToggle component:

  1. Detects the user’s system theme preference on first load
  2. Applies the dark class to the <html> element when dark mode is active
  3. Saves the user’s preference to localStorage
  4. Listens for clicks to toggle between light and dark modes
  5. Ensures smooth transitions without jarring flashes

The component is intentionally self-contained with inline scripts to avoid flash of unstyled content (FOUC) when the page loads.