JSON menu
Defines the application menu in a single JSON file; the vertical menu, the horizontal menu, the mobile drawer and the command palette search are drawn from that definition.
zb:menu zubazar/ui-pro
This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →
Basic usage
Give the menu prop a file name, a full path, an array or a Menu object; the example uses an array, in an application use a JSON file.
<nav class="flex flex-col gap-0.5">
<zb:menu :menu="[
['section' => 'General'],
['title' => 'Orders', 'icon' => 'shopping-bag', 'url' => '#orders'],
['title' => 'Reports', 'icon' => 'chart-bar', 'children' => [
['title' => 'Sales report', 'url' => '#sales'],
['title' => 'Customer report', 'url' => '#customers'],
]],
]" />
</nav>
| Prop | Values |
|---|---|
menu | File name | full path | array | Menu object |
JSON file
The file lives under resources/menu/; each item is a section heading (section) or a link or group with a title.
{
"menu": [
{ "section": "General" },
{ "title": "Home", "icon": "home", "route": "home" },
{ "title": "Orders", "icon": "shopping-bag", "route": "orders.index", "active": "orders.*" },
{ "title": "Reports", "icon": "chart-bar", "badge": true, "children": [
{ "title": "Sales", "route": "reports.sales" },
{ "title": "Archive", "children": [
{ "title": "2025", "url": "/reports/archive/2025" }
] }
] },
{ "section": "Support" },
{ "title": "Help center", "icon": "lifebuoy", "url": "https://destek.ornek.com", "target": "_blank" }
]
}
| Prop | Values |
|---|---|
section | Section heading; hidden when no items remain under it |
title | Display name |
icon | zb:icon name |
Links
Give a route name with route and its parameters with params, or a relative path or full address with url.
{
"menu": [
{ "title": "Home", "icon": "home", "route": "home" },
{ "title": "Customer card", "icon": "identification", "route": "customers.show", "params": { "customer": 42 } },
{ "title": "Archive", "icon": "archive-box", "url": "/reports/archive" },
{ "title": "Support line", "icon": "phone", "url": "tel:+908500000000" },
{ "title": "Help center", "icon": "lifebuoy", "url": "https://destek.ornek.com", "target": "_blank" }
]
}
| Prop | Values |
|---|---|
route | Route name |
params | Route parameters |
url | Relative path | http(s) | mailto | tel |
target | _blank |
Using it in a layout
Put the menu in the layout's nav slot; horizontal draws a horizontal top menu, and searchItems() gives the same links to the command palette.
{{-- Vertical layout: menu and search from the same file --}}
<zb:layout.sidebar>
<zb:slot:nav><zb:menu menu="sidebar" /></zb:slot:nav>
<zb:slot:search>
<zb:command-palette :items="\Zubazar\Panel\Menu::fromFile('sidebar')->searchItems()" />
</zb:slot:search>
…
</zb:layout.sidebar>
{{-- Horizontal layout: horizontal menu on top, vertical menu in the mobile drawer --}}
<zb:layout.horizontal>
<zb:slot:nav><zb:menu menu="sidebar" horizontal /></zb:slot:nav>
<zb:slot:mobileNav><zb:menu menu="sidebar" /></zb:slot:mobileNav>
…
</zb:layout.horizontal>
| Prop | Values |
|---|---|
horizontal | true | false |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
menu | — | — | Required; a file name (resources/menu/…json), a full path, an array or a Menu object |
horizontal | boolean | false | Horizontal (top) menu; section headings are not shown |
sections | — | auto | In the horizontal menu, true turns each section into a single dropdown (its groups open to the side); false lays the items out side by side; auto uses sections when there are more than 6 items |
section | — | — | JSON: section heading; top level only |
title | — | — | JSON: display name |
icon | — | null | JSON: zb:icon name; shown on the top level |
route | — | null | JSON: route name; if it is not defined, the link is # |
params | — | [] | JSON: route parameters |
url | — | null | JSON: a relative path or an http, https, mailto or tel address; anything else becomes # (//host only with the application's own host) |
active | — | null | JSON: a route name or a /path pattern (can be an array, * is a wildcard) |
badge | — | null | JSON: a number or text; on a group, true = number of child items |
target | — | null | JSON: _blank = new tab, wire:navigate is not used |
children | — | — | JSON: child items; up to 3 levels |
Advanced
Active page
Without active, a route item is active while that route is open and a url item when the address matches exactly. To keep an item active on subpages too, give active route names or path patterns starting with / (* is a wildcard).
{
"menu": [
{ "title": "Orders", "icon": "shopping-bag", "route": "orders.index", "active": "orders.*" },
{ "title": "Reports", "icon": "chart-bar", "url": "/reports", "active": ["/reports", "/reports/*"] }
]
}
| Prop | Values |
|---|---|
active | Route name | /path pattern | an array of these |
Building it in PHP
Build the menu in PHP with the Zubazar\Panel\Menu class, for example from an array that comes from a database. transform changes every item before it is rendered; isActive decides whether an item is active, and when it returns null the default rule applies.
use Zubazar\Panel\Menu;
$menu = new Menu(
$definition,
// Every item passes through here before it is rendered: turn your own field into a link.
transform: fn (array $item) => isset($item['module'])
? ['url' => '/modules/'.$item['module']] + $item
: $item,
// true / false: whether the item is active; null: leave it to the default rule.
isActive: fn (array $item) => isset($item['module']) ? $item['module'] === $activeModule : null,
);
// Blade: <zb:menu :menu="$menu" />
// Command palette: $menu->searchItems()
| Prop | Values |
|---|---|
transform | fn (array $item): array |
isActive | fn (array $item): ?bool |
Developer notes
- The layouts'
navslot already wraps the menu; when you use it on its own, put it inside a<nav>. - The menu does no authorization checks; the application decides which items are shown.
- Because menu data can also come from a database, only http, https, mailto and tel addresses and relative paths are accepted; anything else (e.g. javascript:) becomes
#. - A scheme-less
//host/pathaddress is kept only when it points to the application's own host; otherwise it becomes#. - External addresses are never considered active; a group that contains the active item is rendered open.
- The
childrenfield of third-level items is ignored; badges are shown at every level in both the vertical and the horizontal menu. - In the horizontal menu, section headings are skipped and groups become dropdown panels; for the mobile drawer, pass the same file to the
mobileNavslot as a vertical menu. - The old class name
Zubazar\Ui\Menu\Menukeeps working until 3.0.