Docs

Main site

Installation Browser support Extend config MCP server
Validation Page speed testing Responsive design
Accordion Aspect Ratio Avatar Badge Banner Bento Grid Blockquote Breadcrumbs Button Callout Card Drawer Dropdown Link Modal Navbar Navblock Pagination Popover Progress Separator Tabs Timeline Toast Tooltip
Checkbox Datepicker File upload Form field Input Label OTP Radio Range Select Textarea Toggle
Header Footer
Accordion Auth Checkbox & Radio Content Drawers Features Modals Statistics & Metrics Teams Dropdowns
Address Contact number Currency symbol Date Emirates ID Name
Close
Home Documentation Components Modal

Modal

A Modal is a dialog window that overlays the page to focus attention on a single, important task — confirming a destructive action, completing a short form, or surfacing a critical message. "aegov-modal" traps focus inside the dialog, locks the page behind it, and stacks above the rest of the page — moving out of an ancestor only while open, and only if that ancestor would trap it.

Overlay Container
Status
Active
Introduced
4.0.x
View source code

Anatomy

aegov-modal renders a fixed backdrop and a centred (or top/bottom-docked) dialog, both inside a real shadow root. The component stays where it's declared in markup and takes its z-index from a shared overlay manager; only while open, and only if an ancestor would trap it (a transform, filter, stacking context, or hidden ancestor), does it move out of that ancestor, returning on close.

Image showing anatomy of a modal

Item Name Description
1 Backdrop The dimmed overlay behind the dialog. Clicking it closes the modal by default.
2 Dialog The role="dialog" content panel — the default slot renders here.
3 Slotted content Your own heading, body text, and action buttons — there's no separate header/footer slot.

Opening and closing

Control the modal imperatively with show()/close()/toggle() from a trigger's click handler, or declaratively by toggling is-open from your framework's own state. Both paths run the identical open/close sequence — the modal correctly opens even when is-open="true" is present from the very first render, not only on a later change.

Delete application

Delete this application?

This will permanently remove your draft application for the Emirates ID renewal service. This action cannot be undone.

Cancel Delete
<aegov-button id="open-confirm">Delete application</aegov-button>

<aegov-modal id="confirm-modal" label="Delete this application?">
  <h3 id="confirm-title" class="font-bold text-xl mb-2">Delete this application?</h3>
  <p class="text-base mb-6">This will permanently remove your draft application for the Emirates ID renewal service. This action cannot be undone.</p>
  <div class="flex justify-end gap-3">
    <aegov-button variant="secondary" id="cancel-confirm">Cancel</aegov-button>
    <aegov-button variant="danger" id="confirm-delete">Delete</aegov-button>
  </div>
</aegov-modal>

<script>
  const modal = document.getElementById('confirm-modal');
  document.getElementById('open-confirm').addEventListener('click', () => modal.show());
  document.getElementById('cancel-confirm').addEventListener('click', () => modal.close());
  document.getElementById('confirm-delete').addEventListener('click', () => modal.close());
</script>

Sizes

size sets the panel's fixed width (300px, 600px, 800px or 1140px, capped at the viewport width) and scales padding and corner radius together, mirroring aegov-card's own size tokens. sm suits a short yes/no confirmation; base is the default; lg gives forms and richer content more room; xl fits data-heavy or multi-column layouts.

Small Base Large Extra large

Compact dialog

Small size suits a short confirmation.

Close

Standard dialog

Base size fits most confirmations and short forms.

Close

Spacious dialog

Large size gives forms and richer content more room.

Close

Extra large dialog

Extra large fits data-heavy or multi-column content.

Close
<aegov-button id="size-sm">Small</aegov-button>
<aegov-button id="size-base">Base</aegov-button>
<aegov-button id="size-lg">Large</aegov-button>
<aegov-button id="size-xl">Extra large</aegov-button>

<aegov-modal id="modal-sm" size="sm" label="Small modal">
  <h3 class="font-bold text-lg mb-2">Compact dialog</h3>
  <p class="text-base mb-6">Small size suits a short confirmation.</p>
  <div class="flex justify-end"><aegov-button id="close-sm" size="sm">Close</aegov-button></div>
</aegov-modal>
<aegov-modal id="modal-base" size="base" label="Base modal">
  <h3 class="font-bold text-xl mb-2">Standard dialog</h3>
  <p class="text-base mb-6">Base size fits most confirmations and short forms.</p>
  <div class="flex justify-end"><aegov-button id="close-base">Close</aegov-button></div>
