Files
basecoat/docs/src/components/alert-dialog.md
T

120 lines
5.5 KiB
Markdown
Raw Normal View History

2025-04-25 16:42:10 +08:00
---
2026-06-03 23:08:41 +08:00
templateEngineOverride: njk
2025-05-01 12:50:40 +08:00
layout: layouts/page.njk
2025-04-25 16:42:10 +08:00
title: Alert Dialog
description: A modal dialog that interrupts the user with important content and expects a response.
toc:
- label: Usage
id: usage
children:
2025-06-13 14:14:38 +08:00
- label: HTML
id: usage-html
2025-05-07 12:03:38 +08:00
- label: Jinja and Nunjucks
id: usage-macro
2025-04-25 16:42:10 +08:00
---
{% from "macros/code_preview.njk" import code_preview %}
{% from "macros/code_block.njk" import code_block %}
2025-05-01 20:53:03 +08:00
{% from "dialog.njk" import dialog %}
2025-04-25 16:42:10 +08:00
2025-04-29 19:14:31 +08:00
{% set code_html %}
{% set footer %}
2025-06-14 16:48:01 +08:00
<button class="btn-outline" onclick="document.getElementById('alert-dialog').close()">Cancel</button>
<button class="btn-primary" onclick="document.getElementById('alert-dialog').close()">Continue</button>
2025-04-25 16:42:10 +08:00
{% endset %}
{{ dialog(
2025-04-25 23:14:05 +08:00
id="alert-dialog",
2025-04-25 16:42:10 +08:00
title="Are you absolutely sure?",
description="This action cannot be undone. This will permanently delete your account and remove your data from our servers.",
2025-04-25 23:14:05 +08:00
trigger="Open alert dialog",
2025-04-25 16:42:10 +08:00
trigger_attrs={"class": "btn-outline"},
footer=footer,
close_button=false,
2025-06-13 14:14:38 +08:00
close_on_overlay_click=false
2025-04-29 19:14:31 +08:00
) }}
{% endset %}
2025-06-13 14:14:38 +08:00
{{ code_preview("alert-dialog", code_html | prettyHtml) }}
2025-04-25 16:42:10 +08:00
<h2 id="usage"><a href="#usage">Usage</a></h2>
2025-06-13 14:14:38 +08:00
<h3 id="usage-default"><a href="#usage-html">HTML</a></h3>
2025-04-29 19:14:31 +08:00
2025-05-07 12:03:38 +08:00
<section class="prose">
2025-06-14 16:48:01 +08:00
<p>The Alert Dialog component is identical to <a href="/components/dialog" class="font-medium underline underline-offset-4">Dialog</a> except for two small differences: we do not display a close button and we do not let the user close the dialog when clicking on the backdrop (no <code>onclick</code> attribute on the <code>&lt;dialog&gt;</code> or the <code>&lt;article&gt;</code> elements).</p>
2025-05-07 12:03:38 +08:00
</section>
2025-11-04 12:46:00 +08:00
{{ code_block(code_html | prettyHtml) }}
2025-04-25 16:42:10 +08:00
2025-06-14 16:48:01 +08:00
<section class="prose">
<p>The component has the following HTML structure:</p>
<dl>
<dt><code class="highlight language-html">&lt;button type="button"&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>The trigger button to open the alert dialog (using <a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLDialogElement/showModal" target="_blank">the native <code>showModal()</code> method</a>).</dd>
<dt><code class="highlight language-html">&lt;dialog class="dialog" id="{ DIALOG_ID }"&gt;</code></dt>
<dd>Wraps the whole component and acts as the backdrop. It can also have the following attributes:
<ul>
<li><code>aria-labelledby="{ TITLE_ID }"</code>: linked to by the <code>aria-labelledby</code> attribute of the dialog.</li>
<li><code>aria-describedby="{ DESCRIPTION_ID }"</code>: linked to by the <code>aria-describedby</code> attribute of the <code>&lt;dialog&gt;</code>.</li>
</ul>
<dl>
<dt><code class="highlight language-html">&lt;article&gt;</code></dt>
<dd>Wraps the content of the alert dialog.
<dl>
<dt><code class="highlight language-html">&lt;header&gt;</code></dt>
<dd>Contains the header for the alert dialog:
<dl>
<dt><code class="highlight language-html">&lt;h2&gt;</code></dt>
<dd>The title of the alert dialog, must have an <code>id</code> if you use the <code>aria-labelledby</code> attribute on the <code>&lt;dialog&gt;</code>.</dd>
<dt><code class="highlight language-html">&lt;p&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>The description of the alert dialog, must have an <code>id</code> if you use the <code>aria-describedby</code> attribute on the <code>&lt;dialog&gt;</code>.</dd>
</dl>
</dd>
<dt><code class="highlight language-html">&lt;section&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>For the content.</dd>
<dt><code class="highlight language-html">&lt;footer&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>Usually contains actions.</dd>
</dl>
</dd>
</dl>
</dd>
</dl>
</section>
2025-05-07 12:03:38 +08:00
<h3 id="usage-macro"><a href="#usage-macro">Jinja and Nunjucks</a></h3>
2025-04-25 16:42:10 +08:00
2025-05-07 12:03:38 +08:00
<div class="prose">
2025-06-13 14:14:38 +08:00
<p>You can use the <code class="relative rounded bg-muted px-[0.3rem] py-[0.2rem] font-mono text-xs">dialog()</code> Nunjucks or Jinja macro for this component. Make sure to set <code>close_on_overlay_click</code> and <code>close_button</code> to <code>False</code> (or <code>false</code> for Nunjucks).</p>
2025-05-07 12:03:38 +08:00
</div>
<div class="flex flex-wrap gap-2 my-6">
<a class="badge-outline" href="/cli#macros" target="_blank">
2025-05-07 12:03:38 +08:00
Use Nunjucks or Jinja macros
{% lucide "arrow-right" %}
</a>
<a class="badge-outline" href="https://github.com/hunvreus/basecoat/blob/main/src/jinja/dialog.html.jinja" target="_blank">
Jinja macro
{% lucide "arrow-right" %}
</a>
<a class="badge-outline" href="https://github.com/hunvreus/basecoat/blob/main/src/nunjucks/dialog.njk" target="_blank">
Nunjucks macro
{% lucide "arrow-right" %}
</a>
</div>
2025-04-25 16:42:10 +08:00
{% set raw_code %}{% raw %}{% set footer %}
2025-06-14 16:48:01 +08:00
<button class="btn-outline" onclick="document.getElementById('demo-alert-dialog').close()">Cancel</button>
<button class="btn-primary" onclick="document.getElementById('demo-alert-dialog').close()">Continue</button>
2025-04-25 16:42:10 +08:00
{% 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") }}