The modal component provides a dialog box that can be opened and closed via buttons, keyboard shortcuts (ESC), or background clicks. It requires three specific targets: a container, a background overlay, and the modal view itself.
<div
data-controller="modal"
data-modal-hide-class="hidden"
data-modal-entering-class="ease-out duration-300"
data-modal-leaving-class="ease-in duration-200"
data-modal-to-background-class="opacity-100"
data-modal-from-background-class="opacity-0"
data-modal-to-view-class="opacity-100 translate-y-0 sm:scale-100"
data-modal-from-view-class="opacity-0 translate-y-4 sm:translate-y-0 sm:scale-95"
>
<!-- Trigger Button -->
<button
data-action="click->modal#open"
type="button"
>
Open Modal
</button>
<!-- Modal Container -->
<div
data-modal-target="container"
class="fixed inset-0 z-20 hidden overflow-y-auto"
>
<div class="flex items-center justify-center">
<!-- Background Overlay -->
<div
data-modal-target="background"
class="fixed inset-0 opacity-0 transition-opacity"
data-action="click->modal#closeBackground"
aria-hidden="true"
>
<div class="absolute inset-0 bg-gray-500 opacity-75"></div>
</div>
<!-- Modal View -->
<div
data-modal-target="view"
class="inline-block transform rounded-lg bg-white p-6 shadow-xl transition-all"
role="dialog"
aria-modal="true"
>
<h3>Header Title</h3>
<div>Modal content goes here.</div>
<button data-action="click->modal#close">Close</button>
</div>
</div>
</div>
</div>