Modal
A native modal built with <dialog> and declarative command buttons. The browser handles the top layer, focus behavior, modal state, and closing actions without custom JavaScript.
0 KB JavaScript
~40 CSS lines
command declarative controls
dialog native modal
Preview
The code
The markup
<button type="button" commandfor="native-dialog" command="show-modal">
Open modal
</button>
<dialog id="native-dialog" class="modal" aria-labelledby="modal-title">
<div class="modal-inner">
<button class="modal-close" type="button" commandfor="native-dialog" command="request-close" aria-label="Close">×</button>
<h2 id="modal-title">Modal title</h2>
<!-- content -->
<button type="button" commandfor="native-dialog" command="close" value="cancel">Cancel</button>
<button type="button" commandfor="native-dialog" command="close" value="ok">OK</button>
</div>
</dialog>CSS mechanisms
/* ----------------------------------------------------------------------------
* Modal
* ---------------------------------------------------------------------------- */
/**
* Native `<dialog>` panel.
*
* The dialog element stays visually neutral and acts as the modal
* container in the top layer. Use `.modal-inner` for the visible
* surface and custom presentation styles.
*/
.modal {
background: transparent;
border: none;
margin: auto;
overflow: visible;
padding: 0;
/* add your styles here */
}
/**
* Visible modal content wrapper.
* Use this element for custom spacing, surface, border, shadow, etc.
*/
.modal-inner {
/* add your styles here */
}
/* ----------------------------------------------------------------------------
* Modal close button
* ---------------------------------------------------------------------------- */
/**
* Explicit close control.
*/
.modal-close {
/* add your styles here */
}
/* ----------------------------------------------------------------------------
* Modal Animation (Top Layer)
* ---------------------------------------------------------------------------- */
/**
* Closed-state styling for the modal panel.
*
* Uses the new `transition-behavior` / `allow-discrete` mechanism
* so the `display: none → block` and top-layer `overlay` changes
* animate smoothly instead of snapping. Without `allow-discrete`,
* the modal would teleport in and out.
*/
.modal {
opacity: 0;
transition:
opacity 240ms,
overlay 240ms allow-discrete,
display 240ms allow-discrete;
}
/**
* Open state: full opacity. Selectors cover
* modal dialogs (`:open`), and the
* fallback `[open]` attribute selector for legacy dialog support.
*/
.modal:open,
dialog.modal[open] {
opacity: 1;
}
/**
* Initial keyframe override: required so the entrance transition
* runs on the first open after the discrete `display` change.
*/
@starting-style {
.modal:open,
dialog.modal[open] {
opacity: 0;
}
}
/**
* Backdrop transition hook.
*
* Add a background here if you want a visible overlay.
*/
.modal::backdrop {
opacity: 0;
transition:
opacity 240ms,
overlay 240ms allow-discrete,
display 240ms allow-discrete;
}
/**
* Open backdrop becomes visible.
*/
.modal:open::backdrop,
dialog.modal[open]::backdrop {
opacity: 1;
}
/**
* Initial keyframe override for the backdrop, same reason as above.
*/
@starting-style {
.modal:open::backdrop,
dialog.modal[open]::backdrop {
opacity: 0;
}
}Benefits
-
01.
Semantic markup
Purpose-built modal structure with
<dialog>, explicit controls, and accessible labelling viaaria-labelledby. -
02.
Native modal behavior
The browser handles the top layer, backdrop, focus behavior, inert page content, and
Escdismissal. -
03.
No custom JavaScript
Buttons can open and close the modal declaratively with
commandforandcommand. -
04.
Return values
Closing buttons can pass a result with
value, exposed through the dialog’s nativereturnValue.
Current limitations
-
01.
Command support
commandforis newer than<dialog>. Without it, the modal element still exists, but declarative open and close buttons need a fallback. -
02.
Animation support
Smooth entry and exit transitions depend on
@starting-style,allow-discrete,display, andoverlay. Without them, the modal still works but may appear or disappear instantly. -
03.
Focus decisions
The browser provides modal focus behavior, but authors still need to choose sensible labels, close controls, and optional
autofocusplacement.