Tree view
Shows nested data such as categories and subcategories as branches that open and close, with selection, row actions and drag-and-drop.
zb:tree 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 items with items: each has a label, and child items go in a children array.
<zb:tree aria-label="Product categories" :items="[
['label' => 'Electronics', 'children' => [
['label' => 'Computers', 'children' => [['label' => 'Laptops'], ['label' => 'Desktops']]],
['label' => 'Phones', 'children' => [['label' => 'Smartphones']]],
]],
['label' => 'Clothing', 'children' => [['label' => 'Women']]],
]" />
| Prop | Values |
|---|---|
items | Array of ['label' => …, 'children' => […]] |
aria-label | The tree's name; recommended |
Start expanded
expanded opens every branch; for a single branch set open on the item, which always wins over expanded.
<zb:tree expanded :items="[
['label' => 'Electronics', 'children' => [
['label' => 'Computers', 'children' => [['label' => 'Laptop computers'], ['label' => 'Desktop computers']]],
['label' => 'Accessories', 'open' => false, 'children' => [['label' => 'Headphones']]],
]],
]" />
| Prop | Values |
|---|---|
expanded | true | false (default false) |
open | Per item: true | false |
Link and counter
href turns the item's name into a link and badge shows a small counter at the end of the row.
<zb:tree :items="[
['label' => 'Electronics', 'open' => true, 'badge' => 5, 'children' => [
['label' => 'Computers', 'href' => '#computers', 'badge' => 3],
['label' => 'Phones', 'href' => '#phones', 'badge' => 1],
['label' => 'Accessories', 'href' => '#accessories', 'badge' => 1],
]],
]" />
| Prop | Values |
|---|---|
href | Link URL |
badge | Counter on the right (a number or short text) |
Selection and row actions
selectable adds a checkbox to each row and actions adds action buttons at the end of the row.
<div x-data="{ selected: [4], last: '' }" x-on:zb:tree:action="last = $event.detail.action + ' · ' + $event.detail.key" class="space-y-3">
<zb:tree aria-label="Product categories" selectable x-model="selected" expanded :actions="[
'edit' => ['label' => 'Edit', 'icon' => 'pencil-square'],
'add' => ['label' => 'Add subcategory', 'icon' => 'plus'],
'delete' => ['label' => 'Delete', 'icon' => 'trash', 'danger' => true],
]" :items="[
['key' => 1, 'label' => 'Electronics', 'children' => [['key' => 2, 'label' => 'Phones'], ['key' => 3, 'label' => 'Tablets']]],
['key' => 4, 'label' => 'Home and living', 'children' => [['key' => 5, 'label' => 'Kitchen']]],
]" />
<p class="text-sm text-dim">Selected: <span x-text="JSON.stringify(selected)"></span> · Last action: <span x-text="last || '—'"></span></p>
</div>
| Prop | Values |
|---|---|
selectable | true (multiple) · single |
actions | ['name' => ['label' => …, 'icon' => …, 'danger' => …]] |
items[].key | The key in the value and in the event |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
items | — | [] | Array of items (with the keys below) |
expanded | boolean | false | Starts all branches open |
aria-label | — | null | The tree's name for screen readers (attribute; aria-labelledby works too); recommended |
selectable | boolean | false | true / multiple: a multiple selection with checkboxes (value: list of keys) · single: one item (value: a key or null) |
cascade | boolean | false | Multiple selection: a branch is checked with its children; a partly checked branch is mixed |
value | — | null | The starting selection; with wire:model the model's, in a classic form old() |
name | — | null | Classic form: the checked keys in hidden fields (name[] · single: name) |
actions | — | [] | Every row's actions: ['edit' => ['label' => 'Edit', 'icon' => 'pencil-square', 'danger' => false]]; 'edit' => 'Edit' for a text button |
action-handler | — | null | Livewire method called on a row action: (action, key) |
sortable | boolean | false | Rows move by dragging and with Alt+arrows; a grip on every row |
move-handler | — | null | Livewire method called after a move: (key, parent key or null, position) |
nestable | boolean | false | Leaves take children too when dropped on (default: branches only) |
max-depth | — | null | The deepest level a move may make (1: no nesting) |
items[].label | — | — | Displayed name (required) |
items[].key | — | label | The key of the selection and the actions (a number or a string, its type is kept) |
items[].children | — | [] | Child items, with the same structure |
items[].open | — | expanded | Starts this branch open or closed |
items[].href | — | null | If given, the name becomes a link |
items[].badge | — | null | Counter on the right |
items[].actions | boolean | true | The actions shown on this row (['edit']); false for none |
tree.branch: label · href · badge · open · key | — | — | One branch when written in parts instead of items; child branches go in the slot, action buttons in the actions slot. Attributes such as class, wire:key and x-show go on the branch |
Advanced
Keyboard
The tree is a single Tab stop; the arrows, Home and End move between items, and Enter opens the link or toggles the branch. Typing a letter moves to the next item that starts with it.
<zb:tree aria-label="Warehouse sections" expanded :items="[
['label' => 'Main warehouse', 'children' => [['label' => 'Shelf A'], ['label' => 'Shelf B']]],
['label' => 'Store stockroom', 'children' => [['label' => 'Behind the till']]],
]" />
| Prop | Values |
|---|---|
↑ ↓ · Home · End | Move between items |
→ ← | Open or close a branch, move to a child or the parent |
Enter | Follows the link or opens and closes the branch |
letter | The next item starting with that letter |
Checking a branch with its children
With cascade, selecting a branch selects everything under it; a partly selected branch shows a half mark and is not in the value. The default is false: the value is exactly the clicked items.
<zb:tree aria-label="Campaign scope" selectable cascade expanded :value="[3]" :items="[
['key' => 1, 'label' => 'Computers', 'children' => [
['key' => 2, 'label' => 'Laptops'],
['key' => 3, 'label' => 'Accessories', 'children' => [['key' => 4, 'label' => 'Keyboards'], ['key' => 5, 'label' => 'Mice']]],
]],
]" />
| Prop | Values |
|---|---|
cascade | false (default) · true; multiple selection only |
Single selection and a classic form
selectable="single" allows one item; the value is the key itself or null. With name the selection is sent in hidden fields and comes back from old() after a validation error.
<form class="space-y-3">
<zb:tree aria-label="Parent category" selectable="single" name="parent_category" :value="2" expanded :items="[
['key' => 1, 'label' => 'Fashion', 'children' => [['key' => 2, 'label' => 'Women'], ['key' => 3, 'label' => 'Men']]],
['key' => 4, 'label' => 'Sports'],
]" />
</form>
| Prop | Values |
|---|---|
name | Hidden fields: name[] (multiple) · name (single) |
value | The starting selection; with wire:model the model's |
With Livewire
wire:model binds the selection and action-handler calls a Livewire method with (action, key) on a row action. When you write the branches yourself, put the buttons in the branch's actions slot and use wire:click directly.
<zb:tree aria-label="Categories" selectable wire:model="selectedCategories" action-handler="categoryAction"
:actions="['edit' => ['label' => 'Edit', 'icon' => 'pencil-square'], 'delete' => ['label' => 'Delete', 'icon' => 'trash', 'danger' => true]]"
:items="$categories" />
{{-- In the component: public function categoryAction(string $action, int $key): void { … } --}}
<zb:tree aria-label="Categories" selectable wire:model.live="selectedCategories">
@foreach ($categories as $category)
<zb:tree.branch :key="$category->id" :label="$category->name" wire:key="category-{{ $category->id }}">
<x-slot:actions>
<zb:button size="sm" variant="ghost" wire:click="edit({{ $category->id }})">Edit</zb:button>
</x-slot:actions>
</zb:tree.branch>
@endforeach
</zb:tree>
| Prop | Values |
|---|---|
action-handler | Livewire method: (action, key) |
tree.branch: key | The branch's key; default: label |
actions slot | The branch's own action buttons |
Moving by dragging
sortable lets users move rows by dragging or with Alt+arrows; max-depth limits the depth. After a move the tree sends zb:tree:moved and move-handler calls a Livewire method with (key, parent, position).
<div x-data="{ last: '' }" x-on:zb:tree:moved="last = $event.detail.key + ' → ' + ($event.detail.parent ?? 'top level') + ', position ' + ($event.detail.position + 1)" class="space-y-3">
<zb:tree aria-label="Menu categories" sortable :max-depth="3" expanded :items="[
['key' => 'electronics', 'label' => 'Electronics', 'children' => [['key' => 'phones', 'label' => 'Phones'], ['key' => 'tablets', 'label' => 'Tablets']]],
['key' => 'home', 'label' => 'Home and living', 'children' => [['key' => 'kitchen', 'label' => 'Kitchen']]],
['key' => 'books', 'label' => 'Books'],
]" />
<p class="text-sm text-dim">Last move: <span x-text="last || '—'"></span></p>
</div>
| Prop | Values |
|---|---|
sortable | Drag and drop and Alt+arrows |
move-handler | Livewire method: (key, parent, position) |
max-depth | The deepest level |
nestable | Leaves take children too |
Developer notes
- Branches start closed; there is no depth limit.
- The structure is marked with
role="tree"; screen readers read each item's name, level and whether it is open. - When Livewire updates the page, the user's selection, the focus and the Tab stop stay on the same item; the server changes the selection through the model's value.
- Attributes such as
class,wire:keyandx-showonzb:tree.branchgo to the branch itself. - Row buttons show on hover, on the focused row and always on touch screens.
- While dragging, the top third of a row drops before it, the bottom third after it and the middle inside it; hovering over a closed branch opens it and Escape cancels.
- An item cannot be moved into itself or its children; a move the server rejects is undone on the next render.
Events
| Event | detail | Description |
|---|---|---|
input | the checked keys | selectable: sent from the root when the user changes the selection (detail: the list of keys; with a single selection the key or null); wire:model and x-model read it. A value the model writes sends no event. |
change | — | selectable: after every change of the user's; bubbles. |
zb:tree:action | { action, key } | Sent from the tree, bubbling, when a row action (actions, or a button with data-tree-action) is pressed; with action-handler the Livewire method is called with the same values. |
zb:tree:moved | { key, parent, position } | sortable: sent from the tree, bubbling, when an item moves; parent is the new parent's key (null at the top level), position its place among its siblings (from 0). With move-handler the Livewire method is called with the same values. |
Accessibility
- Keyboard
- The tree is one Tab stop; the arrows, Home, End, Enter and type-ahead follow the WAI-ARIA tree pattern. With
selectable, Space checks the focused item. - Selection
- The selection is the item's
aria-checkedvalue (true, false, mixed); with multiple selection the tree hasaria-multiselectable="true". The visible box is hidden from screen readers; the item states its own state. - Row actions
- The buttons are not part of the item's name; each has its own name (
label) and is described by the row's name. The focused row's buttons come after the row with Tab, the other rows' are out of the Tab order; Shift+Tab or Escape goes back to the row. - Moving
- On the keyboard Alt+↑ / Alt+↓ move the item among its siblings, Alt+→ into the item above and Alt+← after its parent. The new place is read from a polite live region and the moved item keeps the focus; the handle is hidden from screen readers.