Overlay <hw:modal>
View examples

Modal

The modal component displays accessible overlay dialogs with backdrop handling, focus trapping, animations, and optional Turbo Frame integration.

For most applications, you will render a trigger and content inside a single <hw:modal> component. If you need to load modal content from the server, you may use the frame-backed modal pattern described later in this document.

Contents

Basic Usage

To display a modal, place a trigger and content inside the root <hw:modal> component. The trigger opens the modal, while <hw:modal.content> contains the dialog surface that will be shown to the user:

BLADE
<hw:modal>
    <hw:modal.trigger>
        Edit profile
    </hw:modal.trigger>

    <hw:modal.content>
        <hw:modal.header>
            <hw:modal.title>Edit profile</hw:modal.title>
            <hw:modal.description>
                Update your account details.
            </hw:modal.description>
        </hw:modal.header>

        <form class="grid gap-4">
            <hw:input name="name" label="Name" />
            <hw:input name="email" type="email" label="Email" />
        </form>

        <hw:modal.footer>
            <hw:modal.close>Cancel</hw:modal.close>
            <hw:button>Save changes</hw:button>
        </hw:modal.footer>
    </hw:modal.content>
</hw:modal>

This example is self-contained: the root component mounts the modal Stimulus controller, the trigger opens it, and the content component renders the overlay, backdrop, dialog surface, and close button.

modal.content must render inside the same modal root. The root supplies the frame target, motion, size and close-button context used by the dependent content. For Turbo Stream updates, replace the modal frame or render the owning Modal root rather than rendering modal.content by itself. modal.trigger may render standalone; pass frame or data-turbo-frame explicitly when it should target a layout-shared Modal frame.

Opening a Modal

<hw:modal.trigger> renders a button-like control that opens the modal. It supports the same visual variants and sizes as <hw:button>:

BLADE
<hw:modal.trigger variant="outline" size="sm">
    Edit
</hw:modal.trigger>

You may also render the trigger as a link. A local link trigger opens the existing modal content immediately and cancels its href navigation:

BLADE
<hw:modal.trigger as="a" href="/users/1/edit">
    Edit user
</hw:modal.trigger>

When the modal is backed by a Turbo Frame, anchor triggers inherit the modal's frame target unless you provide a local frame value or an explicit data-turbo-frame attribute.

Prop Type Default Description
variant string 'default' Button variant
size string 'default' Button size
as button|a 'button' Rendered element
type button|submit|reset 'button' Native type when as="button"
frame string|object|false|null inherited Turbo Frame target for an anchor trigger; overrides the modal target when set

The as prop is trimmed, lowercased, and restricted to button or a; unsupported values are rejected. Disabled anchors omit href and receive aria-disabled="true" and tabindex="-1".

Closing a Modal

The X close icon is shown by default. You may hide it with :close-button="false":

BLADE
<hw:modal :close-button="false">
    <hw:modal.trigger>Open</hw:modal.trigger>

    <hw:modal.content>
        This modal does not render the X close icon.
    </hw:modal.content>
</hw:modal>

Use <hw:modal.close> for semantic footer or inline close actions:

BLADE
<hw:modal.footer>
    <hw:modal.close variant="outline">Cancel</hw:modal.close>
    <hw:button>Save changes</hw:button>
</hw:modal.footer>

modal.close supports variant, size, as, and type with the same defaults as modal.trigger, except variant defaults to outline. It uses the same button|a allowlist, native button type validation, and disabled-anchor behavior.

Dynamic Content with Turbo Frames

For an application layout that should reuse one modal shell for many links, provide a frame value. Laravel Hotwire will render a single Turbo Frame host inside the modal content:

BLADE
<a href="/posts/1/edit" data-turbo-frame="modal">
    Edit post
</a>

<hw:modal id="modal-shell" frame="modal" view-transition>
    <x-slot:loading_template>
        <hw:modal.title>Loading post</hw:modal.title>
        <div class="flex items-center justify-center p-8">
            Loading...
        </div>
    </x-slot:loading_template>
</hw:modal>

Render semantic title and description subcomponents in the destination response. The frame integration assigns missing ids and updates the persistent dialog references before opening:

BLADE
<hw:frame-or-page frame="modal" layout="layouts.app">
    <hw:modal.title>Edit post</hw:modal.title>
    <hw:modal.description>Update the post details.</hw:modal.description>

    <form method="POST">...</form>
</hw:frame-or-page>

When the frame receives content, the modal opens automatically. Close only the submitting instance through the common overlay response macro:

PHP
return turbo_stream()->closeOverlay($request->overlayOrigin());

