| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: "Dialogs" |
| 4 | description: Show modals and popups on demand |
| 5 | icon: "icon-[vaadin--modal-list]" |
| 6 | soon: true |
| 7 | --- |
| 8 | |
| 9 | <script> |
| 10 | server.get("/demo", () => ` |
| 11 | <button class="px-3.5 py-1.5 text-sm font-medium rounded-md cursor-pointer text-neutral-600 dark:text-neutral-300 border border-neutral-200 dark:border-neutral-700 interact:bg-neutral-50 dark:interact:bg-neutral-850 interact:text-neutral-800 dark:interact:text-neutral-100 active:scale-[0.98] transition" |
| 12 | hx-get="/modal" |
| 13 | hx-target="body" |
| 14 | hx-swap="beforeend"> |
| 15 | Open a Modal |
| 16 | </button> |
| 17 | `); |
| 18 | |
| 19 | server.get("/modal", () => ` |
| 20 | <div id="modal" class="fixed inset-0 bg-black/50 z-50 flex flex-col items-center" style="animation:fadeIn 150ms ease" |
| 21 | _="on closeModal add .closing wait for animationend then remove me"> |
| 22 | <div class="absolute inset-0 z-[-1]" _="on click trigger closeModal"></div> |
| 23 | <div class="mt-[10vh] w-4/5 max-w-md bg-white dark:bg-neutral-900 rounded-lg shadow-xl p-6" style="animation:zoomIn 150ms ease" |
| 24 | _="on closeModal in closest #modal add .closing"> |
| 25 | <h1 class="text-lg font-semibold text-neutral-800 dark:text-neutral-100 mb-2">Modal Dialog</h1> |
| 26 | <p class="text-sm text-neutral-600 dark:text-neutral-400 mb-6"> |
| 27 | This is the modal content. You can put anything here, like text, or a form, or an image. |
| 28 | </p> |
| 29 | <button class="px-3.5 py-1.5 text-sm font-medium rounded-md cursor-pointer text-white dark:text-neutral-900 bg-neutral-800 dark:bg-neutral-200 interact:bg-neutral-700 dark:interact:bg-neutral-300 active:scale-[0.98] transition" _="on click trigger closeModal">Close</button> |
| 30 | </div> |
| 31 | </div> |
| 32 | `); |
| 33 | |
| 34 | server.start("/demo"); |
| 35 | </script> |
| 36 | |
| 37 | <div id="demo-content" class="not-prose demo-container flex justify-center"></div> |
| 38 | |
| 39 | ## Browser native dialogs |
| 40 | |
| 41 | The simplest approach uses [`hx-confirm`](/reference/attributes/hx-confirm) to trigger a native browser prompt before sending a request. |
| 42 | |
| 43 | On the client, add `hx-confirm` to any element that makes a request: |
| 44 | |
| 45 | ```html |
| 46 | <button hx-post="/submit" |
| 47 | hx-confirm="Are you sure?" |
| 48 | hx-target="#response"> |
| 49 | Prompt Submission |
| 50 | </button> |
| 51 | ``` |
| 52 | |
| 53 | - [`hx-confirm`](/reference/attributes/hx-confirm)=`"Are you sure?"` shows the browser's built-in confirm dialog. |
| 54 | - The AJAX request only fires if the user clicks OK. No extra HTML or CSS required. |
| 55 | |
| 56 | ## Custom modals |
| 57 | |
| 58 | htmx makes it straightforward to build modals from scratch. A button loads remote content and appends it to `<body>` as a full-screen overlay. |
| 59 | |
| 60 | On the client, a button fetches the modal markup: |
| 61 | |
| 62 | ```html |
| 63 | <button hx-get="/modal" |
| 64 | hx-target="body" |
| 65 | hx-swap="beforeend"> |
| 66 | Open a Modal |
| 67 | </button> |
| 68 | ``` |
| 69 | |
| 70 | - [`hx-get`](/reference/attributes/hx-get) requests `/modal`. |
| 71 | - [`hx-target`](/reference/attributes/hx-target)=`"body"` targets the document body. |
| 72 | - [`hx-swap`](/reference/attributes/hx-swap)=[`"beforeend"`](/reference/attributes/hx-swap#beforeend) appends the response as the last child of `<body>`. |
| 73 | |
| 74 | On the server, respond with the full modal markup. [Hyperscript](https://hyperscript.org) handles the close animation -- adding a `.closing` class, waiting for the CSS transition to finish, then removing the element: |
| 75 | |
| 76 | ```html |
| 77 | <div id="modal" |
| 78 | _="on closeModal add .closing |
| 79 | wait for animationend |
| 80 | then remove me"> |
| 81 | <div class="modal-underlay" |
| 82 | _="on click trigger closeModal"> |
| 83 | </div> |
| 84 | <div class="modal-content"> |
| 85 | <h1>Modal Dialog</h1> |
| 86 | <p>This is the modal content.</p> |
| 87 | <button _="on click trigger closeModal"> |
| 88 | Close |
| 89 | </button> |
| 90 | </div> |
| 91 | </div> |
| 92 | ``` |
| 93 | |
| 94 | Click the underlay or the Close button to dismiss. |
| 95 | |
| 96 | ## Notes |
| 97 | |
| 98 | CSS frameworks like Bootstrap and UIKit ship their own modal components. htmx works with these too -- use `hx-get` to load modal content into a framework-provided container, and trigger the framework's show/hide methods via JavaScript or Hyperscript. |
| 99 | |
| 100 | <style> |
| 101 | @keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } } |
| 102 | @keyframes fadeOut { from { opacity: 1 } to { opacity: 0 } } |
| 103 | @keyframes zoomIn { from { transform: scale(0.9) } to { transform: scale(1) } } |
| 104 | @keyframes zoomOut { from { transform: scale(1) } to { transform: scale(0.9) } } |
| 105 | #modal.closing { animation: fadeOut 150ms ease forwards } |
| 106 | #modal.closing > div:last-child { animation: zoomOut 150ms ease forwards } |
| 107 | </style> |