</aegov-modal>
<aegov-modal id="modal-lg" size="lg" label="Large modal">
  <h3 class="font-bold text-2xl mb-2">Spacious dialog</h3>
  <p class="text-base mb-6">Large size gives forms and richer content more room.</p>
  <div class="flex justify-end"><aegov-button id="close-lg">Close</aegov-button></div>
</aegov-modal>
<aegov-modal id="modal-xl" size="xl" label="Extra large modal">
  <h3 class="font-bold text-2xl mb-2">Extra large dialog</h3>
  <p class="text-base mb-6">Extra large fits data-heavy or multi-column content.</p>
  <div class="flex justify-end"><aegov-button id="close-xl">Close</aegov-button></div>
</aegov-modal>

<script>
  ['sm','base','lg','xl'].forEach(s => {
    const m = document.getElementById('modal-' + s);
    document.getElementById('size-' + s).addEventListener('click', () => m.show());
    document.getElementById('close-' + s).addEventListener('click', () => m.close());
  });
</script>

Position

position docks the dialog to the top, bottom, or centre of the viewport. Top suits a system notice that shouldn't demand full attention; bottom reads as a mobile-style action sheet.

Centre Top Bottom

Centre position

The default — anchored to the centre of the viewport.

Close

Top position

Useful for non-blocking notices near the page header.

Close

Bottom position

A mobile-friendly action-sheet style, close to the thumb zone.

Close
<aegov-button id="pos-center">Centre</aegov-button>
<aegov-button id="pos-top">Top</aegov-button>
<aegov-button id="pos-bottom">Bottom</aegov-button>

<aegov-modal id="modal-center" position="center" label="Centre modal">
  <h3 class="font-bold text-xl mb-2">Centre position</h3>
  <p class="text-base mb-6">The default — anchored to the centre of the viewport.</p>
  <div class="flex justify-end"><aegov-button id="close-center">Close</aegov-button></div>
</aegov-modal>
<aegov-modal id="modal-top" position="top" label="Top modal">
  <h3 class="font-bold text-xl mb-2">Top position</h3>
  <p class="text-base mb-6">Useful for non-blocking notices near the page header.</p>
  <div class="flex justify-end"><aegov-button id="close-top">Close</aegov-button></div>
</aegov-modal>
<aegov-modal id="modal-bottom" position="bottom" label="Bottom modal">
  <h3 class="font-bold text-xl mb-2">Bottom position</h3>
  <p class="text-base mb-6">A mobile-friendly action-sheet style, close to the thumb zone.</p>
  <div class="flex justify-end"><aegov-button id="close-bottom">Close</aegov-button></div>
</aegov-modal>

<script>
  ['center','top','bottom'].forEach(p => {
    const m = document.getElementById('modal-' + p);
    document.getElementById('pos-' + p).addEventListener('click', () => m.show());
    document.getElementById('close-' + p).addEventListener('click', () => m.close());
  });
</script>

Overlay opacity

overlay-opacity controls how much of the page behind the dialog is dimmed — light for a subtle wash, base (default) for balanced contrast, and dark for high-attention dialogs such as destructive confirmations.

Light backdrop Base backdrop Dark backdrop

Light backdrop

A gentle dim, suitable for low-stakes notices.

Close

Base backdrop

The default — clear separation from the page without being heavy.

Close

Dark backdrop

High contrast — use for destructive confirmations or critical alerts.

Close
<aegov-button id="op-light">Light backdrop</aegov-button>
<aegov-button id="op-base">Base backdrop</aegov-button>
<aegov-button id="op-dark">Dark backdrop</aegov-button>

<aegov-modal id="modal-light" overlay-opacity="light" label="Light backdrop">
  <h3 class="font-bold text-xl mb-2">Light backdrop</h3>
  <p class="text-base mb-6">A gentle dim, suitable for low-stakes notices.</p>
  <div class="flex justify-end"><aegov-button id="close-light">Close</aegov-button></div>
</aegov-modal>
<aegov-modal id="modal-base-op" overlay-opacity="base" label="Base backdrop">
  <h3 class="font-bold text-xl mb-2">Base backdrop</h3>
  <p class="text-base mb-6">The default — clear separation from the page without being heavy.</p>
  <div class="flex justify-end"><aegov-button id="close-base-op">Close</aegov-button></div>
