Components
Dialog
The browser does the hard parts. Halora adds the look and the small conveniences.
Example#
<button class="ha-button ha-button--primary" type="button" data-ha-dialog-open="demo-dialog">Open dialog</button>
<button class="ha-button ha-button--secondary" type="button" data-ha-dialog-open="demo-dialog-sm">Small dialog</button>
<dialog class="ha-dialog" id="demo-dialog" aria-labelledby="demo-dialog-title">
<div class="ha-dialog__header">
<h2 class="ha-dialog__title" id="demo-dialog-title">Curriculum</h2>
<button class="ha-icon-button ha-icon-button--sm ha-icon-button--plain" type="button" data-ha-dialog-close aria-label="Close dialog"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="M6 6l12 12M18 6 6 18"/></svg></button>
</div>
<div class="ha-dialog__body">
<p>Press Escape, click the backdrop, or use the close button. Focus returns to the button that opened this dialog.</p>
</div>
<div class="ha-dialog__footer">
<button class="ha-button ha-button--secondary" type="button" data-ha-dialog-close>Cancel</button>
<button class="ha-button ha-button--primary" type="button" data-ha-dialog-close>Done</button>
</div>
</dialog>
<dialog class="ha-dialog ha-dialog--sm" id="demo-dialog-sm" aria-labelledby="demo-dialog-sm-title">
<div class="ha-dialog__header">
<h2 class="ha-dialog__title" id="demo-dialog-sm-title">Small</h2>
<button class="ha-icon-button ha-icon-button--sm ha-icon-button--plain" type="button" data-ha-dialog-close aria-label="Close dialog"><svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><path d="M6 6l12 12M18 6 6 18"/></svg></button>
</div>
<div class="ha-dialog__body"><p>A 440px dialog for short confirmations.</p></div>
</dialog>Wire it with initDialogs(). The docs site already calls it on every page.
Sizes#
| Modifier | Width |
|---|---|
ha-dialog--sm | 440px |
(default) | 640px |
ha-dialog--lg | 1000px, 90% of the viewport height |
Under 640px | Full screen, 100vw by 100dvh, square corners, stacked footer buttons |
Behavior#
import { initDialogs, openDialog, closeDialog } from '@originlabsbr/halora/dialog';
// buttons with data-ha-dialog-open="id" and data-ha-dialog-close
const stop = initDialogs({ hash: true });
// or by hand
const dialog = document.getElementById('cv');
openDialog(dialog, { trigger: document.getElementById('open-cv') });
closeDialog(dialog);With hash: true, #cv opens the dialog with that id on load and on navigation, the URL updates when it opens, and the hash clears when it closes.
Accessibility#
- Use
aria-labelledbypointing at the title. showModal()makes the rest of the page inert and traps focus. Escape closes it.- Focus returns to the opener. The behavior records the trigger and restores it on close.
- Page scroll is locked with
html:has(dialog[open]), with anha-scroll-lockfallback class. - The dialog background is opaque even on brands with translucent surfaces.
| Class | Purpose |
|---|---|
ha-dialog, --sm, --lg | The element and sizes. |
ha-dialog__header, __title, __body, __footer | Parts. The body scrolls, the header and footer do not. |
Do
- Give every dialog a visible title and a close button.
- Use it for focused tasks and short reads.
Don't
- Open a dialog on page load.
- Nest dialogs.