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

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

PRO

This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →

Basic usage

Basic usage: view
BLADE

Give the items with items: each has a label, and child items go in a children array.

blade
<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']]],
]" />
PropValues
itemsArray of ['label' => …, 'children' => […]]
aria-labelThe tree's name; recommended

Start expanded

Start expanded: view
BLADE

expanded opens every branch; for a single branch set open on the item, which always wins over expanded.

blade
<zb:tree expanded :items="[
    ['label' => 'Electronics', 'children' => [
        ['label' => 'Computers', 'children' => [['label' => 'Laptop computers'], ['label' => 'Desktop computers']]],
        ['label' => 'Accessories', 'open' => false, 'children' => [['label' => 'Headphones']]],
    ]],
]" />
PropValues
expandedtrue | false (default false)
openPer item: true | false

Selection and row actions

Selection and row actions: view
BLADE

selectable adds a checkbox to each row and actions adds action buttons at the end of the row.

blade
<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>
PropValues
selectabletrue (multiple) · single
actions['name' => ['label' => …, 'icon' => …, 'danger' => …]]
items[].keyThe key in the value and in the event

Props

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

PropTypeDefaultDescription
items—[]Array of items (with the keys below)
expandedbooleanfalseStarts all branches open
aria-label—nullThe tree's name for screen readers (attribute; aria-labelledby works too); recommended
selectablebooleanfalsetrue / multiple: a multiple selection with checkboxes (value: list of keys) · single: one item (value: a key or null)
cascadebooleanfalseMultiple selection: a branch is checked with its children; a partly checked branch is mixed
value—nullThe starting selection; with wire:model the model's, in a classic form old()
name—nullClassic 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—nullLivewire method called on a row action: (action, key)
sortablebooleanfalseRows move by dragging and with Alt+arrows; a grip on every row
move-handler—nullLivewire method called after a move: (key, parent key or null, position)
nestablebooleanfalseLeaves take children too when dropped on (default: branches only)
max-depth—nullThe deepest level a move may make (1: no nesting)
items[].label— — Displayed name (required)
items[].key—labelThe 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—expandedStarts this branch open or closed
items[].href—nullIf given, the name becomes a link
items[].badge—nullCounter on the right
items[].actionsbooleantrueThe 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

6 examples

Keyboard

Keyboard: view
BLADE

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.

blade
<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']]],
]" />
PropValues
↑ ↓ · Home · EndMove between items
→ ←Open or close a branch, move to a child or the parent
EnterFollows the link or opens and closes the branch
letterThe next item starting with that letter

Written in parts, with hidden branches

Written in parts, with hidden branches: view
BLADE

Instead of items you can write the branches with zb:tree.branch; child branches go in the slot. A branch hidden with x-show is skipped by the keyboard.

blade
<div x-data="{ hideEmpty: false }" class="space-y-3">
    <zb:checkbox x-model="hideEmpty" label="Hide empty categories" />
    <zb:tree aria-label="Categories" expanded>
        <zb:tree.branch label="Electronics" badge="12">
            <zb:tree.branch label="Phones" badge="12" />
            <zb:tree.branch label="Tablets" badge="0" x-show="! hideEmpty" />
        </zb:tree.branch>
        <zb:tree.branch label="Clothing" badge="0" x-show="! hideEmpty" />
    </zb:tree>
</div>
PropValues
zb:tree.branchlabel, href, badge, open; child branches in the slot
x-showHides the branch; a hidden branch is skipped by the keyboard

Checking a branch with its children

Checking a branch with its children: view
BLADE

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.

blade
<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']]],
    ]],
]" />
PropValues
cascadefalse (default) · true; multiple selection only

Single selection and a classic form

Single selection and a classic form: view
BLADE

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.

blade
<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>
PropValues
nameHidden fields: name[] (multiple) · name (single)
valueThe starting selection; with wire:model the model's

With Livewire

BLADE

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.

blade
<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>
PropValues
action-handlerLivewire method: (action, key)
tree.branch: keyThe branch's key; default: label
actions slotThe branch's own action buttons

Moving by dragging

Moving by dragging: view
BLADE

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).

blade
<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>
PropValues
sortableDrag and drop and Alt+arrows
move-handlerLivewire method: (key, parent, position)
max-depthThe deepest level
nestableLeaves take children too

Developer notes

Events, hooks and details
  • 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:key and x-show on zb:tree.branch go 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

EventdetailDescription
inputthe checked keysselectable: 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-checked value (true, false, mixed); with multiple selection the tree has aria-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.
Zubazar UI · ZubazarDesigned for people.