Theme Toggle
The ThemeToggle component creates a dark/light mode switch with HTMX server-side persistence.
Quick Start
Usage Scenarios
With Optional Icon
With Label
In Navbar
Navbar(
brand="MyApp",
items=[
NavItem("Home", href="/"),
ThemeToggle(current_theme=req.session.get("theme", "light"), cls="ms-auto")
]
)
Server-Side Theme Management
@app.post("/theme/toggle")
def toggle_theme(req):
current = req.session.get("theme", "light")
new_theme = "dark" if current == "light" else "light"
req.session["theme"] = new_theme
from faststrap.presets import hx_refresh
return hx_refresh()
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
current_theme |
str |
"auto" | Current theme ("light", "dark", "auto") |
endpoint |
str |
"/theme/toggle" | Server endpoint for theme changes |
toggle_id |
str |
"theme-toggle" | Unique ID for the toggle |
show_label |
bool |
False |
Whether to show label text |
label_text |
str |
"Dark Mode" | Label text to display |
show_icon |
bool |
False |
Whether to show the decorative sun/moon icon |
**kwargs |
Any |
- | Additional HTML attributes |
Accessibility
- Uses
role="switch"on the input for proper screen reader semantics. - Label is associated via
forandidattributes. - Decorative icons use
aria-hidden="true".
API Reference
faststrap.components.forms.theme_toggle.ThemeToggle(current_theme='auto', endpoint='/theme/toggle', toggle_id='theme-toggle', show_label=False, label_text='Dark Mode', show_icon=False, **kwargs)
Dark/light mode toggle switch with HTMX persistence.
Creates a toggle switch that POSTs to a server endpoint to persist theme preference. The server should handle the toggle logic and return updated UI or trigger a page refresh.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_theme
|
ThemeType
|
Current theme ("light", "dark", "auto") |
'auto'
|
endpoint
|
str
|
Server endpoint to POST theme changes |
'/theme/toggle'
|
toggle_id
|
str
|
Unique ID for the toggle |
'theme-toggle'
|
show_label
|
bool
|
Whether to show label text |
False
|
label_text
|
str
|
Label text to display |
'Dark Mode'
|
show_icon
|
bool
|
Whether to render the decorative theme icon |
False
|
**kwargs
|
Any
|
Additional HTML attributes |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
Div containing the theme toggle switch |
Examples:
Basic toggle:
With label:
Custom endpoint:
Server-side handler:
@app.post("/theme/toggle")
def toggle_theme(req: Request):
current = req.session.get("theme", "light")
new_theme = "dark" if current == "light" else "light"
req.session["theme"] = new_theme
# Return updated UI or trigger refresh
from faststrap.presets import hx_refresh
return hx_refresh()
Note
The toggle uses Bootstrap's form-check-input styling. The server endpoint should: 1. Read current theme from session/cookie 2. Toggle to new theme 3. Save to session/cookie 4. Return response (refresh, redirect, or updated HTML)
Source code in src/faststrap/components/forms/theme_toggle.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |