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

Head contents

A component for the <head> of your own layout that writes the meta tags and applies the appearance preferences before the first paint.

zb:layout.head zubazar/ui · free (MIT)

In your own layout

BLADE

Put it inside <head>; add your assets such as the favicon and @vite to the slot.

blade
<!DOCTYPE html>
<html lang="en">
<head>
    <zb:layout.head :title="$title ?? null">@vite(['resources/css/app.css', 'resources/js/app.js'])</zb:layout.head>
</head>
<body class="bg-page">
    {{ $slot }}
    @livewireScripts
</body>
</html>
PropValues
titleTab title; defaults to config('app.name')

Saved preferences

BLADE

For each preference, the value given by the server comes first; if it is null, the value stored in the browser, then the config/zubazar-ui.php default.

blade
<zb:layout.head
    :mode="auth()->user()?->theme_mode"
    :brand-color="auth()->user()?->brand_color"
    :sidebar-mode="auth()->user()?->sidebar_mode"
    :navbar-mode="auth()->user()?->navbar_mode"
    :skin="auth()->user()?->skin"
    :content-width="auth()->user()?->content_width"
    :semi-dark="auth()->user()?->semi_dark"
    :preference-url="route('ui-preference.update')"
>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
</zb:layout.head>
PropValues
modelight | dark | system
brand-colordefault | green | red | yellow | blue | gray | dark | #rrggbb
sidebar-modeexpanded | collapsed
navbar-modefixed | static | hidden
skindefault | bordered
content-widthdefault | wide | wider | full | 1520px
semi-darktrue | false

Saving preferences to the account

PHP

With preference-url, the new value is POSTed to this URL as JSON as soon as the user changes the look.

php
// routes/web.php
Route::post('ui-preference', UiPreferenceController::class)
    ->middleware('auth')
    ->name('ui-preference.update');

// app/Http/Controllers/UiPreferenceController.php
public function __invoke(Request $request): Response
{
    $data = $request->validate([
        'mode' => ['sometimes', 'in:light,dark,system'],
        'brand' => ['sometimes', 'regex:/^(#[0-9a-fA-F]{6}|default|green|red|yellow|blue|gray|dark)\z/'],
        'sidebar' => ['sometimes', 'in:expanded,collapsed'],
        'navbar' => ['sometimes', 'in:fixed,static,hidden'],
        'skin' => ['sometimes', 'in:default,bordered'],
        'content' => ['sometimes', 'in:default,wide,wider,full'],
        'semiDark' => ['sometimes', 'boolean'],
    ]);

    $columns = [
        'mode' => 'theme_mode', 'brand' => 'brand_color', 'sidebar' => 'sidebar_mode', 'navbar' => 'navbar_mode',
        'skin' => 'skin', 'content' => 'content_width', 'semiDark' => 'semi_dark',
    ];

    $request->user()->update(
        collect($data)->mapWithKeys(fn ($value, $key) => [$columns[$key] => $value])->all()
    );

    return response()->noContent();
}
PropValues
preference-urlPOST URL; when null, the preferences are stored only in the browser

Using the stores

Using the stores: view
BLADE

Change the look from your own code with $store; set and toggle apply the value at once, store it and, with preference-url, save it to the account.

blade
<div x-data class="flex flex-wrap items-center gap-2">
    <zb:button variant="white" x-on:click="$store.appearance.toggle()">Change mode</zb:button>
    <zb:button variant="white" x-on:click="$store.brand.set('green')">Green primary color</zb:button>
    <zb:button variant="white" x-on:click="$store.brand.set('default')">Default color</zb:button>
    <span class="text-sm text-muted">
        Mode: <b class="text-ink" x-text="$store.appearance.value"></b> · Color: <b class="text-ink" x-text="$store.brand.color"></b>
    </span>
</div>
PropValues
$store.appearancevalue (the mode) · set(light | dark | system) · toggle() · scope
$store.brandcolor · set(name | #rrggbb) · forget()
$store.sidebarcollapsed · toggle() · setCollapsed(true | false)
$store.navbarmode · set(fixed | static | hidden)
$store.customizerskin · semiDark · setSkin() · setSemiDark() · reset()

Props

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

PropTypeDefaultDescription
title—nullTab title; defaults to config('app.name')
mode—nulllight · dark · system
theme—nullA color theme's key (Appearance::define); when null, the current theme
scope—nullThe scope the choices are kept in; when null, Appearance::scope(), config('zubazar-ui.layout.scope'), else app
brand-color—nulldefault · green · red · yellow · blue · gray · dark · #rrggbb
sidebar-mode—nullexpanded · collapsed
navbar-mode—nullfixed · static · hidden; when null, the preference in the browser, or fixed if there is none
skin—nulldefault · bordered
content-width—nulldefault (1280px) · wide (1440px) · wider (1600px) · full · a CSS length (1520px); boxed is wide. The top bar, the page and the footer share one container; null: the browser's choice, then config, then full
default-content-width—nullThe width when the user has none of their own, in place of config (a package's default: the store's commerce.content_width)
semi-dark—nulltrue · false; when null, the preference in the browser
preference-url—nullURL that the preferences are POSTed to; when null, they are stored only in the browser

Slots: default (the application's head assets: @vite, favicon, fonts)

Developer notes

Events, hooks and details
  • The ready-made layouts add this component themselves; you only need it when you write your own layout.
  • The component writes the charset, viewport and csrf-token meta tags, the <title> tag and the @livewireStyles output itself; @livewireScripts stays with you at the end of <body>.
  • Give <body> the page background with bg-page.
  • Because the boot script (zubazar-ui-boot.js) runs before the page is painted, there is no flash on wire:navigate transitions either.
  • The Alpine stores (appearance, brand, sidebar, navbar, customizer) come from the Zubazar UI JavaScript: import '@zubazar/ui' or @zubazarUiScripts.
  • Failing all of them, the package default applies (fixed for navbar-mode). null means "not saved"; do not pass a separate default for users without a saved value.
  • A single page can force its own value with Zubazar\Ui\Support\Layout::page().
  • The values are written to the <html> tag in the first frame: data-theme, the --zb-user-* variables for the user's color, data-skin, data-content-width (--zb-container-width for a length of your own) and data-sidebar-collapsed. Until 3.0 the dark class is written too.
  • The choices are kept under the scope, in the localStorage['zubazar-ui:{scope}'] key.
  • The preference request carries the X-CSRF-TOKEN header and contains only the key that changed: mode, brand, sidebar, navbar, skin, content or semiDark.
  • The request runs in the background; even if it fails, the look is kept for that session.
  • The stores are registered when Alpine starts. The buttons in the example work inside the example's own frame; since the primary color choice is stored in the browser, undo it with "Default color" when you are done.
Zubazar UI · ZubazarDesigned for people.