Get Started
Components
- Accordion
- Alert
- Alert Dialog
- Aspect Ratio
- Attachment
- Avatar
- Badge
- Breadcrumb
- Bubble
- Button
- Button Group
- Card
- Checkbox
- Collapsible
- Combobox
- Context Menu
- Date Picker (Lite)
- Dialog
- Direction
- Drawer
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP (Lite)
- Item
- Kbd
- Label
- Marker
- Menubar
- Message
- Native Select
- Navigation Menu
- Pagination
- Popover
- Progress
- Radio Group
- Scroll Area
- Select
- Separator
- Sheet
- Sidebar
- Skeleton
- Slider
- Spinner
- Switch
- Table
- Tabs
- Textarea
- Toast
- Toggle
- Toggle Group
- Tooltip
The theme defines dark colors under a .dark class and a dark variant for Tailwind, so dark mode is a matter of setting that class on <html>.
Apply the theme before the first paint
Add a small inline script to <head> that reads the stored choice, or the system preference, before the page renders:
import { raw } from "hono/html"
const themeScript = `try {
const theme = localStorage.theme
if (theme === "dark" || (!theme && matchMedia("(prefers-color-scheme: dark)").matches)) {
document.documentElement.classList.add("dark")
}
} catch {}`
// in <head>
<script>{raw(themeScript)}</script>Add a mode toggle
Render a button with the components, and toggle the class in a module script:
import { Button } from "@/components/ui/button"
<Button variant="outline" size="icon" data-mode-toggle>
<span class="sr-only">Toggle theme</span>
</Button>document.addEventListener("click", (event) => {
if (!event.target.closest("[data-mode-toggle]")) return
const dark = document.documentElement.classList.toggle("dark")
localStorage.theme = dark ? "dark" : "light"
})In HonoX you can write the toggle as an island instead.