</aegov-modal>
<aegov-modal id="modal-dark" overlay-opacity="dark" label="Dark backdrop">
  <h3 class="font-bold text-xl mb-2">Dark backdrop</h3>
  <p class="text-base mb-6">High contrast — use for destructive confirmations or critical alerts.</p>
  <div class="flex justify-end"><aegov-button id="close-dark">Close</aegov-button></div>
</aegov-modal>

<script>
  const map = { light: 'light', base: 'base-op', dark: 'dark' };
  Object.entries(map).forEach(([key, id]) => {
    const m = document.getElementById('modal-' + id);
    document.getElementById('op-' + key).addEventListener('click', () => m.show());
    document.getElementById('close-' + id).addEventListener('click', () => m.close());
  });
</script>

Preventing accidental dismissal

Set is-close-with-overlay="false" to stop a backdrop click from closing the dialog — useful for a form where losing progress by an accidental click outside would be costly. Escape still closes the dialog either way; it always closes for accessibility and cannot be disabled.

Update mobile number

Update mobile number

New mobile number
Cancel Save
<aegov-button id="open-form">Update mobile number</aegov-button>

<aegov-modal id="form-modal" is-close-with-overlay="false" label="Update mobile number">
  <h3 id="form-title" class="font-bold text-xl mb-4">Update mobile number</h3>
  <aegov-form-field>
    <aegov-label>New mobile number</aegov-label>
    <aegov-input field-id="form-mobile" type="tel" placeholder="+971 50 000 0000"></aegov-input>
  </aegov-form-field>
  <div class="flex justify-end gap-3 mt-6">
    <aegov-button variant="secondary" id="form-cancel">Cancel</aegov-button>
    <aegov-button id="form-save">Save</aegov-button>
  </div>
</aegov-modal>

<script>
  const m = document.getElementById('form-modal');
  document.getElementById('open-form').addEventListener('click', () => m.show());
  document.getElementById('form-cancel').addEventListener('click', () => m.close());
  document.getElementById('form-save').addEventListener('click', () => m.close());
</script>

Composing with Form Field

A modal drops naturally around any other component — here, an aegov-form-field for a short, single-field update. Closing with unsaved changes is one of the clearest cases for listening to aegovModalBeforeClose. Save and Cancel close the modal with close(), which fires the same event, so the example keeps the saved note apart from the draft and only cancels the close while the two differ — Save stores the draft first, Cancel reverts it. The guard covers a backdrop click and close(); Escape always closes the modal without firing it (WCAG 2.1.2).

Edit profile note

Edit profile note

Note
Cancel Save
<aegov-button id="open-guarded">Edit profile note</aegov-button>

<aegov-modal id="guarded-modal" label="Edit profile note">
  <h3 id="guarded-title" class="font-bold text-xl mb-4">Edit profile note</h3>
  <aegov-form-field>
    <aegov-label>Note</aegov-label>
    <aegov-textarea field-id="guarded-note" placeholder="Add a note visible only to case officers."></aegov-textarea>
  </aegov-form-field>
  <div class="flex justify-end gap-3 mt-6">
    <aegov-button variant="secondary" id="guarded-cancel">Cancel</aegov-button>
    <aegov-button id="guarded-save">Save</aegov-button>
  </div>
</aegov-modal>

<script>
  const m = document.getElementById('guarded-modal');
  let savedNote = '';
  // aegov-textarea reflects field-id to a real id only after it
  // hydrates, so look it up fresh at use-time rather than caching
  // a reference before hydration has necessarily finished.
  document.getElementById('open-guarded').addEventListener('click', () => m.show());
  document.getElementById('guarded-save').addEventListener('click', () => {
    savedNote = document.getElementById('guarded-note').value;
    m.close();
  });
  // Runs for a backdrop click and for close(). Escape always closes (WCAG 2.1.2).
  m.addEventListener('aegovModalBeforeClose', (e) => {
    if (document.getElementById('guarded-note').value !== savedNote) {
      e.preventDefault();
      window.alert('You have unsaved changes. Choose Save to keep them or Cancel to discard them.');
    }
  });
  document.getElementById('guarded-cancel').addEventListener('click', () => {
    document.getElementById('guarded-note').value = savedNote;
    m.close();
  });
</script>

RTL support

On open, aegov-modal resolves its direction at its declared position — from the nearest dir ancestor, else <html dir> — and pins it as dir on the host until it closes. A dir="rtl" on a wrapper therefore still applies even if the modal has to move out of a trapping ancestor. Setting dir="rtl" directly on aegov-modal, as below, also works and is never overwritten.

حذف الطلب

حذف هذا الطلب؟

سيتم حذف مسوّدة طلبك لخدمة تجديد الهوية الإماراتية بشكل دائم. لا يمكن التراجع عن هذا الإجراء.

إلغاء حذف
<aegov-button id="open-rtl">حذف الطلب</aegov-button>

<aegov-modal id="rtl-modal" dir="rtl" label="حذف هذا الطلب؟">
  <h3 id="rtl-title" class="font-bold text-xl mb-2">حذف هذا الطلب؟</h3>
  <p class="text-base mb-6">سيتم حذف مسوّدة طلبك لخدمة تجديد الهوية الإماراتية بشكل دائم. لا يمكن التراجع عن هذا الإجراء.</p>
  <div class="flex justify-end gap-3">
    <aegov-button variant="secondary" id="rtl-cancel">إلغاء</aegov-button>
    <aegov-button variant="danger" id="rtl-confirm">حذف</aegov-button>
  </div>
</aegov-modal>

<script>
  const m = document.getElementById('rtl-modal');
  document.getElementById('open-rtl').addEventListener('click', () => m.show());
  document.getElementById('rtl-cancel').addEventListener('click', () => m.close());
  document.getElementById('rtl-confirm').addEventListener('click', () => m.close());
</script>

Usage

Here is the recommended usage of a modal on any website or application. These are recommended usage guides, however the developer must use the best practices of web and application development to accurately use this component.

Always give the dialog an accessible name
Set label to the text of the visible heading inside the modal. Without it, screen readers announce only "dialog".

Set dir="rtl" on the page, a wrapper, or aegov-modal itself for an RTL dialog
On open, the modal pins the direction it resolves at its declared position as dir on the host (removed again on close), so a wrapper's dir="rtl" still applies even if the modal has to move out of a trapping ancestor. A dir you set on aegov-modal yourself is never overwritten.

Listen for aegovModalBeforeClose to guard unsaved changes
Call event.preventDefault() inside the handler to keep the modal open, then prompt the user in place. This covers a backdrop click and a programmatic close() — Escape always closes the modal (WCAG 2.1.2); see the Keyboard support notes.

Pair overlay-opacity="dark" with a danger-styled confirm button for destructive actions
The heavier backdrop visually matches the weight of an irreversible action like a delete confirmation.

Don't rely on aegovModalBeforeClose to stop Escape from closing the dialog
Escape always closes the modal without emitting aegovModalBeforeClose at all, per WCAG 2.1.2 — a handler that calls preventDefault() protects against a backdrop click or a programmatic close, but never against Escape.

Don't nest a multi-step form inside a modal
Keep dialog content to a single task. A workflow that needs several steps is better served by a dedicated page or a wizard, where progress and back-navigation are easier to reason about than inside an overlay.

API reference

The following are the properties and references of a modal

aegov-modal properties

AttributeDescriptionTypeDefault

field-id

DOM id for the inner dialog content element. Auto-generated if omitted.

string

auto-generated

is-open

Whether the modal is currently visible. Can be set from the start — the component correctly opens (focus trap, scroll lock, Escape listener) even when declared open on load, not only on a later change.

boolean

false

is-close-with-overlay

Whether clicking the backdrop closes the modal.

boolean

true

is-prevent-scroll

Lock page scroll while the modal is open. Reference-counted, so a second open overlay keeps scroll locked correctly until the last one closes.

boolean

true

overlay-opacity

Backdrop dim level.

light | base | dark

base

position

Where the dialog docks in the viewport.

center | top | bottom

center

size

Fixed panel width (300px, 600px, 800px or 1140px, capped at the viewport width) plus padding and corner-radius scale, mirroring aegov-card's size tokens.

sm | base | lg | xl

base

is-padding

Apply inner content padding. Turn off for full-bleed content such as a banner image.

boolean

true

label

Accessible name for the dialog, set as aria-label on the inner role="dialog" element. Use the text of the visible heading inside the modal so the spoken name matches what sighted users see.

string

—

Slots

SlotDescription

(default)

The dialog content — heading, body text, and any action buttons. There is no separate header/body/footer slot; compose the layout with regular HTML inside the default slot.

