Component Library

← Back to gallery

Selection-scoped floating action in HTML, CSS and vanilla JS

A copy-paste Selection-scoped floating action in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Buttons · Tags: floating-action-button, selection, actions

Live preview

Source (copy-ready)

<section class="dcl-floating-action-button-selection" data-dcl-scheme="light">
<h2>Archive finished tasks</h2><fieldset data-stack><legend>Completed tasks</legend>
        <label><input type="checkbox"> Check spelling</label><label><input type="checkbox"> Confirm artwork</label>
        <label><input type="checkbox"> Send to printer</label></fieldset>
        <div class="dcl-floating-action-button-selection-dock"><button type="button" class="dcl-floating-action-button-selection-primary" data-archive hidden>Archive 0 selected</button></div><p class="dcl-floating-action-button-selection-status" role="status" data-status></p>
<script>
(() => {
  const root = document.currentScript.parentElement;
  const instance = [...document.getElementsByClassName("dcl-floating-action-button-selection")].indexOf(root);
  if (instance > 0) {
    for (const node of root.querySelectorAll("[id], [for], [aria-labelledby], [aria-controls], [aria-describedby], a[href], input[name]")) {
      for (const key of ["id", "for", "aria-labelledby", "aria-controls", "aria-describedby", "name"]) {
        if (node.hasAttribute(key)) node.setAttribute(key, node.getAttribute(key).split(" ").map(value => value + "--" + instance).join(" "));
      }
      const href = node.getAttribute("href");
      if (href && href.startsWith("#")) node.setAttribute("href", href + "--" + instance);
    }
  }
  const scheme = matchMedia("(prefers-color-scheme: dark)");
  const applyScheme = () => { root.dataset.dclScheme = scheme.matches ? "dark" : "light"; };
  scheme.addEventListener("change", applyScheme);
  applyScheme();
  const choices = [...root.querySelectorAll("input")];
  const archive = root.querySelector("[data-archive]");
  const status = root.querySelector("[data-status]");
  const current = () => choices.filter(choice => choice.checked);
  function update() {
    const count = current().length;
    archive.hidden = !count;
    archive.textContent = "Archive " + count + " selected";
    status.textContent = count + " selected.";
  }
  choices.forEach(choice => choice.addEventListener("change", update));
  archive.addEventListener("click", () => {
    const count = current().length;
    current().forEach(choice => { choice.checked = false; choice.closest("label").hidden = true; });
    update();
    status.textContent = count + " archived.";
    const next = choices.find(choice => !choice.closest("label").hidden);
    if (next) next.focus();
    else { status.tabIndex = -1; status.focus(); }
  });
})();
</script>
</section>
<style>
.dcl-floating-action-button-selection { box-sizing: border-box; max-inline-size: 42rem; padding: clamp(.75rem, 4vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
.dcl-floating-action-button-selection *, .dcl-floating-action-button-selection *::before, .dcl-floating-action-button-selection *::after { box-sizing: border-box; }
.dcl-floating-action-button-selection h2, .dcl-floating-action-button-selection h3, .dcl-floating-action-button-selection p, .dcl-floating-action-button-selection ul, .dcl-floating-action-button-selection dl, .dcl-floating-action-button-selection figure { margin: 0; }
.dcl-floating-action-button-selection h2 { font-size: 1.25rem; line-height: 1.25; }
.dcl-floating-action-button-selection h3 { font-size: 1rem; }
.dcl-floating-action-button-selection > * + * { margin-block-start: .75rem; }
.dcl-floating-action-button-selection button, .dcl-floating-action-button-selection summary, .dcl-floating-action-button-selection input, .dcl-floating-action-button-selection select, .dcl-floating-action-button-selection textarea { font: inherit; }
.dcl-floating-action-button-selection button, .dcl-floating-action-button-selection summary { padding: .45rem .7rem; min-block-size: 2.75rem; border: 1px solid; border-radius: .375rem; cursor: pointer; text-align: start; }
.dcl-floating-action-button-selection select, .dcl-floating-action-button-selection input[type="text"], .dcl-floating-action-button-selection textarea { max-inline-size: 100%; padding: .45rem; border: 1px solid; border-radius: .375rem; }
.dcl-floating-action-button-selection button:active { transform: translateY(1px); }
.dcl-floating-action-button-selection button[disabled], .dcl-floating-action-button-selection button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; transform: none; }
.dcl-floating-action-button-selection button[aria-pressed="true"] { border-style: double; border-width: 3px; }
.dcl-floating-action-button-selection details > summary { inline-size: fit-content; }
.dcl-floating-action-button-selection [data-panel], .dcl-floating-action-button-selection dialog { border: 1px solid; border-radius: .625rem; padding: .8rem; }
.dcl-floating-action-button-selection dialog { max-inline-size: min(calc(100% - 2rem), 28rem); max-block-size: calc(100% - 2rem); }
.dcl-floating-action-button-selection dialog > * + * { margin-block-start: .75rem; }
.dcl-floating-action-button-selection [data-row] { display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; min-inline-size: 0; }
.dcl-floating-action-button-selection [data-stack] { display: grid; gap: .6rem; min-inline-size: 0; }
.dcl-floating-action-button-selection [data-list] { list-style: none; padding: 0; display: grid; gap: .55rem; }
.dcl-floating-action-button-selection [data-wide] { flex: 1 1 11rem; min-inline-size: 0; }
.dcl-floating-action-button-selection .dcl-floating-action-button-selection-status { min-block-size: 1.5em; font-weight: 600; }
.dcl-floating-action-button-selection .dcl-floating-action-button-selection-glyph { display: inline-grid; place-items: center; font-size: 1.2em; line-height: 1; }
.dcl-floating-action-button-selection [hidden] { display: none; }
.dcl-floating-action-button-selection { color: #11292f; background-color: #ffffff; color-scheme: light; border-color: #697f84; }
.dcl-floating-action-button-selection a { color: #005c62; }
.dcl-floating-action-button-selection button, .dcl-floating-action-button-selection summary, .dcl-floating-action-button-selection input, .dcl-floating-action-button-selection select, .dcl-floating-action-button-selection textarea { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-floating-action-button-selection .dcl-floating-action-button-selection-primary, .dcl-floating-action-button-selection button[aria-pressed="true"] { color: #ffffff; background-color: #00535a; }
.dcl-floating-action-button-selection button:hover, .dcl-floating-action-button-selection summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-floating-action-button-selection button[disabled], .dcl-floating-action-button-selection button[aria-disabled="true"] { color: #405a60; background-color: #ffffff; }
.dcl-floating-action-button-selection [data-panel], .dcl-floating-action-button-selection dialog { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-floating-action-button-selection :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-floating-action-button-selection[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; border-color: #839fa5; }
.dcl-floating-action-button-selection[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-floating-action-button-selection[data-dcl-scheme="dark"] button, .dcl-floating-action-button-selection[data-dcl-scheme="dark"] summary, .dcl-floating-action-button-selection[data-dcl-scheme="dark"] input, .dcl-floating-action-button-selection[data-dcl-scheme="dark"] select, .dcl-floating-action-button-selection[data-dcl-scheme="dark"] textarea { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-floating-action-button-selection[data-dcl-scheme="dark"] .dcl-floating-action-button-selection-primary, .dcl-floating-action-button-selection[data-dcl-scheme="dark"] button[aria-pressed="true"] { color: #11292f; background-color: #8fe1da; }
.dcl-floating-action-button-selection[data-dcl-scheme="dark"] button:hover, .dcl-floating-action-button-selection[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-floating-action-button-selection[data-dcl-scheme="dark"] button[disabled], .dcl-floating-action-button-selection[data-dcl-scheme="dark"] button[aria-disabled="true"] { color: #b0c9ce; background-color: #142f36; }
.dcl-floating-action-button-selection[data-dcl-scheme="dark"] [data-panel], .dcl-floating-action-button-selection[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-floating-action-button-selection[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-floating-action-button-selection .dcl-floating-action-button-selection-dock { min-block-size: 2.75rem; display: flex; justify-content: end; } .dcl-floating-action-button-selection .dcl-floating-action-button-selection-dock button { border-radius: 2rem; }
</style>

When to use

Surface one batch action only once there is a selection to operate on.

Avoid

Do not make selection alone trigger a change or cover remaining rows.

Accessibility

Keep focus in the selection list when the action appears; announce changes to its count. Light/dark body text: 15.19:1 and 13.07:1, respectively.

Was this useful? Send feedback.