Workspace Switcher in HTML, CSS and vanilla JS
A copy-paste Workspace Switcher in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="example-workspace-switcher" data-dcl-layout="stack" data-dcl-scheme="light" aria-labelledby="example-workspace-switcher-title"><header><h2 id="example-workspace-switcher-title">Workspace Switcher</h2></header><div class="parts"><details><summary>Options</summary><ul><li><a href="#example-workspace-switcher-item-0">Design</a></li><li><a href="#example-workspace-switcher-item-1">Engineering</a></li><li><a href="#example-workspace-switcher-item-2">Support</a></li></ul></details><section id="example-workspace-switcher-item-0" tabindex="-1"><h3>Design</h3><p>The Design workspace holds shared drafts.</p></section><section id="example-workspace-switcher-item-1" tabindex="-1"><h3>Engineering</h3><p>The Engineering workspace holds integration notes.</p></section><section id="example-workspace-switcher-item-2" tabindex="-1"><h3>Support</h3><p>The Support workspace tracks questions.</p></section></div>
<script>
(() => {
const root = document.currentScript.closest(".example-workspace-switcher");
const instance = Array.from(document.getElementsByClassName("example-workspace-switcher")).indexOf(root);
if (instance > 0) {
const references = ["id", "aria-labelledby", "aria-controls", "aria-describedby", "for", "name"];
for (const element of [root, ...root.querySelectorAll("*")]) {
for (const attr of references) {
if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr) + "--" + instance);
}
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 disclosure = root.querySelector("details");
const trigger = disclosure.querySelector("summary");
disclosure.addEventListener("keydown", (event) => {
if (event.key !== "Escape" || !disclosure.open) return;
disclosure.open = false;
trigger.focus();
});
disclosure.querySelectorAll("a").forEach((link) => {
link.addEventListener("click", (event) => {
if (event.ctrlKey || event.metaKey || event.altKey || event.shiftKey) return;
event.preventDefault();
const target = document.getElementById(link.hash.slice(1));
disclosure.open = false;
target.focus();
});
});
})();
</script></section>
<style>
.example-workspace-switcher { color: #11292f; background-color: #ffffff; color-scheme: light; }
.example-workspace-switcher a { color: #005c62; }
.example-workspace-switcher button, .example-workspace-switcher summary, .example-workspace-switcher input, .example-workspace-switcher select {
color: #11292f; background-color: #e4edef; border-color: #697f84;
}
.example-workspace-switcher button:hover, .example-workspace-switcher summary:hover, .example-workspace-switcher button[aria-selected="true"] {
color: #ffffff; background-color: #00535a;
}
.example-workspace-switcher dialog { color: #11292f; background-color: #ffffff; }
.example-workspace-switcher [data-note] { color: #11292f; background-color: #e4edef; }
.example-workspace-switcher :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.example-workspace-switcher[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; color-scheme: dark; }
.example-workspace-switcher[data-dcl-scheme="dark"] a { color: #8fe1da; }
.example-workspace-switcher[data-dcl-scheme="dark"] button, .example-workspace-switcher[data-dcl-scheme="dark"] summary, .example-workspace-switcher[data-dcl-scheme="dark"] input, .example-workspace-switcher[data-dcl-scheme="dark"] select {
color: #eff8f9; background-color: #1c3c44; border-color: #839fa5;
}
.example-workspace-switcher[data-dcl-scheme="dark"] button:hover, .example-workspace-switcher[data-dcl-scheme="dark"] summary:hover, .example-workspace-switcher[data-dcl-scheme="dark"] button[aria-selected="true"] {
color: #11292f; background-color: #8fe1da;
}
.example-workspace-switcher[data-dcl-scheme="dark"] dialog { color: #eff8f9; background-color: #142f36; }
.example-workspace-switcher[data-dcl-scheme="dark"] [data-note] { color: #eff8f9; background-color: #1c3c44; }
.example-workspace-switcher[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.example-workspace-switcher, .example-workspace-switcher * { box-sizing: border-box; }
.example-workspace-switcher { font: 1rem/1.5 system-ui, sans-serif; padding: 1rem; border-radius: 0.75rem; overflow-wrap: anywhere; }
.example-workspace-switcher h2, .example-workspace-switcher h3 { line-height: 1.15; margin-block: 0 0.75rem; }
.example-workspace-switcher p { margin-block: 0.5rem; }
.example-workspace-switcher ul { margin-block: 0.5rem; padding-inline-start: 1.5rem; }
.example-workspace-switcher article, .example-workspace-switcher details, .example-workspace-switcher [data-note] { border: 1px solid; border-radius: 0.375rem; padding: 0.75rem; }
.example-workspace-switcher button, .example-workspace-switcher summary, .example-workspace-switcher input, .example-workspace-switcher select {
min-block-size: 44px; border: 1px solid; border-radius: 0.375rem; padding: 0.5rem 0.75rem; font: inherit;
}
.example-workspace-switcher button, .example-workspace-switcher summary { cursor: pointer; transition: background-color 140ms ease-out; }
.example-workspace-switcher button:disabled { cursor: not-allowed; text-decoration: line-through; }
.example-workspace-switcher input { display: block; inline-size: 100%; }
.example-workspace-switcher dialog { max-inline-size: min(32rem, 90vw); border-radius: 0.75rem; padding: 1rem; }
.example-workspace-switcher th, .example-workspace-switcher td { padding: 0.5rem; text-align: start; border-block-end: 1px solid; }
.example-workspace-switcher table { border-collapse: collapse; inline-size: 100%; }
.example-workspace-switcher caption { font-weight: bold; text-align: start; }
.example-workspace-switcher dt { font-weight: 600; }
.example-workspace-switcher dd { margin: 0 0 0.75rem; }
.example-workspace-switcher [hidden] { display: none !important; }
.example-workspace-switcher .parts { display: grid; gap: 1rem; }
@media (max-width: 30rem) {
.example-workspace-switcher[data-dcl-layout="rail"] .parts { grid-template-columns: minmax(0, 1fr); }
.example-workspace-switcher[data-dcl-layout="rail"] .parts > :is(p, ul, article) { grid-column: 1; grid-row: auto; }
.example-workspace-switcher[data-dcl-layout="rail"] .parts > :is(details, nav) { grid-row: auto; }
}
@media (prefers-reduced-motion: reduce) { .example-workspace-switcher * { transition: none !important; } }
</style>
When to use
Use this stacked pattern to give each item a full-width row.
Avoid
Do not hide the main navigation behind a disclosure when room is available.
Accessibility
Use ordinary links inside the disclosure; Escape closes it and returns focus to the summary.
Was this useful? Send feedback.