--- 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) }}

Usage

HTML + Javascript

{% lucide "circle-alert" %}

This component requires Javascript.

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).

Components with Javascript {% lucide "arrow-right" %} Use the CLI {% lucide "arrow-right" %} Individual JS file (dialog.js) {% lucide "arrow-right" %}
{% from "tabs.njk" import tabs %} {% set tabsets = [ { tab: "HTML", panel: code_block(code_html | prettyHtml, class="") }, { tab: "Javascipt (Alpine.js)", panel: code_block(code_script | prettyHtml, class="") } ] %} {{ tabs(id="alert-dialog", tabsets=tabsets) }}

Jinja and Nunjucks

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).

Use Nunjucks or Jinja macros {% lucide "arrow-right" %} Jinja macro {% lucide "arrow-right" %} Nunjucks macro {% lucide "arrow-right" %}
{% set raw_code %}{% raw %}{% set footer %} {% endset %} {{ dialog( id="demo-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 ) }}{% endraw %} {% endset%} {{ code_block(raw_code, "jinja") }}