Events

EventDetailDescription

aegovModalOpen

void

Fired synchronously when opening starts, before focus moves into the dialog.

aegovModalClose

void

Fired once the modal has finished closing and focus has been returned to the trigger.

aegovModalBeforeClose

void

Cancellable — call event.preventDefault() to keep the modal open (for example, unsaved form data). Fired for a backdrop click or a programmatic close() call. Not fired for toggle(), for setting is-open to false, or for Escape — see the Keyboard support note below.

aegovModalBackdropClick

MouseEvent

Fired for every click that reaches the full-viewport container — including clicks inside the dialog panel — regardless of whether is-close-with-overlay allows the click to close it. The modal closes only when the click lands outside the panel.

aegovModalEscapePress

KeyboardEvent

Fired when Escape is pressed while the modal is the topmost open overlay. Cancelling it has no effect — Escape always closes.

Methods

MethodReturnsDescription

show()

Promise<void>

Open the modal.

close()

Promise<void>

Close the modal (fires the cancellable aegovModalBeforeClose first).

toggle()

Promise<void>

Flip the open state. Closing this way skips aegovModalBeforeClose and the exit animation.

Pass-through attributes

AttributeNotesForwards to

aria-label, aria-labelledby, aria-describedby

Forwarded to the inner role="dialog" element, but the attribute also stays on the host, where it is not valid ARIA. aria-labelledby and aria-describedby cannot reach an id in your page from inside the shadow root, so they have no effect. Use the label prop instead.

inner dialog element

other aria-*, data-*

Forwarded to the inner role="dialog" element.

inner dialog element

id

Not forwarded. Use field-id — it sets the real id of the dialog content element.

n/a

class, style

Applied to the host element. Limited reach — the backdrop and dialog markup live inside a real shadow root, so page-level CSS cannot style them directly.

host

Notes

The following notes are applicable of this component.

Built-in accessibility

  • The dialog renders with role="dialog" and aria-modal="true" — confirmed reaching the real inner dialog element regardless of how the modal was opened.
  • Focus moves to the first focusable element inside the dialog as soon as it opens — confirmed for both a declarative is-open="true" and an imperative show() call. If the dialog has no focusable children, the dialog container itself receives focus so assistive technology still announces it.
  • Tab/Shift + Tab correctly wrap focus within just the dialog's own focusable elements in both directions, without ever reaching the page behind it.
  • On close, focus is returned to the exact element that had focus when the dialog opened.
  • Page scroll is locked while the dialog is open (reference-counted, so a second, nested overlay keeps scroll locked until the last one closes) and correctly released on close.
  • The modal stays where it is declared. Only while it is open, and only if an ancestor would trap its fixed-position layers (a transform, filter, stacking context, or hidden ancestor), it moves out of that ancestor — to document.body as a last resort — and returns on close. Its z-index comes from a shared overlay manager, so it stacks above the page and above any overlay opened before it.
  • On open, the direction resolved at its declared position is pinned as dir on the host, so the dialog keeps an ancestor's dir="rtl" even after a move.

Developer responsibilities

  • Always set the label prop — without it, the component logs a console warning, and screen readers announce only "dialog". Do not rely on aria-labelledby on the host: the dialog lives in a shadow root and cannot resolve an id in your page.
  • Make the heading state the action plainly, so the name alone conveys it. aria-describedby on the host has the same shadow-root limitation and does not link supporting body text.
  • Listen for aegovModalBeforeClose and call preventDefault() when the user has unsaved changes, then prompt them in place rather than silently discarding data.
  • For an RTL dialog, set dir="rtl" on the page, an ancestor, or aegov-modal itself — the modal pins the direction it resolves on open, so any of these is kept.

Keyboard support

Key Function
Enter, Space Activate the focused button or control.
Tab Move focus to the next focusable element inside the dialog, wrapping to the first after the last.
Shift + Tab Move focus to the previous focusable element, wrapping to the last after the first.
Escape Close the dialog immediately and return focus to the trigger. With overlays stacked, only the top one responds to Escape and Tab. Escape always closes and does not fire the cancellable aegovModalBeforeClose event — confirmed live with a listener that calls preventDefault() on aegovModalBeforeClose, which stops a backdrop click from closing the dialog but has no effect on Escape. Build any "are you sure you want to lose this?" guard around the backdrop-click and programmatic-close paths, not around blocking Escape itself.