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
Put it inside <head>; add your assets such as the favicon and @vite to the slot.
<!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>
| Prop | Values |
|---|---|
title | Tab title; defaults to config('app.name') |
Saved preferences
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.
<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>
| Prop | Values |
|---|---|
mode | light | dark | system |
brand-color | default | green | red | yellow | blue | gray | dark | #rrggbb |
sidebar-mode | expanded | collapsed |
navbar-mode | fixed | static | hidden |
skin | default | bordered |
content-width | default | wide | wider | full | 1520px |
semi-dark | true | false |
Saving preferences to the account
With preference-url, the new value is POSTed to this URL as JSON as soon as the user changes the look.
// 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();
}
| Prop | Values |
|---|---|
preference-url | POST URL; when null, the preferences are stored only in the browser |
Using the stores
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.
<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>
| Prop | Values |
|---|---|
$store.appearance | value (the mode) · set(light | dark | system) · toggle() · scope |
$store.brand | color · set(name | #rrggbb) · forget() |
$store.sidebar | collapsed · toggle() · setCollapsed(true | false) |
$store.navbar | mode · set(fixed | static | hidden) |
$store.customizer | skin · semiDark · setSkin() · setSemiDark() · reset() |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
title | — | null | Tab title; defaults to config('app.name') |
mode | — | null | light · dark · system |
theme | — | null | A color theme's key (Appearance::define); when null, the current theme |
scope | — | null | The scope the choices are kept in; when null, Appearance::scope(), config('zubazar-ui.layout.scope'), else app |
brand-color | — | null | default · green · red · yellow · blue · gray · dark · #rrggbb |
sidebar-mode | — | null | expanded · collapsed |
navbar-mode | — | null | fixed · static · hidden; when null, the preference in the browser, or fixed if there is none |
skin | — | null | default · bordered |
content-width | — | null | default (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 | — | null | The 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 | — | null | true · false; when null, the preference in the browser |
preference-url | — | null | URL 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
- 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-tokenmeta tags, the<title>tag and the@livewireStylesoutput itself;@livewireScriptsstays with you at the end of<body>. - Give
<body>the page background withbg-page. - Because the boot script (
zubazar-ui-boot.js) runs before the page is painted, there is no flash onwire:navigatetransitions 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 (
fixedfornavbar-mode).nullmeans "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-widthfor a length of your own) anddata-sidebar-collapsed. Until 3.0 thedarkclass is written too. - The choices are kept under the
scope, in thelocalStorage['zubazar-ui:{scope}']key. - The preference request carries the
X-CSRF-TOKENheader and contains only the key that changed:mode,brand,sidebar,navbar,skin,contentorsemiDark. - 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.