Components Modal
CSS-only transitions NEW #
A <dialog> opened with dialog.showModal() fades in and its <article> zooms in, without JS classes.
Pico's modal JS (.modal-is-open, .modal-is-opening, .modal-is-closing) still works on top of it.
dialog.close()closes it at once, as before.<dialog open>in markup and non-modaldialog.show()aren't animated.- Browsers without
@starting-style, and reduced motion, open it at once.
dialog:modal {
transition: opacity var(--pico-transition);
> article {
transition: transform var(--pico-transition);
}
}
@starting-style {
dialog:modal {
opacity: 0;
> article {
transform: scale(0.96);
}
}
}
Open and close without JS NEW #
The Invoker Commands API lets a <button> open and close a dialog with attributes only. Give the button a command and a commandfor that holds the dialog's id:
<button command="show-modal" commandfor="my-dialog">Open</button>
<dialog id="my-dialog" closedby="any">
<article>
<header>
<button aria-label="Close" rel="prev" command="close" commandfor="my-dialog"></button>
<h3>Title</h3>
</header>
<p>Content.</p>
<footer>
<button class="secondary" command="request-close" commandfor="my-dialog">Cancel</button>
<button command="close" commandfor="my-dialog" value="confirm">Confirm</button>
</footer>
</article>
</dialog>
show-modalis the same asdialog.showModal(), so the transitions above apply.closeis the same asdialog.close(). It closes at once, and the button'svaluebecomesdialog.returnValue.request-closefires acancelevent first, as Escape does, so a listener can stop it withpreventDefault().- The button must be a
<button>(or<input type="button">), not inside a<form>that it would submit, unless it hastype="button". - The dialog gets a
commandevent, andevent.sourceis the button that invoked it. - Browsers without support ignore the attributes and nothing opens. Add a
clicklistener that callsshowModal()andclose()as a fallback if you need them.
Click outside to close NEW #
<dialog closedby="any"> closes when the user clicks outside its box. The browser does this, so the click itself needs no JS.
The dim and blur are on ::backdrop, and they fade in over --pico-transition.
closedby="closerequest" and closedby="none" get the same ::backdrop and box styles while keeping their native close behavior; dialogs without closedby are unchanged.
- Escape and an outside click both close the dialog when
closedby="any". An outside click fires thecloseevent once. - The box uses Pico's article widths: the viewport width minus
--pico-spacingon each side below 576px, 510px from 576px, and 700px from 768px. - To change the width, set
widthon the dialog with a selector more specific thandialog:modal[closedby](or equally specific and loaded after Blades), such asdialog:modal[closedby].wide. Settingmax-widthon the article leaves the box as it is, and a click on the space inside the box does not close the dialog. - Browsers without
closedbysupport are expected to keep the dialog open on an outside click. The box and backdrop styles still apply, and the fade-in also needs@starting-style. - If your page locks scroll with
.modal-is-open, clear the class in acloselistener too, since the browser can close the dialog without your buttons.
How it works
/open-sveti/blades/refs/heads/main/src/blades/components/_modal-modern.css :
dialog:modal[closedby] {
width: calc(100% - var(--pico-spacing) * 2);
max-width: none;
min-width: 0;
height: fit-content;
min-height: 0;
max-height: calc(100% - var(--pico-spacing) * 2);
margin: auto;
padding: 0;
background: transparent;
-webkit-backdrop-filter: none;
backdrop-filter: none;
@media (min-width: 576px) {
width: min(510px, calc(100% - var(--pico-spacing) * 2));
}
@media (min-width: 768px) {
width: min(700px, calc(100% - var(--pico-spacing) * 2));
}
> article {
width: 100%;
max-width: none;
max-height: calc(100vh - var(--pico-spacing) * 2);
max-height: calc(100dvh - var(--pico-spacing) * 2);
margin: 0;
}
&::backdrop {
background: var(--pico-modal-overlay-background-color);
-webkit-backdrop-filter: var(--pico-modal-overlay-backdrop-filter);
backdrop-filter: var(--pico-modal-overlay-backdrop-filter);
transition:
background-color var(--pico-transition),
-webkit-backdrop-filter var(--pico-transition),
backdrop-filter var(--pico-transition);
@starting-style {
background-color: transparent;
-webkit-backdrop-filter: none;
backdrop-filter: none;
}
@media (prefers-reduced-motion: reduce) {
transition: none;
}
}
}
Syntax #
The classic modal component with graceful spacings across devices and viewports, using the semantic HTML tag <dialog>.
Modals are built with <dialog> as a wrapper and <article> for the modal content.
Inside <header> <button rel="prev"> is defined to float: right; allowing a close icon to be top aligned with a title.
<dialog open>
<article>
<header>
<button aria-label="Close" rel="prev"></button>
<p>
<strong>🗓️ Thank You for Registering!</strong>
</p>
</header>
<p>
We're excited to have you join us for our
upcoming event. Please arrive at the museum
on time to check in and get started.
</p>
<ul>
<li>Date: Saturday, April 15</li>
<li>Time: 10:00am - 12:00pm</li>
</ul>
</article>
</dialog>Inside <footer>, the content is right aligned by default.
<dialog open>
<article>
<h2>Confirm Your Membership</h2>
<p>
Thank you for signing up for a membership!
Please review the membership details below:
</p>
<ul>
<li>Membership: Individual</li>
<li>Price: $10</li>
</ul>
<footer>
<button class="secondary">
Cancel
</button>
<button>Confirm</button>
</footer>
</article>
</dialog>Demo#
Toggle a modal by clicking the button below.
Pico does not include JavaScript code. You need to implement your JS to interact with modals.
As a starting point, you can look at the HTMLDialogElement or the advanced examples below:
To open a modal, add the open attribute to the<dialog> container.
Utilities#
Modals come with 3 utility classes.
These classes are not available in the class-less version.
.modal-is-open prevents any scrolling and interactions below the modal.
.modal-is-opening brings an opening animation.
.modal-is-closing brings a closing animation.