Theme Switchers
Not planned for production yet
The theme switcher and the dark theme are planned for a future release. Do not add them to a production page yet.
<button type="button" class="btn" data-variant="outline" data-size="icon" aria-label="Toggle dark mode" data-tooltip="Toggle dark mode" data-side="bottom" onclick="window.tsugi.theme.toggle()">
<span class="hidden dark:block"><i class="icon icon-sun" aria-hidden="true"></i></span>
<span class="block dark:hidden"><i class="icon icon-moon" aria-hidden="true"></i></span>
</button>
The theme is the class dark on the html element
window.tsugi.theme.toggle() adds or removes the class dark on html and saves the choice. The theme switcher is an ordinary button that makes that call.
A page applies the saved theme in its head, before anything is drawn
Without these lines in the head, a dark-theme visitor sees a light flash on every load.
<script>
(() => {
try {
if (localStorage.getItem("themeMode") === "dark") {
document.documentElement.classList.add("dark");
}
} catch (_) {}
})();
</script>
The icon and the label follow the theme with the dark: variant, with no script
<div class="flex flex-wrap items-center gap-3">
<button type="button" class="btn" data-variant="ghost" data-size="icon-sm" aria-label="Toggle dark mode" onclick="window.tsugi.theme.toggle()">
<span class="hidden dark:block"><i class="icon icon-sun" aria-hidden="true"></i></span>
<span class="block dark:hidden"><i class="icon icon-moon" aria-hidden="true"></i></span>
</button>
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="window.tsugi.theme.toggle()">
<span class="hidden dark:block"><i class="icon icon-sun" aria-hidden="true"></i></span>
<span class="block dark:hidden"><i class="icon icon-moon" aria-hidden="true"></i></span>
<span class="hidden dark:block">Light mode</span>
<span class="block dark:hidden">Dark mode</span>
</button>
</div>
set() picks a theme by name
<div class="flex flex-wrap gap-2">
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="window.tsugi.theme.set('light')"><i class="icon icon-sun" aria-hidden="true"></i>Light</button>
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="window.tsugi.theme.set('dark')"><i class="icon icon-moon" aria-hidden="true"></i>Dark</button>
</div>
The page reads the theme with get(), and hears a change on tsugi:themechange
<div class="flex flex-col items-start gap-3">
<button type="button" class="btn" data-variant="outline" data-size="sm" onclick="window.tsugi.theme.toggle()">Toggle</button>
<p class="text-muted-foreground text-sm" id="demo-theme-switcher-result"></p>
</div>
<script>
document.addEventListener("DOMContentLoaded", () => {
const result = document.getElementById("demo-theme-switcher-result");
result.textContent = "get() returns " + window.tsugi.theme.get() + ".";
document.addEventListener("tsugi:themechange", (event) => {
result.textContent = "tsugi:themechange says " + event.detail.mode + ".";
});
});
</script>
Two components redraw themselves on a change
A chart and the rich text editor redraw on tsugi:themechange. A canvas or frame the page draws itself has to do the same.
API Reference
Attributes
| Attribute | On | Values | Default | Description |
|---|---|---|---|---|
| class="dark" | the html element | present or absent | absent | The page is in the dark theme. The script adds and removes it; every colour token has a second value under it. |
Methods and properties
| Member | Type | Description |
|---|---|---|
| window.tsugi.theme.toggle() | method | Switches to the other theme. |
| window.tsugi.theme.set(mode) | method | Sets the theme. "dark" turns the dark theme on; any other value turns it off. |
| window.tsugi.theme.get() | method | Returns "dark" or "light". |
Events
| Event | On | Detail | Description |
|---|---|---|---|
| tsugi:themechange | document | { mode } | Sent after every toggle() and set(), also when the theme did not change. mode is "dark" or "light". |
The choice is saved in localStorage under the key themeMode, as "dark" or "light". Where storage is blocked the theme still changes, for that page only.
The theme switcher is a button with no script of its own. The registry does not start it.