Selection summary popover in HTML, CSS and vanilla JS
A copy-paste Selection summary popover in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="dcl-popover-selection-summary" data-dcl-scheme="light">
<h2>Selection summary popover</h2>
<fieldset data-stack><legend>Include in export</legend><label><input type="checkbox" value="Notes" checked> Notes</label><label><input type="checkbox" value="Photos"> Photos</label><label><input type="checkbox" value="Timeline"> Timeline</label></fieldset><button type="button" popovertarget="dcl-popover-selection-summary-selection" aria-controls="dcl-popover-selection-summary-selection" aria-expanded="false">Review selection</button><section id="dcl-popover-selection-summary-selection" popover="manual" aria-labelledby="dcl-popover-selection-summary-selection-title"><div data-summary-layout><div data-summary-scroll role="region" aria-labelledby="dcl-popover-selection-summary-selection-title"><h3 id="dcl-popover-selection-summary-selection-title" tabindex="-1">Export selection</h3><ul data-summary></ul><p data-empty hidden>No sections selected.</p></div><div data-actions><button type="button" data-dismiss>Close</button></div></div></section>
<script>
(() => {
const root = document.currentScript.closest('.dcl-popover-selection-summary');
const instance = Array.from(document.getElementsByClassName('dcl-popover-selection-summary')).indexOf(root);
if (instance) {
const suffix = '--' + instance;
const attributes = ['id','aria-labelledby','aria-describedby','aria-controls','for','name','popovertarget'];
for (const element of root.querySelectorAll('*')) {
for (const attr of attributes) {
if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr).split(/\s+/).map(value => value + suffix).join(' '));
}
const href = element.getAttribute('href');
if (href && href.startsWith('#')) element.setAttribute('href', href + suffix);
}
}
const media = matchMedia('(prefers-color-scheme: dark)');
const theme = () => { root.dataset.dclScheme = media.matches ? 'dark' : 'light'; };
media.addEventListener('change', theme);
theme();
const key = Symbol.for('dcl.popover-selection-summary.open');
const openSummaries = document[key] ??= new Set();
for (const panel of root.querySelectorAll('[popover]')) {
const trigger = root.querySelector('[popovertarget="' + panel.id + '"]');
const place = () => {
const origin = trigger.getBoundingClientRect();
const edge = 8;
const gap = 6;
const limit = innerHeight - edge;
const below = Math.max(edge, origin.bottom + gap);
const aboveSpace = Math.max(0, Math.min(limit, origin.top - gap) - edge);
const belowSpace = Math.max(0, limit - below);
panel.style.setProperty('--summary-height', Math.max(aboveSpace, belowSpace) + 'px');
panel.style.left = Math.max(edge, Math.min(origin.left, innerWidth - panel.offsetWidth - edge)) + 'px';
panel.style.top = (aboveSpace > belowSpace ? edge : below) + 'px';
};
const dismissOnEscape = event => {
if (event.key !== 'Escape' || event.defaultPrevented) return;
const open = [...openSummaries].filter(summary => summary.matches(':popover-open'));
const focused = document.activeElement?.closest('.dcl-popover-selection-summary');
const target = open.find(summary => summary.closest('.dcl-popover-selection-summary') === focused) ?? open.at(-1);
if (target !== panel) return;
event.preventDefault();
const restore = panel.contains(document.activeElement);
panel.hidePopover();
if (restore) trigger.focus();
};
panel.addEventListener('toggle', event => {
const open = event.newState === 'open';
trigger.setAttribute('aria-expanded', String(open));
if (open) {
place();
openSummaries.add(panel);
document.addEventListener('keydown', dismissOnEscape);
window.addEventListener('resize', place);
window.addEventListener('scroll', place, true);
} else {
openSummaries.delete(panel);
document.removeEventListener('keydown', dismissOnEscape);
window.removeEventListener('resize', place);
window.removeEventListener('scroll', place, true);
}
});
panel.querySelector('[data-dismiss]').addEventListener('click', () => { panel.hidePopover(); trigger.focus(); });
}
const update = () => {
const summary = root.querySelector('[data-summary]');
const choices = [...root.querySelectorAll('input:checked')];
summary.replaceChildren(...choices.map(choice => {
const item = document.createElement('li');
item.textContent = choice.value;
return item;
}));
root.querySelector('[data-empty]').hidden = choices.length > 0;
};
root.querySelector('fieldset').addEventListener('change', update);
update();
})();
</script>
</section>
<style>
.dcl-popover-selection-summary { box-sizing: border-box; max-width: 58rem; padding: clamp(.75rem, 3vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui,sans-serif; overflow-wrap: anywhere; }
.dcl-popover-selection-summary *, .dcl-popover-selection-summary *::before, .dcl-popover-selection-summary *::after { box-sizing: border-box; }
.dcl-popover-selection-summary h2, .dcl-popover-selection-summary h3, .dcl-popover-selection-summary p, .dcl-popover-selection-summary ul, .dcl-popover-selection-summary ol, .dcl-popover-selection-summary figure { margin: 0; }
.dcl-popover-selection-summary h2 { font-size: 1.25rem; margin-bottom: .875rem; }
.dcl-popover-selection-summary h3 { font-size: 1rem; }
.dcl-popover-selection-summary li + li, .dcl-popover-selection-summary p + p { margin-top: .375rem; }
.dcl-popover-selection-summary ul, .dcl-popover-selection-summary ol { padding-inline-start: 1.35rem; }
.dcl-popover-selection-summary a { text-underline-offset: .18em; }
.dcl-popover-selection-summary a:hover { text-decoration-thickness: .18em; }
.dcl-popover-selection-summary :focus-visible { outline-style: solid; outline-width: 3px; outline-offset: 2px; }
.dcl-popover-selection-summary button, .dcl-popover-selection-summary summary, .dcl-popover-selection-summary input, .dcl-popover-selection-summary select { font: inherit; max-width: 100%; }
.dcl-popover-selection-summary button, .dcl-popover-selection-summary input, .dcl-popover-selection-summary select { padding: .35rem .65rem; border: 1px solid; border-radius: .375rem; }
.dcl-popover-selection-summary button, .dcl-popover-selection-summary summary, .dcl-popover-selection-summary select { cursor: pointer; }
.dcl-popover-selection-summary button:active, .dcl-popover-selection-summary summary:active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-popover-selection-summary button:disabled, .dcl-popover-selection-summary button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; }
.dcl-popover-selection-summary [hidden] { display: none !important; }
.dcl-popover-selection-summary [data-line], .dcl-popover-selection-summary [data-actions] { display: flex; gap: .5rem .75rem; align-items: center; flex-wrap: wrap; }
.dcl-popover-selection-summary [data-grid] { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(100%,9rem),1fr)); gap: .75rem; }
.dcl-popover-selection-summary [data-stack], .dcl-popover-selection-summary [data-targets] { display: grid; gap: .75rem; }
.dcl-popover-selection-summary [data-targets] { border-top: 1px solid; padding-top: .75rem; margin-top: .75rem; }
.dcl-popover-selection-summary [data-card], .dcl-popover-selection-summary [data-status], .dcl-popover-selection-summary [data-panel] { padding: .75rem; border: 1px solid; border-radius: .5rem; }
.dcl-popover-selection-summary [data-status] { margin-top: .75rem; }
.dcl-popover-selection-summary [data-status]:empty { border-width: 0; padding: 0; margin: 0; }
.dcl-popover-selection-summary [aria-current] { font-weight: 750; }
.dcl-popover-selection-summary dialog, .dcl-popover-selection-summary [popover] { max-height: calc(100dvh - 2rem); max-width: calc(100% - 2rem); width: min(36rem,100%); border: 1px solid; border-radius: .75rem; padding: 1rem; overflow: auto; }
.dcl-popover-selection-summary dialog h3, .dcl-popover-selection-summary [popover] h3 { margin-bottom: .5rem; }
.dcl-popover-selection-summary dialog [data-actions], .dcl-popover-selection-summary [popover] [data-actions] { margin-top: .75rem; }
.dcl-popover-selection-summary svg { display: block; width: 100%; height: auto; }
.dcl-popover-selection-summary[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-popover-selection-summary[data-dcl-scheme="light"] a { color: #005c62; }
.dcl-popover-selection-summary[data-dcl-scheme="light"] button, .dcl-popover-selection-summary[data-dcl-scheme="light"] input, .dcl-popover-selection-summary[data-dcl-scheme="light"] select, .dcl-popover-selection-summary[data-dcl-scheme="light"] summary, .dcl-popover-selection-summary[data-dcl-scheme="light"] [data-card], .dcl-popover-selection-summary[data-dcl-scheme="light"] [data-status], .dcl-popover-selection-summary[data-dcl-scheme="light"] [data-panel] { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-popover-selection-summary[data-dcl-scheme="light"] button:hover, .dcl-popover-selection-summary[data-dcl-scheme="light"] summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-popover-selection-summary[data-dcl-scheme="light"] dialog, .dcl-popover-selection-summary[data-dcl-scheme="light"] [popover] { color: #11292f; background-color: #ffffff; border-color: #697f84; }
.dcl-popover-selection-summary[data-dcl-scheme="light"] :focus-visible { outline-color: #005c62; }
.dcl-popover-selection-summary[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-popover-selection-summary[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-popover-selection-summary[data-dcl-scheme="dark"] button, .dcl-popover-selection-summary[data-dcl-scheme="dark"] input, .dcl-popover-selection-summary[data-dcl-scheme="dark"] select, .dcl-popover-selection-summary[data-dcl-scheme="dark"] summary, .dcl-popover-selection-summary[data-dcl-scheme="dark"] [data-card], .dcl-popover-selection-summary[data-dcl-scheme="dark"] [data-status], .dcl-popover-selection-summary[data-dcl-scheme="dark"] [data-panel] { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-popover-selection-summary[data-dcl-scheme="dark"] button:hover, .dcl-popover-selection-summary[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-popover-selection-summary[data-dcl-scheme="dark"] dialog, .dcl-popover-selection-summary[data-dcl-scheme="dark"] [popover] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; }
.dcl-popover-selection-summary[data-dcl-scheme="dark"] :focus-visible { outline-color: #8fe1da; }
.dcl-popover-selection-summary [popover] { inset: auto; margin: 0; position: fixed; width: min(21rem,calc(100% - 1rem)); max-height: min(26rem,var(--summary-height,100dvh)); padding: 0; overflow: hidden; }
.dcl-popover-selection-summary [data-summary-layout] { display: grid; grid-template-rows: minmax(0,1fr) auto; max-height: min(calc(26rem - 2px),calc(var(--summary-height,100dvh) - 2px)); padding: .375rem; }
.dcl-popover-selection-summary [data-summary-scroll] { overflow: auto; padding: .375rem; }
.dcl-popover-selection-summary [data-summary-layout] [data-actions] { margin: 0; padding: .125rem .375rem; }
.dcl-popover-selection-summary fieldset { margin-bottom: .75rem; }
</style>
When to use
A toolbar popover lists the items currently selected in the demo; selections update it without reopening. Replace the example content and wire the controls or destination IDs to your own screen.
Avoid
Not for editing the full selection.
Accessibility
Keyboard focus is visibly outlined. Expose an understandable summary without announcing every passive change. The main text contrast is 15.2:1/13.1:1 in light/dark mode, respectively.
Was this useful? Send feedback.