Modal
Alpine x-data 驱动的叠加对话框。点击 trigger slot 中的元素打开,或从其他地方 $dispatch("open-modal-{id}") 打开。size(sm/md/lg/xl/full)、title、showClose、closeOnBackdrop 共 5 个 prop。背景点击 / Escape / × 按钮关闭。x-trap 处理焦点陷阱,x-teleport 渲染到 body 末尾。
1. 最简: 带 trigger slot
Confirm
A centered dialog with a backdrop. Esc and a backdrop click both close it.
<x-modal title="Confirm">
<x-slot:trigger>
<x-button>Open default modal</x-button>
</x-slot:trigger>
<p class="text-sm text-base-content/80">A centered dialog with a backdrop. Esc and a backdrop click both close it.</p>
</x-modal>
省略 title 时 — header 只会显示 × 按钮 (确认 title 不是必填项)
Without a title the header row is dropped and only the close button remains.
<x-modal>
<x-slot:trigger>
<x-button appearance="soft">Open titleless modal</x-button>
</x-slot:trigger>
<p class="text-sm text-base-content/80">Without a title the header row is dropped and only the close button remains.</p>
</x-modal>
size — sm / md / lg / xl / full
Small modal
Best for short confirmations.
Medium modal (default)
The default width.
Large modal
Room for a short form.
Extra-large modal
Room for a table or a two-column layout.
Full-width modal
Edge-to-edge apart from the outer margin.
<x-modal title="Small modal" size="sm">
<x-slot:trigger><x-button size="sm">sm (max-w-sm)</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">Best for short confirmations.</p>
</x-modal>
<x-modal title="Medium modal (default)" size="md">
<x-slot:trigger><x-button size="sm">md (max-w-lg)</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">The default width.</p>
</x-modal>
<x-modal title="Large modal" size="lg">
<x-slot:trigger><x-button size="sm">lg (max-w-2xl)</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">Room for a short form.</p>
</x-modal>
<x-modal title="Extra-large modal" size="xl">
<x-slot:trigger><x-button size="sm">xl (max-w-4xl)</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">Room for a table or a two-column layout.</p>
</x-modal>
<x-modal title="Full-width modal" size="full">
<x-slot:trigger><x-button size="sm">full (max-w-full mx-4)</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">Edge-to-edge apart from the outer margin.</p>
</x-modal>
showClose — 控制 × 按钮的显示
showClose=false
The X is hidden — give the user an explicit action instead.
showClose=true (default)
The X sits in the header.
<x-modal title="showClose=false" :showClose="false">
<x-slot:trigger><x-button appearance="outline" size="sm">showClose=false</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">The X is hidden — give the user an explicit action instead.</p>
<x-slot:actions>
<x-button size="sm" @click="open = false">OK</x-button>
</x-slot:actions>
</x-modal>
<x-modal title="showClose=true (default)">
<x-slot:trigger><x-button appearance="outline" size="sm">showClose=true (default)</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">The X sits in the header.</p>
</x-modal>
closeOnBackdrop — 点击背景的行为
closeOnBackdrop=true (default)
Clicking the backdrop closes the dialog.
closeOnBackdrop=false
The backdrop is inert — use this for destructive confirmations.
<x-modal title="closeOnBackdrop=true (default)">
<x-slot:trigger><x-button appearance="outline" size="sm">closeOnBackdrop=true (default)</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">Clicking the backdrop closes the dialog.</p>
</x-modal>
<x-modal title="closeOnBackdrop=false" :closeOnBackdrop="false">
<x-slot:trigger><x-button appearance="outline" size="sm">closeOnBackdrop=false</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">The backdrop is inert — use this for destructive confirmations.</p>
</x-modal>
2. title + size + actions slot (典型确认对话框)
Delete confirmation
Are you sure you want to delete this? This cannot be undone.
New project
<x-modal title="Delete confirmation" size="sm" :closeOnBackdrop="false">
<x-slot:trigger><x-button color="error" size="sm">Delete…</x-button></x-slot:trigger>
<p class="text-sm text-base-content/80">Are you sure you want to delete this? This cannot be undone.</p>
<x-slot:actions>
<x-button appearance="ghost" size="sm" @click="open = false">Cancel</x-button>
<x-button color="error" size="sm" @click="open = false">Delete</x-button>
</x-slot:actions>
</x-modal>
<x-modal title="New project" size="md">
<x-slot:trigger><x-button color="primary" size="sm">+ Create</x-button></x-slot:trigger>
<div class="space-y-3">
<x-input label="Project name" placeholder="my-app" />
<x-textarea label="Description" rows="3" placeholder="optional" />
</div>
<x-slot:actions>
<x-button appearance="ghost" size="sm" @click="open = false">Cancel</x-button>
<x-button color="primary" size="sm" @click="open = false">Create</x-button>
</x-slot:actions>
</x-modal>
内容比 viewport 更高时 — panel 保持在 viewport 内,只有 body 滚动;header 和 actions 始终可见 (v0.10.9)
Edit profile
<x-modal title="Edit profile" size="lg">
<x-slot:trigger><x-button color="primary" size="sm">Open a tall form</x-button></x-slot:trigger>
<div class="space-y-3">
<x-input label="First name" placeholder="Ada" />
<x-input label="Last name" placeholder="Lovelace" />
<x-input label="Email" type="email" placeholder="ada@example.com" />
<x-input label="Phone" type="tel" placeholder="+1 555 0100" />
<x-input label="Company" placeholder="Analytical Engines Ltd." />
<x-input label="Job title" placeholder="Chief Engineer" />
<x-input label="Street address" placeholder="12 Byron St." />
<x-input label="City" placeholder="London" />
<x-input label="Postal code" placeholder="W1K 1AA" />
<x-input label="Country" placeholder="United Kingdom" />
<x-input label="Website" type="url" placeholder="https://example.com" />
<x-textarea label="Bio" rows="4" placeholder="A short introduction…" />
</div>
<x-slot:actions>
<x-button appearance="ghost" size="sm" @click="open = false">Cancel</x-button>
<x-button color="primary" size="sm" @click="open = false">Save</x-button>
</x-slot:actions>
</x-modal>
4. 传入 id,从其他位置的任意按钮通过 dispatch 打开(size: sm/md(default)/lg/xl/full,通过背景click/Escape/×/actions 的 @click='open = false' 关闭,x-trap 实现 focus trap,x-teleport 渲染到 body 下)
Welcome
Declared without a trigger slot: it is opened purely by dispatching open-modal-welcome.
Reset settings
All settings return to their defaults.
<x-button color="primary" size="sm" x-on:click="$dispatch('open-modal-welcome')">Open the welcome modal</x-button>
<x-button color="info" size="sm" x-on:click="$dispatch('open-modal-confirm-reset')">Open the reset modal</x-button>
<x-modal id="welcome" title="Welcome" size="md">
<p class="text-sm text-base-content/80">Declared without a trigger slot: it is opened purely by dispatching <code class="text-xs">open-modal-welcome</code>.</p>
<x-slot:actions>
<x-button size="sm" @click="open = false">Close</x-button>
</x-slot:actions>
</x-modal>
<x-modal id="confirm-reset" title="Reset settings" size="sm" :closeOnBackdrop="false">
<p class="text-sm text-base-content/80">All settings return to their defaults.</p>
<x-slot:actions>
<x-button appearance="ghost" size="sm" @click="open = false">Cancel</x-button>
<x-button color="warning" size="sm" @click="open = false">Reset</x-button>
</x-slot:actions>
</x-modal>