v0.0.2
Demos (demo panel, opens in a new tab)

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

Button: view
BLADE

The default form is the moon/sun button of the top bar; clicking switches to the opposite of the mode on screen.

blade
<zb:theme-switcher />

Panel

Panel: view
BLADE

as="drawer" shows the sections in a panel that opens from the right, with **Reset to defaults** and **Done** buttons.

blade
<zb:theme-switcher as="drawer" trigger="button" />
PropValues
asbutton · drawer · inline
triggeredge · button · false
nameThe panel's name (for zb:drawer.trigger)
title · description · labelThe panel's title, the line below it, the accessible name of the button that opens it

In the page

In the page: view
BLADE

as="inline" draws the sections where it stands; good for a settings page.

blade
<zb:theme-switcher as="inline" title="Appearance" />
PropValues
resettrue · false
reset-urlA link that also returns a server-side choice (e.g. the store theme) to its default

Sections

Sections: view
BLADE

sections chooses the sections to show and their order: an array or "mode,color".

blade
<zb:theme-switcher as="inline" :sections="['color', 'mode']" :modes="['light', 'dark']" :custom="false" :reset="false" />
PropValues
sectionsmode · color · theme · skin · width · sidebar · navbar · header · footer · language · links
modes['light', 'dark', 'system'] · false
colorstrue · false · key => [#rrggbb, name]
customtrue · false

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
as—buttonbutton (the top bar button) · drawer (a panel from the right) · inline (in the page)
sections—nullThe 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
colorsbooleantrueThe presets: true (Appearance::presets()), false, or key => [#rrggbb, name]
custombooleantrueThe free color picker (#rrggbb)
themes—nullThe themes on offer: null (what Zubazar UI Pro offers on this request; none in the free package), false, or items (key, name, url…)
title—nulldrawer: the panel's title (when null, "Appearance"); inline: a heading above the sections
description—nullThe line below the title (in the drawer, when null, "Changes apply right away…"); false hides it
label—nulldrawer: the accessible name of the button that opens it (when null, "Appearance settings")
name—theme-switcherdrawer: the panel's name (zb:drawer.trigger, ZubazarUI.drawer.open())
trigger—edgedrawer: edge (a gear on the right edge) · button (an icon button in place) · false (a button of your own)
resetbooleantrueReset to defaults: clears the scope's saved choices
reset-url—nullWith reset: a link that also returns a server-side choice to its default
headingsbooleantruefalse: section headings for screen readers only
section: name—nullThe section's key (data-switcher-section)
section: title— — Heading (required); the group's accessible name
section: description—nullA line below the heading
section: options—[]value => label or [label, description, icon]; as cards when one has a description
section: setting—nullThe appearance choice it is tied to: mode, brandColor, skin, contentWidth, sidebar, navbarMode (no Alpine needed)
section: value · change—nullA 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

2 examples

Only the color

Only the color: view
BLADE

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.

blade
<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>
PropValues
headingstrue · false (headings for screen readers only)

A section of your own

A section of your own: view
BLADE

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.

blade
<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>
PropValues
nameThe section's key
titleHeading (required)
optionsvalue => label or value => [label, description, icon]
settingThe kit's appearance choice (no Alpine needed)
value · changeAlpine expressions for a choice of your own

Developer notes

Events, hooks and details
  • 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-picker and zb:theme-customizer are wrappers of this component until 3.0; write zb:theme-switcher in 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 trigger to false and use <zb:drawer.trigger name="…">. The panel layouts add this panel themselves when customizer is on.
  • In the page, **Reset to defaults** clears only the choices of that page's scope.
  • The switcher's own sections are mode, color and theme; theme shows only when Zubazar UI Pro offers themes. Zubazar UI Pro adds skin, width, sidebar, navbar for the panel and header, footer, language, links for the store.
  • A section with nothing to offer is skipped; modes, colors and custom narrow the choices of their own sections.
  • A link, button or form with data-switcher-keep-open that 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).
Zubazar UI · ZubazarDesigned for people.