Theme switcher
The component for choosing the mode, the primary color and, in Zubazar UI Pro, the theme; it renders as a top bar button, a panel from the right or sections in the page.
zb:theme-switcher zubazar/ui · free (MIT)
Button
The default form is the moon/sun button of the top bar; clicking switches to the opposite of the mode on screen.
<zb:theme-switcher />
Panel
as="drawer" shows the sections in a panel that opens from the right, with **Reset to defaults** and **Done** buttons.
<zb:theme-switcher as="drawer" trigger="button" />
| Prop | Values |
|---|---|
as | button · drawer · inline |
trigger | edge · button · false |
name | The panel's name (for zb:drawer.trigger) |
title · description · label | The panel's title, the line below it, the accessible name of the button that opens it |
In the page
as="inline" draws the sections where it stands; good for a settings page.
<zb:theme-switcher as="inline" title="Appearance" />
| Prop | Values |
|---|---|
reset | true · false |
reset-url | A link that also returns a server-side choice (e.g. the store theme) to its default |
Sections
sections chooses the sections to show and their order: an array or "mode,color".
<zb:theme-switcher as="inline" :sections="['color', 'mode']" :modes="['light', 'dark']" :custom="false" :reset="false" />
| Prop | Values |
|---|---|
sections | mode · color · theme · skin · width · sidebar · navbar · header · footer · language · links |
modes | ['light', 'dark', 'system'] · false |
colors | true · false · key => [#rrggbb, name] |
custom | true · false |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
as | — | button | button (the top bar button) · drawer (a panel from the right) · inline (in the page) |
sections | — | null | The sections and their order: an array or "mode,color"; when null, mode, color, theme |
modes | — | ['light', 'dark', 'system'] | The mode choices, or false; not shown for a theme without a dark member |
colors | boolean | true | The presets: true (Appearance::presets()), false, or key => [#rrggbb, name] |
custom | boolean | true | The free color picker (#rrggbb) |
themes | — | null | The themes on offer: null (what Zubazar UI Pro offers on this request; none in the free package), false, or items (key, name, url…) |
title | — | null | drawer: the panel's title (when null, "Appearance"); inline: a heading above the sections |
description | — | null | The line below the title (in the drawer, when null, "Changes apply right away…"); false hides it |
label | — | null | drawer: the accessible name of the button that opens it (when null, "Appearance settings") |
name | — | theme-switcher | drawer: the panel's name (zb:drawer.trigger, ZubazarUI.drawer.open()) |
trigger | — | edge | drawer: edge (a gear on the right edge) · button (an icon button in place) · false (a button of your own) |
reset | boolean | true | Reset to defaults: clears the scope's saved choices |
reset-url | — | null | With reset: a link that also returns a server-side choice to its default |
headings | boolean | true | false: section headings for screen readers only |
section: name | — | null | The section's key (data-switcher-section) |
section: title | — | — | Heading (required); the group's accessible name |
section: description | — | null | A line below the heading |
section: options | — | [] | value => label or [label, description, icon]; as cards when one has a description |
section: setting | — | null | The appearance choice it is tied to: mode, brandColor, skin, contentWidth, sidebar, navbarMode (no Alpine needed) |
section: value · change | — | null | A choice of your own: the Alpine expression of the chosen value and of the function a choice goes to |
Slots: default (sections of your own: zb:theme-switcher.section)
Advanced
Only the color
Under a label of your own, pass :sections="['color']" for the color alone; :headings="false" leaves the section heading to screen readers only. Choose the presets on offer with colors; a key is default, a preset name or a #rrggbb.
<div class="space-y-2">
<p class="text-sm font-medium text-ink">Store color</p>
<zb:theme-switcher as="inline" :sections="['color']" :headings="false" :reset="false" :custom="false"
:colors="['default' => ['#794dff', 'Default'], 'green' => ['#008933', 'Green'], 'blue' => ['#038197', 'Cyan']]" />
</div>
| Prop | Values |
|---|---|
headings | true · false (headings for screen readers only) |
A section of your own
Add a section of your own to the switcher with zb:theme-switcher.section: a heading and a radio group (options), or your own controls in its slot. To tie it to one of the kit's appearance choices, pass setting; for a choice of your own, pass the Alpine expressions value and change.
<div x-data="{ density: 'comfortable' }">
<zb:theme-switcher as="inline" :sections="['mode']" :reset="false">
<zb:theme-switcher.section name="density" title="Density"
:options="['comfortable' => 'Comfortable', 'compact' => 'Compact']"
value="density" change="(value) => density = value" />
</zb:theme-switcher>
<p class="mt-3 text-sm text-muted">Chosen: <span class="text-ink" x-text="density"></span></p>
</div>
| Prop | Values |
|---|---|
name | The section's key |
title | Heading (required) |
options | value => label or value => [label, description, icon] |
setting | The kit's appearance choice (no Alpine needed) |
value · change | Alpine expressions for a choice of your own |
Developer notes
- Choices apply at once and are kept in the browser per scope; when the layout has a
preference-url, they are also saved to the account. - The 1.x tags
zb:theme-toggle,zb:brand-color-pickerandzb:theme-customizerare wrappers of this component until 3.0; writezb:theme-switcherin new code. - The button announces the new mode to screen readers. The panel layouts show this button in the top bar themselves (
theme-toggle). - To open the panel with a button of your own, set
triggertofalseand use<zb:drawer.trigger name="…">. The panel layouts add this panel themselves whencustomizeris on. - In the page, **Reset to defaults** clears only the choices of that page's scope.
- The switcher's own sections are
mode,colorandtheme;themeshows only when Zubazar UI Pro offers themes. Zubazar UI Pro addsskin,width,sidebar,navbarfor the panel andheader,footer,language,linksfor the store. - A section with nothing to offer is skipped;
modes,colorsandcustomnarrow the choices of their own sections. - A link, button or form with
data-switcher-keep-openthat reloads the page opens the panel again at once on the next page. - To add a package's section to every switcher, use
Zubazar\Ui\Support\Appearance::section($name, $view).