The host can generate multiple instances from one declaration. Links targeting modal open a new instance (including links in an existing modal); links and forms without an explicit Frame target inside the opened content stay in that instance. The server view keeps <hw:frame-or-page frame="modal">, which emits the generated Frame id when responding to that instance. The package handles shell ids, accessible references, stacking and cleanup. Use the concrete id from the request if using an empty update/replace close stream instead: updating the canonical modal host does not close a child instance. A global refresh may discard a parent draft, so it is not a substitute for closing only the child. See frame-backed overlay instances.

A shared host can serve compact customer forms and wide sales forms: use <hw:button as="a" frame="modal" overlay-size="sm"> to size the loading state and <hw:frame-or-page frame="modal" layout="layouts.app" overlay-size="lg"> to let the response override it. Size accepts sm, md, lg, xl, full, auto or a CSS width. It updates the positioner, panel, content and close-icon hooks together; closing restores the host's size. Modal does not accept overlay-side. See presentation per opening.

If the slot does not contain <hw:modal.content>, an empty content host is appended automatically. If the slot contains one content host, that host wraps the frame and its slot becomes fallback content. More than one content host with frame is invalid.

The root component owns the matching frame id. Do not place a raw <turbo-frame> with that id in the slot; use one <hw:modal.content> when fallback content is needed.

The frame host rendered by the component looks like this:

HTML
<turbo-frame
    id="modal"
    data-controller="turbo--view-transition"
    data-turbo--view-transition-skip-initial-value="true"
    data-modal-target="dynamicContent"
></turbo-frame>

view-transition mounts turbo--view-transition directly on the persistent frame host. It animates navigations after the modal is open; the initial overlay opening and closing continue to use the modal's own motion. The prop has no effect without frame, and browsers without the View Transitions API keep the standard Turbo render.

Enable the option on the modal host, not only on a <hw:frame-or-page view-transition> response. Turbo preserves the existing host during a frame render and replaces its children, so attributes from the response frame are not copied to the host.

frame accepts strings or objects resolved with dom_id(); null, false, empty, and whitespace-only values disable the frame host.

An anchor <hw:modal.trigger> inside a frame-backed modal inherits the host frame as data-turbo-frame. Turbo navigates into the host; the modal opens when a loading template or the response content reaches the frame. A local anchor trigger without a frame opens the existing modal content immediately and cancels its href navigation. A local frame value overrides the inherited target. An explicit native data-turbo-frame wins; binding it to false suppresses the inherited target and makes the trigger local.

Loading Templates

The loading_template slot lives on the root modal and is used while dynamic frame content is loading.

The resolution order is: per-link data-loading-template, the modal's loading_template slot, then no loading template.

BLADE
<a
    href="/posts/1/edit"
    data-turbo-frame="modal"
    data-loading-template="#form-skeleton"
>
    Edit post
</a>

<template id="form-skeleton">
    <hw:modal.title>Loading post</hw:modal.title>
    <div class="grid gap-3 p-4">
        <hw:skeleton class="h-6 w-1/3" />
        <hw:skeleton class="h-32 w-full" />
    </div>
</template>

Behavior

Arbitrary attributes are forwarded to the root modal element. The accessibility attributes aria-label, aria-labelledby, aria-description, and aria-describedby are the exception: set them on <hw:modal> and the component routes them to the semantic dialog overlay for both explicit and automatic frame content. Authored values take precedence over references generated from modal.title and modal.description.

Regular data-controller / data-action attributes and the stimulus prop are merged and deduplicated with the internal modal controller. Component-owned data-modal-* attributes are protected; configure supported behavior with props instead.

BLADE
<hw:modal
    data-test-id="edit-post-modal"
    data-controller="analytics"
    data-action="modal:opened->analytics#track"
    aria-label="Edit post"
>
    ...
</hw:modal>

modal.title and modal.description receive stable ids and automatically set aria-labelledby and aria-describedby when they render with modal.content in the same server response.

The overlay uses data-state="open|closed", data-motion="default|none", hidden, and inert. Presence observes the actual finite CSS motion on the backdrop and dialog, so custom timing belongs in CSS rather than a duration prop. During exit the overlay becomes closed and inert immediately, remains rendered until motion settles, and can be reopened without stale teardown. Reduced-motion preference skips the wait.

Use initial-focus to choose where focus moves when the modal opens:

  • auto honors an eligible [autofocus] element, then focuses the dialog surface.
  • dialog focuses the dialog surface, falling back to the first focusable element.
  • first-focusable focuses the first eligible control, falling back to the dialog surface.
  • none leaves focus where it is; the first Tab or Shift+Tab still enters the focus trap.

Initial focus runs once per opening. Turbo Frame updates, morph reconnects, and resuming a parent modal after a nested overlay closes do not move it again.

