Component Library

← Back to gallery

Bottom sheet sort choices in HTML, CSS and vanilla JS

A copy-paste Bottom sheet sort choices in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Overlays · Tags: bottom-sheet, sheet, mobile, dialog, actions

Live preview

Source (copy-ready)

<section class="dcl-bottom-sheet-sort-choice" data-dcl-scheme="light">
<h2>Bottom sheet sort choices</h2>
<button type="button" data-open aria-haspopup="dialog">Choose sort order</button><dialog data-dialog  aria-labelledby="dcl-bottom-sheet-sort-choice-dialog-title"><h3 id="dcl-bottom-sheet-sort-choice-dialog-title" tabindex="-1">Sort trail visits</h3><fieldset data-stack><legend>Display order</legend><label><input type="radio" name="dcl-bottom-sheet-sort-choice-order" value="Newest first" checked> Newest first</label><label><input type="radio" name="dcl-bottom-sheet-sort-choice-order" value="Closest first" > Closest first</label><label><input type="radio" name="dcl-bottom-sheet-sort-choice-order" value="Name, A to Z" > Name, A to Z</label></fieldset><div data-actions><button type="button" data-commit>Apply sort</button><button type="button" data-close autofocus>Close</button></div></dialog><p role="status" data-status></p>
<script>
(() => {
const root = document.currentScript.closest('.dcl-bottom-sheet-sort-choice');
const instance = Array.from(document.getElementsByClassName('dcl-bottom-sheet-sort-choice')).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 panel = root.querySelector('dialog');
const opener = root.querySelector('[data-open]');
opener.addEventListener('click', () => panel.showModal());
panel.querySelector('[data-close]').addEventListener('click', () => panel.close());
panel.addEventListener('close', () => opener.focus());
let applied = panel.querySelector('input:checked');
panel.addEventListener('close', () => { applied.checked = true; });
panel.querySelector('[data-commit]').addEventListener('click', () => {
  applied = panel.querySelector('input:checked');
  root.querySelector('[role="status"]').textContent = 'Sort order: ' + applied.value;
  panel.close();
});
})();
</script>
</section>
<style>
.dcl-bottom-sheet-sort-choice { 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-bottom-sheet-sort-choice *, .dcl-bottom-sheet-sort-choice *::before, .dcl-bottom-sheet-sort-choice *::after { box-sizing: border-box; }
.dcl-bottom-sheet-sort-choice h2, .dcl-bottom-sheet-sort-choice h3, .dcl-bottom-sheet-sort-choice p, .dcl-bottom-sheet-sort-choice ul, .dcl-bottom-sheet-sort-choice ol, .dcl-bottom-sheet-sort-choice figure { margin: 0; }
.dcl-bottom-sheet-sort-choice h2 { font-size: 1.25rem; margin-bottom: .875rem; }
.dcl-bottom-sheet-sort-choice h3 { font-size: 1rem; }
.dcl-bottom-sheet-sort-choice li + li, .dcl-bottom-sheet-sort-choice p + p { margin-top: .375rem; }
.dcl-bottom-sheet-sort-choice ul, .dcl-bottom-sheet-sort-choice ol { padding-inline-start: 1.35rem; }
.dcl-bottom-sheet-sort-choice a { text-underline-offset: .18em; }
.dcl-bottom-sheet-sort-choice a:hover { text-decoration-thickness: .18em; }
.dcl-bottom-sheet-sort-choice :focus-visible { outline-style: solid; outline-width: 3px; outline-offset: 2px; }
.dcl-bottom-sheet-sort-choice button, .dcl-bottom-sheet-sort-choice summary, .dcl-bottom-sheet-sort-choice input, .dcl-bottom-sheet-sort-choice select { font: inherit; max-width: 100%; }
.dcl-bottom-sheet-sort-choice button, .dcl-bottom-sheet-sort-choice input, .dcl-bottom-sheet-sort-choice select { padding: .35rem .65rem; border: 1px solid; border-radius: .375rem; }
.dcl-bottom-sheet-sort-choice button, .dcl-bottom-sheet-sort-choice summary, .dcl-bottom-sheet-sort-choice select { cursor: pointer; }
.dcl-bottom-sheet-sort-choice button:active, .dcl-bottom-sheet-sort-choice summary:active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-bottom-sheet-sort-choice button:disabled, .dcl-bottom-sheet-sort-choice button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; }
.dcl-bottom-sheet-sort-choice [hidden] { display: none !important; }
.dcl-bottom-sheet-sort-choice [data-line], .dcl-bottom-sheet-sort-choice [data-actions] { display: flex; gap: .5rem .75rem; align-items: center; flex-wrap: wrap; }
.dcl-bottom-sheet-sort-choice [data-grid] { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(100%,9rem),1fr)); gap: .75rem; }
.dcl-bottom-sheet-sort-choice [data-stack], .dcl-bottom-sheet-sort-choice [data-targets] { display: grid; gap: .75rem; }
.dcl-bottom-sheet-sort-choice [data-targets] { border-top: 1px solid; padding-top: .75rem; margin-top: .75rem; }
.dcl-bottom-sheet-sort-choice [data-card], .dcl-bottom-sheet-sort-choice [data-status], .dcl-bottom-sheet-sort-choice [data-panel] { padding: .75rem; border: 1px solid; border-radius: .5rem; }
.dcl-bottom-sheet-sort-choice [data-status] { margin-top: .75rem; }
.dcl-bottom-sheet-sort-choice [data-status]:empty { border-width: 0; padding: 0; margin: 0; }
.dcl-bottom-sheet-sort-choice [aria-current] { font-weight: 750; }
.dcl-bottom-sheet-sort-choice dialog, .dcl-bottom-sheet-sort-choice [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-bottom-sheet-sort-choice dialog h3, .dcl-bottom-sheet-sort-choice [popover] h3 { margin-bottom: .5rem; }
.dcl-bottom-sheet-sort-choice dialog [data-actions], .dcl-bottom-sheet-sort-choice [popover] [data-actions] { margin-top: .75rem; }
.dcl-bottom-sheet-sort-choice svg { display: block; width: 100%; height: auto; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] a { color: #005c62; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] button, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] input, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] select, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] summary, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] [data-card], .dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] [data-status], .dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] [data-panel] { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] button:hover, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] dialog, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] [popover] { color: #11292f; background-color: #ffffff; border-color: #697f84; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="light"] :focus-visible { outline-color: #005c62; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] button, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] input, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] select, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] summary, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] [data-card], .dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] [data-status], .dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] [data-panel] { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] button:hover, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] dialog, .dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] [popover] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; }
.dcl-bottom-sheet-sort-choice[data-dcl-scheme="dark"] :focus-visible { outline-color: #8fe1da; }
.dcl-bottom-sheet-sort-choice dialog { inset: auto 0 0; margin: 0 auto; max-width: 100%; border-radius: 1rem 1rem 0 0; }
</style>

When to use

Bottom-aligned modal groups native sort radios with explicit Apply rather than firing on focus or selection. Replace the example content and wire the controls or destination IDs to your own screen.

Avoid

Avoid for dozens of choices.

Accessibility

Keyboard focus is visibly outlined. Group radios by legend and announce the applied option after dismissal. The main text contrast is 15.2:1/13.1:1 in light/dark mode, respectively.

Was this useful? Send feedback.