Skip to content
HyperUX Experimental
Demo

Focus-trapped modal dialog with Escape handling, optional backdrop, and scoped open/close events.


Alpine.js Dialog

huxDialog handles open/close state, optional backdrop behavior, and scoped lifecycle events for modal UI in Alpine.js. Pair it with your own dialog markup and focus trap directives.

This pattern expects the Alpine Focus plugin when you use x-trap in your markup: @alpinejs/focus.

API

huxDialog(config)

Returns an Alpine data object with:

Internal helper methods are private implementation details and are not part of the supported API contract.

Options

Quick Start

<div x-data="huxDialog({ dialogId: 'settings' })">
  <button type="button" x-on:click="openDialog()">Open dialog</button>

  <template x-teleport="body">
    <div x-cloak x-show="isOpen" x-on:keydown.escape.stop="handleEscape()">
      <div
        x-cloak
        x-show="showBackdrop"
        aria-hidden="true"
        x-on:click="handleBackdropClick()"
      ></div>

      <div
        x-trap.noscroll="isOpen"
        role="dialog"
        aria-modal="true"
        x-bind:aria-labelledby="dialogTitleId || null"
      >
        <h2 x-bind:id="dialogTitleId || null">Settings</h2>
        <button type="button" x-on:click="closeDialog()">Close</button>
      </div>
    </div>
  </template>
</div>

Common Usage Patterns

Persistent Confirmation Dialog

huxDialog({
  dialogId: 'confirm-delete',
  isPersistent: true,
})

Seamless Dialog (No Backdrop)

huxDialog({
  dialogId: 'quick-actions',
  isSeamless: true,
})

Behavior Contract

Error Handling

Accessibility Notes

Notes