modal.content is the dialog's scroll container. The footer stays in that normal flow and scrolls with the header and body; it is not sticky by default. For persistent actions, build an application layout with a dedicated body scrollport rather than applying position: sticky to modal.footer.

The shipped presets give a footer its edge-to-edge background, border, and rounded lower corners only when it is the last direct child of modal.content or of the component-owned Turbo Frame. A footer followed by other content remains a plain action row without negative margins or lower-corner styling. Frame-backed content receives the same spacing as direct content, and size="full" keeps each row at its natural height while the combined content remains scrollable.

Props

Prop Type Default Description
id string|object generated Root id. Pass a model for a stable cross-request id.
size string 'md' Preset (sm/md/lg/xl/full/auto) or arbitrary width (800px, 60vw)
class string '' Additional CSS classes on the panel
close-button bool true Shows the X close icon
fixed-top bool false Pins the modal to the top with a margin (ignored when size="full")
frame string|object|false|null null Enables dynamic modal content using a Turbo Frame. Objects are resolved with dom_id()
motion string 'default' default follows CSS motion; none disables it
initial-focus string 'auto' auto, dialog, first-focusable, or none; invalid values use auto
stimulus Htmlable|null null Additional Stimulus attributes to merge onto the root element
view-transition bool false Animates successive renders inside the frame host with the View Transitions API
aria-label string|null null Authored accessible name routed to the dialog overlay
aria-labelledby string|null null Authored accessible name reference routed to the dialog overlay
aria-description string|null null Authored accessible description routed to the dialog overlay
aria-describedby string|null null Authored accessible description reference routed to the dialog overlay

Subcomponents

Component Description
modal.trigger Button-like control that opens the modal
modal.content Overlay, backdrop and dialog content
modal.header Header layout inside content
modal.title Modal title
modal.description Supporting text
modal.footer Footer action layout
modal.close Button-like control that closes the modal

Size Presets

All presets except auto apply w-full plus a fixed max-w-* cap, so the dialog fills the available width up to the preset's cap.

size Width px cap (md+) Height
sm w-full md:max-w-md 448 auto
md (default) w-full md:max-w-xl 576 auto
lg w-full md:max-w-3xl 768 auto
xl w-full md:max-w-5xl 1024 auto
full w-full n/a h-full within viewport pad
auto No cap, no w-full n/a auto

Pass an arbitrary size to set an inline max width on the dialog positioner:

BLADE
<hw:modal size="800px">...</hw:modal>
<hw:modal size="60vw">...</hw:modal>

Controller Reference

Values

Value Type Default Description
lock-scroll Boolean true Locks body scroll when open
close-on-escape Boolean true Closes on Escape key
close-on-click-outside Boolean true Closes when clicking outside the modal
initial-focus String auto Selects the initial focus strategy

Actions

Action Description
modal#open Opens the modal
modal#close Closes the modal
modal#closeForCache Closes synchronously before Turbo caches the page

Events

Event Description
modal:opened Fired after the opening animation completes
modal:closed Fired when the overlay closes; detail.source identifies the origin of the close.

Accessibility

  • role="dialog" and aria-modal="true" on the overlay.
  • modal.title and modal.description automatically name and describe the overlay.
  • Frame-loaded labels are reconciled before the modal opens, and explicit unique label ids are preserved.
  • Root aria-label, aria-labelledby, aria-description, and aria-describedby values override generated references.
  • Focus is moved according to initial-focus and trapped inside the modal while it is open.
  • Focus returns to the element that triggered the modal on close.
  • Escape closes the modal by default and may be configured through the controller value.

Turbo Integration

The modal closes automatically on turbo:before-cache, preventing ghost modals when navigating with Turbo Drive.

For a frame-backed instance, use its validated request origin rather than the canonical host id:

PHP
return turbo_stream()->closeOverlay($request->overlayOrigin());

Requirements

  • No external dependencies.
  • Ships with _composition.js, _focus_trap.js, _frame_overlay.js, _overlay.js, _overlay_stack.js, _presence.js, and _top_layer.js; publishing the modal controller publishes these helpers too.

Styling hooks

The component exposes stable data-slot hooks for preset and application CSS:

  • data-slot="modal-overlay"
  • data-slot="modal-trigger"
  • data-slot="modal-backdrop"
  • data-slot="modal-positioner"
  • data-slot="modal-panel"
  • data-slot="modal-content"
  • data-slot="modal-header"
  • data-slot="modal-title"
  • data-slot="modal-description"
  • data-slot="modal-footer"
  • data-slot="modal-close"
  • data-slot="modal-close-icon"
  • data-slot="modal"