Modal
A window for short forms and details that opens on top of the page and dims what is behind it.
zb:modal zubazar/ui · free (MIT)
Basic usage
Give the modal a unique name and open it with zb:modal.trigger; X, Esc and a backdrop click close it.
<zb:modal.trigger name="catalog-customer"><zb:button>Open the customer summary</zb:button></zb:modal.trigger>
<zb:modal name="catalog-customer" title="Customer summary">
<dl class="space-y-3 text-sm">
<div class="flex justify-between"><dt class="text-muted">Customer</dt><dd>Ayşe Yılmaz</dd></div>
<div class="flex justify-between"><dt class="text-muted">Membership</dt><dd>Premium</dd></div>
<div class="flex justify-between"><dt class="text-muted">Last order</dt><dd>September 12, 2026</dd></div>
</dl>
</zb:modal>
| Prop | Values |
|---|---|
name | Unique name; events carry this name |
title | Header line (with the X button) |
Widths
Pick the maximum width with max-width: sm for a short message, xl or 2xl for a two-column form or a table, 3xl–5xl or full for a long text (terms, a contract), where only the body scrolls and the header and the footer stay in place.
<div class="flex flex-wrap gap-2" x-data>
<zb:modal.trigger name="catalog-width-sm"><zb:button variant="white">sm</zb:button></zb:modal.trigger>
<zb:modal.trigger name="catalog-width-lg"><zb:button variant="white">lg</zb:button></zb:modal.trigger>
<zb:modal.trigger name="catalog-width-2xl"><zb:button variant="white">2xl</zb:button></zb:modal.trigger>
</div>
<zb:modal name="catalog-width-sm" title="Narrow window" max-width="sm"><p class="text-sm">For a short confirmation or message.</p></zb:modal>
<zb:modal name="catalog-width-lg" title="Wide window" max-width="lg"><p class="text-sm">For single-column forms.</p></zb:modal>
<zb:modal name="catalog-width-2xl" title="Widest window" max-width="2xl"><p class="text-sm">For two-column forms and small tables.</p></zb:modal>
| Prop | Values |
|---|---|
max-width | sm | md | lg | xl | 2xl | 3xl | 4xl | 5xl | full |
With Livewire
Send the zb:modal:open and zb:modal:close events from the server; close-action calls a Livewire method when the user closes the window.
// <zb:modal name="product-form" title="Product" close-action="resetForm"> … </zb:modal>
public function edit(int $productId): void
{
$this->product = Product::findOrFail($productId);
$this->dispatch('zb:modal:open', name: 'product-form');
}
public function save(): void
{
$this->product->save();
$this->dispatch('zb:modal:close', name: 'product-form');
}
public function resetForm(): void
{
$this->reset('product');
}
| Prop | Values |
|---|---|
close-action | Livewire method called when the user closes the window |
Composable parts
Build the window from the zb:modal.header, zb:modal.body and zb:modal.footer parts; a button inside zb:modal.close closes it.
<zb:modal.trigger name="add-task">
<zb:button>New task</zb:button>
</zb:modal.trigger>
<zb:modal name="add-task" max-width="lg">
<zb:modal.header title="New task" description="Choose the branch and the assignee." />
<zb:modal.body class="space-y-4">
<zb:input name="task_name" label="Task name" placeholder="Kadıköy – stock count" />
<zb:select name="branch" label="Branch" :options="['kadikoy' => 'Kadıköy', 'uskudar' => 'Üsküdar', 'atasehir' => 'Ataşehir']" placeholder="Choose a branch" />
<zb:select name="assignee" label="Assignee" :options="['ahmet' => 'Ahmet Yılmaz', 'elif' => 'Elif Kaya']" placeholder="Choose a team member" />
</zb:modal.body>
<zb:modal.footer>
<zb:modal.close>
<zb:button variant="white">Cancel</zb:button>
</zb:modal.close>
<zb:modal.close>
<zb:button x-on:click="$dispatch('toast', { type: 'success', message: 'Task saved.' })">Save</zb:button>
</zb:modal.close>
</zb:modal.footer>
</zb:modal>
| Prop | Values |
|---|---|
zb:modal.trigger | name (the modal to open) |
zb:modal.header | title, description; slot: header content |
zb:modal.body · footer | Body and footer |
zb:modal.close | The element inside closes the modal |
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
name | — | — | Required. The name used in the events and the ZubazarUI.modal.open('…') / close('…') calls; closing without a name closes all modals |
title | — | null | Header line and X button; not rendered when empty |
max-width | — | md | sm · md · lg · xl · 2xl · 3xl · 4xl · 5xl · full; another value is an error |
aria-label | — | null | Accessible name when there is no title; when empty, "Dialog" (in the app language) |
closable-on-backdrop | boolean | true | Closes on a backdrop click |
close-action | — | null | Livewire method called when the user closes the window |
request-close-event | — | null | X / Esc / backdrop emit this event instead of closing |
alert | boolean | false | A question that needs an answer: role="alertdialog" (confirmation-dialog uses it) |
wire:model / x-model | — | — | Binds the open state both ways: it opens when the property is true and sets it to false when the user closes it (confirmation-dialog takes it too) |
modal.trigger: name | — | — | Required; opens the modal with this name on click (confirmation-dialog included) |
modal.header: title · description | — | null | Title, description and X; when title is not given, the slot is the title itself |
modal.body · modal.footer | — | — | Scrolling body and right-aligned footer |
modal.close | — | — | Clicking the element inside closes the modal |
Slots: default: the window body (scrolls), the parts (zb:modal.header, zb:modal.body, zb:modal.footer) or a Livewire component that draws the parts · footer: right-aligned footer · zb:modal.trigger and zb:modal.close open and close it.
Advanced
Window without a title
Without title, no header line or X is drawn and the content uses the whole window. Give an aria-label to name the window (when empty, "Dialog" is read, in the app language).
<zb:modal.trigger name="catalog-done"><zb:button variant="white">Finish the import</zb:button></zb:modal.trigger>
<zb:modal name="catalog-done" max-width="sm" aria-label="Import complete">
<div class="flex flex-col items-center gap-3 py-4 text-center">
<zb:icon name="check-circle" size="12" stroke="1.4" class="text-success-line" />
<p class="text-base font-semibold text-ink">Import complete</p>
<p class="text-sm text-muted">148 products were added to the Electronics category.</p>
<zb:modal.close><zb:button class="mt-2">Done</zb:button></zb:modal.close>
</div>
</zb:modal>
| Prop | Values |
|---|---|
aria-label | Accessible name of the window when there is no title |
Do not close on outside click
So a long form is not lost, turn off backdrop clicks with :closable-on-backdrop="false"; X and Esc still work. Pass the value with a colon, otherwise "false" arrives as text and the feature stays on.
<zb:modal.trigger name="catalog-sticky"><zb:button variant="white">Open the ticket form</zb:button></zb:modal.trigger>
<zb:modal name="catalog-sticky" title="Support ticket" :closable-on-backdrop="false">
<zb:textarea name="ticket_note" label="Ticket note" rows="4" />
<zb:slot:footer>
<zb:modal.close><zb:button variant="white">Close</zb:button></zb:modal.close>
</zb:slot:footer>
</zb:modal>
| Prop | Values |
|---|---|
closable-on-backdrop | true | false |
Asking before closing
With request-close-event, X, Esc and the backdrop do not close the window but emit an event with this name; the listening code decides and closes it with close() or ZubazarUI.modal.close('catalog-note'). close() in the footer skips this check.
<zb:modal.trigger name="catalog-note"><zb:button variant="white">Add a note</zb:button></zb:modal.trigger>
<zb:modal
name="catalog-note"
title="Add a note"
request-close-event="catalog-note-close-requested"
x-on:catalog-note-close-requested.window="if (confirm('The note you wrote will not be saved. Close anyway?')) close()"
>
<zb:textarea name="customer_note" label="Note" rows="3" />
</zb:modal>
| Prop | Values |
|---|---|
request-close-event | Name of the event emitted on a close request |
Parts drawn by a Livewire component
Start the component's root element with zb:modal.header or zb:modal.body; the parts fill the window and the header names it. If the component draws its own header, don't give the modal a title, or two headers show.
<zb:modal name="customer-form">
<livewire:customer-form />
</zb:modal>
{{-- resources/views/livewire/customer-form.blade.php --}}
<div>
<zb:modal.header title="New customer" />
<zb:modal.body>…</zb:modal.body>
<zb:modal.footer>…</zb:modal.footer>
</div>
Close button in a classic modal
zb:modal.close also works in the classic usage with title and the footer slot.
<zb:modal.trigger name="delivery-note">
<zb:button variant="white">Delivery note</zb:button>
</zb:modal.trigger>
<zb:modal name="delivery-note" title="Delivery note">
<p class="text-sm text-dim">If the customer is not at the address, leave the package with the building attendant and take a photo.</p>
<zb:slot:footer>
<zb:modal.close><zb:button variant="white">Close</zb:button></zb:modal.close>
</zb:slot:footer>
</zb:modal>
Developer notes
- Only one modal stays open at a time.
- A modal also opens with the
zb:modal:openevent or, in JavaScript,ZubazarUI.modal.open('name'); inside the slots,close()closes it. - On narrow screens the window is always full width.
- An
zb:modal:closesent from the server does not call theclose-actionmethod again. The old event namesopen-modal,close-modalandmodal-closedstill work. - With the parts, the content must start with a part; to wrap the body and footer in a
form, put the form after the header. zb:modal.headermust be inside anzb:modal: outside one the page fails with an error, and in a Livewire component clicking X orzb:modal.closelogs an error to the console.- If a Livewire component draws only the body and the footer, write the header in the modal with
zb:modal.header, before the component.
Events
| Event | detail | Description |
|---|---|---|
zb:modal:opened | { name } | Sent from the modal root when it opens; bubbles to window. |
zb:modal:closed | { name } | Sent from the modal root on every close (X, Escape, backdrop, close(), a server zb:modal:close). The legacy name modal-closed is sent at the same time. |
zb:modal:open (listens) | { name } | Dispatched on window, it opens the modal with that name. The legacy name open-modal also works. |
zb:modal:close (listens) | { name } | Closes the modal with that name, or every open modal when no name is given. The legacy name close-modal also works. |
Accessibility
- ARIA and roles
- The panel has
role="dialog"andaria-modal="true"; a confirmation (zb:confirmation-dialog) usesrole="alertdialog". The title (titleorzb:modal.header) names the dialog througharia-labelledby, and the headerdescriptiondescribes it througharia-describedby. Without a title the name comes fromaria-label, or falls back to "Dialog". - Keyboard
- Escape closes the modal, or sends the
request-close-eventinstead when one is set. Tab and Shift+Tab cycle inside the modal. An open menu or list inside takes the first Escape; the modal closes on the next one. - Focus
- On open, focus moves to an element with
autofocus, else to the first focusable element (usually the X in the title row), else to the panel itself. On close it returns to the element that opened the modal. The page does not scroll while the modal is open. - Buttons
- The X button is named "Close".
zb:modal.triggeraddsaria-haspopup="dialog"to the button or link it wraps. The backdrop hasaria-hidden. - What you provide
- Give a modal without a title an
aria-label, or screen readers only hear "Dialog". In a classic body withtitle, text with the id{name}-descriptionalso describes the dialog. The rest of the page does not getinert;aria-modaland the Tab loop keep it apart.