Component Library

← Back to gallery

Confirm-before-off switch in HTML, CSS and vanilla JS

A copy-paste Confirm-before-off switch in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Forms · Tags: toggle switch, confirmation, destructive prevention, role switch, form

Live preview

Source (copy-ready)

<section class="example-toggle-switch-confirm-off" data-dcl-scheme="light" aria-labelledby="example-toggle-switch-confirm-off-title">
<header><h2 id="example-toggle-switch-confirm-off-title">Confirm-before-off switch</h2>
<p>Turning protection off asks for explicit confirmation while turning it on remains immediate.</p></header>
<div class="parts">
<button type="button" role="switch" aria-checked="true" data-switch>Backup protection On</button>
<div class="confirm-box" data-confirm hidden><p>Turning off backup protection means deleted files cannot be recovered.</p><div class="actions"><button type="button" data-cancel>Keep protection on</button><button type="button" data-confirm-off>Turn off protection</button></div></div><p class="status" role="status" aria-live="polite" data-status></p>
</div>
<script>
(() => {
  const root = document.currentScript.closest(".example-toggle-switch-confirm-off");
  const instance = Array.from(document.getElementsByClassName("example-toggle-switch-confirm-off")).indexOf(root);
  if (instance > 0) {
    const singleReferences = ["id", "aria-activedescendant", "for", "list", "name"];
    const tokenReferences = ["aria-labelledby", "aria-controls", "aria-describedby"];
    for (const element of [root, ...root.querySelectorAll("*")]) {
      for (const attr of singleReferences) {
        if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
      }
      for (const attr of tokenReferences) {
        if (element.hasAttribute(attr)) {
          const rewritten = element.getAttribute(attr).trim().split(/\s+/).filter(Boolean).map((reference) => reference + "--" + instance);
          element.setAttribute(attr, rewritten.join(" "));
        }
      }
      const href = element.getAttribute("href");
      if (href && href.startsWith("#")) element.setAttribute("href", href + "--" + instance);
    }
  }
  const preference = matchMedia("(prefers-color-scheme: dark)");
  const apply = () => { root.dataset.dclScheme = preference.matches ? "dark" : "light"; };
  preference.addEventListener("change", apply);
  apply();
  const sw=root.querySelector('[data-switch]'),box=root.querySelector('[data-confirm]'),yes=root.querySelector('[data-confirm-off]'),no=root.querySelector('[data-cancel]'),status=root.querySelector('[data-status]');
  function state(on){sw.setAttribute('aria-checked',String(on));sw.textContent='Backup protection '+(on?'On':'Off');status.textContent='Backup protection is '+(on?'on':'off')+'.'}
  function closeConfirmation(message){box.hidden=true;sw.focus();status.textContent=message}
  sw.addEventListener('click',()=>{if(sw.getAttribute('aria-checked')==='true'){box.hidden=false;no.focus();status.textContent='Confirmation required before turning off.'}else{box.hidden=true;state(true)}});
  yes.addEventListener('click',()=>{box.hidden=true;state(false);sw.focus()});
  no.addEventListener('click',()=>closeConfirmation('Backup protection remains on.'));
  box.addEventListener('keydown',(event)=>{if(event.key==='Escape'){event.preventDefault();closeConfirmation('Backup protection remains on.')}});
  state(true);
})();
</script></section>
<style>
.example-toggle-switch-confirm-off { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-toggle-switch-confirm-off a { color: #005c62; }
.example-toggle-switch-confirm-off button, .example-toggle-switch-confirm-off summary, .example-toggle-switch-confirm-off input, .example-toggle-switch-confirm-off select, .example-toggle-switch-confirm-off textarea {
  color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-toggle-switch-confirm-off button:hover, .example-toggle-switch-confirm-off summary:hover, .example-toggle-switch-confirm-off button[aria-selected="true"], .example-toggle-switch-confirm-off button[aria-pressed="true"] {
  color: #ffffff; background-color: #00535a;
}
.example-toggle-switch-confirm-off dialog { color: #11292f; background-color: #ffffff; }
.example-toggle-switch-confirm-off [data-note] { color: #11292f; background-color: #e4edef; }
.example-toggle-switch-confirm-off [data-error] { color: #7f1d1d; background-color: #fee2e2; }
.example-toggle-switch-confirm-off [data-success] { color: #14532d; background-color: #dcfce7; }
.example-toggle-switch-confirm-off [data-warning] { color: #713f12; background-color: #fef9c3; }
.example-toggle-switch-confirm-off :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }

.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] button, .example-toggle-switch-confirm-off[data-dcl-scheme="dark"] summary,
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] input, .example-toggle-switch-confirm-off[data-dcl-scheme="dark"] select, .example-toggle-switch-confirm-off[data-dcl-scheme="dark"] textarea {
  color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] button:hover, .example-toggle-switch-confirm-off[data-dcl-scheme="dark"] summary:hover,
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] button[aria-selected="true"], .example-toggle-switch-confirm-off[data-dcl-scheme="dark"] button[aria-pressed="true"] {
  color: #11292f; background-color: #8fe1da;
}
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] [data-error] { color: #fecaca; background-color: #451a1a; }
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] [data-success] { color: #bbf7d0; background-color: #14532d; }
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] [data-warning] { color: #fde68a; background-color: #451a03; }
.example-toggle-switch-confirm-off[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }

.example-toggle-switch-confirm-off, .example-toggle-switch-confirm-off * { box-sizing: border-box; min-width: 0; }
.example-toggle-switch-confirm-off { display: block; font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-toggle-switch-confirm-off h2, .example-toggle-switch-confirm-off h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-toggle-switch-confirm-off header p, .example-toggle-switch-confirm-off p { margin-block: 0.5rem; }
.example-toggle-switch-confirm-off ul, .example-toggle-switch-confirm-off ol { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-toggle-switch-confirm-off dl { margin-block: 0.5rem; }
.example-toggle-switch-confirm-off dt { font-weight: 700; }
.example-toggle-switch-confirm-off dd { margin: 0 0 0.6rem; }
.example-toggle-switch-confirm-off fieldset { min-inline-size: 0; margin: 0; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-toggle-switch-confirm-off legend { padding-inline: 0.25rem; font-weight: 700; }
.example-toggle-switch-confirm-off button, .example-toggle-switch-confirm-off input, .example-toggle-switch-confirm-off select, .example-toggle-switch-confirm-off textarea {
  min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-toggle-switch-confirm-off input, .example-toggle-switch-confirm-off select, .example-toggle-switch-confirm-off textarea { display: block; inline-size: 100%; max-inline-size: 100%; }
.example-toggle-switch-confirm-off input[type="checkbox"], .example-toggle-switch-confirm-off input[type="radio"] {
  display: inline-block; inline-size: auto; min-block-size: auto; padding: 0; flex: 0 0 auto;
}
.example-toggle-switch-confirm-off input[type="file"] { padding: 0.45rem; }
.example-toggle-switch-confirm-off input[type="range"] { padding-inline: 0; }
.example-toggle-switch-confirm-off textarea { min-block-size: 6rem; resize: vertical; }
.example-toggle-switch-confirm-off button, .example-toggle-switch-confirm-off summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-toggle-switch-confirm-off button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-toggle-switch-confirm-off table { border-collapse: collapse; inline-size: 100%; }
.example-toggle-switch-confirm-off th, .example-toggle-switch-confirm-off td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-toggle-switch-confirm-off [hidden] { display: none !important; }
.example-toggle-switch-confirm-off [data-note], .example-toggle-switch-confirm-off [data-error], .example-toggle-switch-confirm-off [data-success], .example-toggle-switch-confirm-off [data-warning] { border-radius: 0.5rem; padding: 0.65rem; }
.example-toggle-switch-confirm-off .parts { display: grid; gap: 1rem; }
.example-toggle-switch-confirm-off .field, .example-toggle-switch-confirm-off .fields { display: grid; gap: 0.4rem; }
.example-toggle-switch-confirm-off .fields { gap: 0.75rem; }
.example-toggle-switch-confirm-off .actions, .example-toggle-switch-confirm-off .inline-row { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }
.example-toggle-switch-confirm-off .panel { border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-toggle-switch-confirm-off .plain-list { margin: 0; padding: 0; list-style: none; }
.example-toggle-switch-confirm-off .choice { display: flex; gap: 0.5rem; align-items: flex-start; }
.example-toggle-switch-confirm-off .choice input { margin-block-start: 0.25rem; }
.example-toggle-switch-confirm-off .choice-stack { display: grid; gap: 0.55rem; }
.example-toggle-switch-confirm-off .tile { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0.55rem; align-items: start; border: 1px solid; border-radius: 0.5rem; padding: 0.75rem; }
.example-toggle-switch-confirm-off .tile[data-active] { border-width: 3px; padding: calc(0.75rem - 2px); }
.example-toggle-switch-confirm-off .pill { display: inline-flex; gap: 0.4rem; align-items: center; border: 1px solid; border-radius: 999px; padding: 0.35rem 0.7rem; }
.example-toggle-switch-confirm-off .pill[data-active] { border-width: 3px; padding: calc(0.35rem - 2px) calc(0.7rem - 2px); }
.example-toggle-switch-confirm-off .hint { font-size: 0.875rem; }
.example-toggle-switch-confirm-off .status { min-block-size: 1.5rem; margin-block: 0; }
.example-toggle-switch-confirm-off .two-column, .example-toggle-switch-confirm-off .split-layout { display: grid; gap: 1rem; }
.example-toggle-switch-confirm-off .sticky-panel { position: sticky; inset-block-start: 1rem; align-self: start; }
.example-toggle-switch-confirm-off .scroll-x { overflow-x: auto; max-inline-size: 100%; }
.example-toggle-switch-confirm-off .visually-hidden {
  position: absolute; inline-size: 1px; block-size: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
@media (min-width: 34rem) {
  .example-toggle-switch-confirm-off .two-column { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .example-toggle-switch-confirm-off .split-layout { grid-template-columns: minmax(0, 1fr) minmax(12rem, 18rem); }
}
@media (max-width: 30rem) {
  .example-toggle-switch-confirm-off { padding: 0.75rem; }
  .example-toggle-switch-confirm-off .actions, .example-toggle-switch-confirm-off .inline-row { align-items: stretch; }
  .example-toggle-switch-confirm-off .wizard-steps { grid-template-columns: minmax(0, 1fr) !important; }
}
@media (prefers-reduced-motion: reduce) { .example-toggle-switch-confirm-off * { transition: none !important; } }
.example-toggle-switch-confirm-off .confirm-box[hidden] { display: none !important; }

.example-toggle-switch-confirm-off .confirm-box{border:1px solid;border-radius:.5rem;padding:.75rem;margin-block-start:.75rem}
</style>

When to use

Use only when disabling has significant consequences, such as losing backup protection. Keep enabling frictionless and make cancellation the safe default.

Avoid

Do not demand confirmation for ordinary reversible preferences; repeated friction trains users to ignore it.

Accessibility

The first activation of the on-state switch reveals confirmation buttons instead of changing state. Focus starts on the safe cancellation, Escape dismisses the confirmation, focus returns to the switch on both exits, and outcomes are announced.

Was this useful? Send feedback.