MODALLY
It's your trusty nested modals ally.
Try me! Video? Real Video? Image? ↓Your trusty nested modal ally — simple, framework-agnostic modal dialogs with accessible focus management.
Modally turns any hidden element into an accessible modal dialog. It follows the WAI-ARIA Authoring Practices dialog pattern: focus moves into the dialog on open, is trapped inside while open, and returns to the trigger on close. Esc closes the top-most modal.
Give your content an id and a link that targets it. Modally wraps the content in an accessible dialog for you.
<div id="hello" hidden><h1>Hello world!</h1></div>
<a href="#hello" target="_modal">Open</a>
const modally = new Modally()
modally.add('hello')
Prefer HTML? Use the <modally-dialog> custom element. It uses no shadow DOM, so your own CSS styles the content directly. Every attribute becomes a modal option.
<modally-dialog id="greeting" max-width="600">
<h1>Hey there!</h1>
<p>Styled by your own stylesheet — no piercing needed.</p>
</modally-dialog>
<a href="#greeting" target="_modal">Open</a>
Open a modal from inside another — Modally tracks the stack automatically and blurs the parent. No extra code.
<a href="#child" target="_modal">Open a nested modal</a>
Pass video: true and a data-video URL. YouTube, Vimeo and raw video files are detected and embedded automatically, with optional autoplay.
modally.add('video-modal', { video: true })
<a href="#video-modal" target="_modal"
data-video="https://www.youtube.com/watch?v=u9QJo5fBADE">Watch</a>
Pass image: true and a data-image URL to open a responsive lightbox.
modally.add('image-modal', { image: true })
<a href="#image-modal" target="_modal"
data-image="/photo.jpg" data-width="800" data-height="1192">View</a>
| Option | Default | Description |
|---|---|---|
maxWidth | 'none' | Max width of the modal window (px). |
disableScroll | true | Block body scroll while open. |
verticalAlign | 'middle' | middle, top or bottom. |
closeOthers | false | Close other open modals on open. |
video / image | false | Enable video embed / image lightbox. |
See the README for the full list. Options can also be set via modally-<option> attributes on the element.
Modally dispatches lifecycle events on document: modally:open, modally:opened, modally:close, modally:closed and modally:added — each with the modal instance in event.detail.
document.addEventListener('modally:opened', (e) => {
console.log(e.detail.id)
})
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Yes I am this lazy.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Yes I am this lazy.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Yes I am this lazy.
This modal is declared with the <modally-dialog> custom element. No shadow DOM — your own CSS styles it directly.