What the popup component does
When a user clicks an element with data-anim-popup-link="popupName",
the component finds the corresponding popup (by name) and opens it.
-
Adds
data-anim-popup-opento the<body>. - Locks page scroll (can be disabled in component settings if your build supports it).
- Moves focus into the popup and remembers the previously focused element.
-
Closes by: close button (
data-anim-popup-close), overlay click, or ESC.
src/components/layout/popup/
Include the popup markup
Add the popup HTML to your page using <include>.
The component file already contains a base popup example.
<include src="@components/layout/popup/popup.html"></include>
In popup.html each modal root has the attribute
data-anim-popup="popupName". That value is the popup name.
You can create multiple popups — just give each one a unique name.
Open and close a popup
To open a popup, add data-anim-popup-link to any trigger element and set the popup
name.
<a href="#" data-anim-popup-link="popup">Open Popup</a>
To close, use an element with data-anim-popup-close inside the popup.
Overlay click and ESC are supported by default.
Open popup via URL hash
The component can open a popup via hash in the URL. For example, this will open the popup named
popup:
http://localhost:1111/#popup
YouTube video inside popup
You can open a YouTube video in a popup with autoplay. Add
data-anim-popup-youtube to the popup trigger and pass the video ID.
https://www.youtube.com/shorts/PQdR1VxI6Y8,
the ID is PQdR1VxI6Y8.
<a
href="#"
data-anim-popup-link="popup"
data-anim-popup-youtube="PQdR1VxI6Y8"
>Watch video</a>
If your popup is opened by hash or programmatically (not by a click trigger), you can attach the video ID directly to the popup root:
<div
data-anim-popup="popup"
data-anim-popup-youtube="PQdR1VxI6Y8"
aria-hidden="true"
class="popup"
>
...
</div>
By default, the video is injected into the element marked with
data-anim-popup-content (replacing its content).
If you need a different placement, create a dedicated element and mark it with
data-anim-popup-youtube-place.
Methods and events
Methods
You can control popups programmatically via a global object:
window.animPopup.
window.animPopup.open('popup');
Events
The popup system emits events on document so you can hook into lifecycle
moments.
beforePopupOpen— before openafterPopupOpen— after openbeforePopupClose— before closeafterPopupClose— after close
document.addEventListener("afterPopupOpen", function (e) {
// Current popup element:
const currentPopup = e.detail.popup;
});
Download
Want popups that work well by default: focus, scroll lock, overlay click, ESC close, and optional YouTube autoplay? Download the template and build faster.