--- layout: layouts/page.njk title: Alert Dialog description: A modal dialog that interrupts the user with important content and expects a response. toc: - label: Usage id: usage children: - label: HTML + Javascript id: usage-default - label: Jinja and Nunjucks id: usage-macro --- {% from "macros/code_preview.njk" import code_preview %} {% from "macros/code_block.njk" import code_block %} {% from "dialog.njk" import dialog %} {% set code_html %} {% set footer %} {% endset %} {{ dialog( id="alert-dialog", title="Are you absolutely sure?", description="This action cannot be undone. This will permanently delete your account and remove your data from our servers.", trigger="Open alert dialog", trigger_attrs={"class": "btn-outline"}, footer=footer, close_button=false, close_on_overlay_click=false, register_on=["alpine:init", "htmx:afterSwap"] ) }} {% endset %} {% set code_script %} {% endset %} {{ code_preview("alert-dialog", code_script | prettyHtml ~ code_html | prettyHtml) }}
The Alert Dialog component uses the same Alpine.js code as the Dialog component with one small difference: we set initialCloseOnOverlayClick to false to prevent the dialog from closing when the overlay is clicked.
You can include the Javascript code provided below, load it as an individual file or use the CLI. Some Alpine.js properties are also required on certain elements (e.g. x-bind, x-data, @click).
You can use the dialog() Nunjucks or Jinja macro for this component. If you use one of the macros, make sure to set close_on_overlay_click and close_button to False (or false for Nunjucks).