Panel events
Panel dispatches custom events.
Panel fires its custom events from the [data-panel] element. They bubble, so listen on the panel itself or on document.
Available events
Fires just before the panel opens. This is the hook for loading content: hand a promise to e.detail.promise and the open waits until it settles.
Detail:
- trigger: HTMLElement | null
- The element that initiated the open, or
nullif opened programmatically. - signal: AbortSignal
- Aborted if the open is cancelled before the promise resolves.
- promise: Promise | null
- Set this to a promise to delay the open.
panelEl.addEventListener('panel:beforeopen', e => {
e.detail.promise = fetch('/api/content', { signal: e.detail.signal })
.then(r => r.text())
.then(html => { panelEl.querySelector('.panel-wrapper').innerHTML = html; });
});
Or skip the event and use the convenience method:
panel.onBeforeOpen(async (el, signal) => {
const html = await fetch('/content', { signal }).then(r => r.text());
el.querySelector('.panel-wrapper').innerHTML = html;
}, { once: true });
Fires when the open animation starts.
Detail:
- trigger: HTMLElement | null
- The element that initiated the open, or
nullif opened programmatically.
Fires once the panel is fully open and the transition has finished.
Detail:
- trigger: HTMLElement | null
- The element that initiated the open, or
nullif opened programmatically.
Fires when the close animation starts.
Detail:
- trigger: HTMLElement | null
- The element that last opened the panel, or
nullif closed programmatically.
Fires once the panel is fully closed and the transition has finished.
Detail:
- trigger: HTMLElement | null
- The element that last opened the panel, or
nullif closed programmatically.
Fires when the panel stops being a collapsible disclosure and becomes plain expanded content, or the other way round. See the static option. It does not fire on init: that is the panel's starting state, not a change.
Detail:
- static: boolean
truewhen the panel just became static,falsewhen it became collapsible again (which lands it closed).
Examples
Open this panel and the video plays; close it and the video pauses and rewinds to the start. The events are logged to the console so you can watch the order they fire in.
const panel = document.getElementById('my-panel');
const video = panel.querySelector('video');
panel.addEventListener('panel:opened', () => video.play());
panel.addEventListener('panel:closing', () => video.pause());
panel.addEventListener('panel:closed', () => { video.currentTime = 0; });