Modal
Focus attention on one task or message with a modal. Compare default, large, and required-action dialogs.
This component requires the following JavaScript plugin:
Examples
Default
Mobile
320pxMobile Lg
480pxTablet
640pxDesktop
1024pxLarge
Mobile
320pxMobile Lg
480pxTablet
640pxDesktop
1024pxForced Action
Mobile
320pxMobile Lg
480pxTablet
640pxDesktop
1024pxComponent API
Initialization
import { modalInit } from '@uswds-tailwind/compat/modal'
modalInit()Run after the markup exists. To retain an instance, use Modal.getOrCreateInstance(root, options).
The instance belongs to the root. Initialization moves the root to the end of the document body.
Anatomy
Set authored attributes in your HTML. The component manages generated attributes. Types describe HTML attribute values.
root
Required modal container. Initialization moves it to the document body.
| Attribute | Type | Description |
|---|---|---|
data-scope |
"modal" |
Authored. Identifies the component scope. |
data-part |
"root" |
Authored. Identifies this part. |
data-value |
string |
Authored. Unique value used by external triggers to target this modal. |
data-force-action |
boolean-like attribute |
Authored. Suppresses Escape and outside dismissal. Bare attribute is true; omission or "false" is false. The forceAction option takes precedence. |
trigger
Button outside the root for user activation. Several triggers may share a target.
| Attribute | Type | Description |
|---|---|---|
data-scope |
"modal" |
Authored. Identifies the component scope. |
data-part |
"trigger" |
Authored. Identifies this part. |
data-target |
string |
Authored. Matches the root’s data-value. |
aria-controls |
ID reference |
Generated. ID of the associated content. |
backdrop
Required background overlay inside the root.
| Attribute | Type | Description |
|---|---|---|
data-part |
"backdrop" |
Authored. Identifies this part. |
data-state |
"open" | "closed" |
Generated. "open" or "closed"; use for styling. |
hidden |
boolean attribute |
Generated. Present when closed. |
positioner
Required wrapper around the dialog content.
| Attribute | Type | Description |
|---|---|---|
data-part |
"positioner" |
Authored. Identifies this part. |
content
Required dialog content.
| Attribute | Type | Description |
|---|---|---|
data-part |
"content" |
Authored. Identifies this part. |
aria-label |
string |
Authored. Explicit accessible name; takes precedence over the title relationship. The aria-label option overrides this attribute. |
data-state |
"open" | "closed" |
Generated. "open" or "closed"; use for styling. |
hidden |
boolean attribute |
Generated. Present when closed. |
role |
"dialog" |
Generated. Accessibility role. |
tabindex |
integer |
Generated. "-1" for programmatic focus. |
aria-labelledby |
ID references |
Generated. Title relationship when no explicit label is supplied. |
aria-describedby |
ID references |
Generated. Description relationship, when present. |
title
Visible heading used to name the dialog. Required unless an explicit accessible label is supplied.
| Attribute | Type | Description |
|---|---|---|
data-part |
"title" |
Authored. Identifies this part. |
description
Optional explanatory text associated with the dialog.
| Attribute | Type | Description |
|---|---|---|
data-part |
"description" |
Authored. Identifies this part. |
close-trigger
Button inside the content for an explicit action. Keep an action available in forced-action dialogs.
| Attribute | Type | Description |
|---|---|---|
data-part |
"close-trigger" |
Authored. Identifies this part. |
Configuration
Pass these JavaScript options to the instance constructor or factory. Authored configuration attributes are read during construction.
| Option | Description |
|---|---|
id |
Instance identifier; defaults to root data-value, then its ID, then a generated value. |
forceAction |
boolean. Overrides the root’s data-force-action; defaults to the attribute value. |
aria-label |
string. Overrides the label authored on the content. |
Methods
| Method | Returns | Description |
|---|---|---|
open() |
Promise<void> |
Requests opening. |
close() |
Promise<void> |
Requests closing. |
destroy() |
void |
Stops effects and clears the instance lookup. |
import { Modal } from '@uswds-tailwind/compat/modal'
const modal = Modal.getOrCreateInstance( '[data-scope="modal"][data-part="root"]',)
await modal.open()Promises settle the queued update, not CSS transitions. The component starts closed and manages its own state. It does not support open, defaultOpen, ids, or onOpenChange options.
Retain and remove the root during teardown. destroy() does not restore its original parent or HTML.
See initialization and cleanup.
Accessibility
- Supply a meaningful accessible name and sensible actions.
- The runtime manages focus trapping, focus return, background accessibility, and scroll locking.
- Escape and outside interaction dismiss ordinary dialogs. Forced-action dialogs require an explicit action.
aria-modalis not generated by this implementation.