Button Library · Interactive
Toggle
Stays on or off, tells screen readers which.
States
Default
Hover
Active
Focus
Code
toggle.html
<button class="btn-toggle" type="button" aria-pressed="false">
<svg class="btn-toggle__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="m12 4 2.5 5.2 5.7.8-4.1 4 1 5.7L12 17.2 6.9 19.7l1-5.7-4.1-4 5.7-.8Z"></path>
</svg>
<span>Follow</span>
</button>
toggle.css
.btn-toggle {
--btn-bg: #2f2f3a;
--btn-fg: #ffffff;
--btn-font: inherit;
--btn-size: 15px;
--btn-weight: 600;
--btn-px: 22px;
--btn-py: 11px;
--btn-w: auto;
--btn-h: auto;
--btn-bw: 0px;
--btn-line: #8b6cff;
--btn-radius: 10px;
--btn-shadow: none;
--btn-speed: 180ms;
}
.btn-toggle {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
gap: 0.5em;
box-sizing: border-box;
width: var(--btn-w);
height: var(--btn-h);
padding: var(--btn-py) var(--btn-px);
font-family: var(--btn-font);
font-size: var(--btn-size);
font-weight: var(--btn-weight);
line-height: 1.2;
color: var(--btn-fg);
background: var(--btn-bg);
border: var(--btn-bw) solid var(--btn-line);
border-radius: var(--btn-radius);
box-shadow: var(--btn-shadow);
cursor: pointer;
text-decoration: none;
white-space: nowrap;
transition: all var(--btn-speed) ease;
}
.btn-toggle:focus-visible {
outline: 2px solid var(--btn-bg);
outline-offset: 2px;
}
.btn-toggle:disabled {
opacity: 0.5;
pointer-events: none;
}
.btn-toggle__icon {
width: 1.15em;
height: 1.15em;
flex-shrink: 0;
}
.btn-toggle[aria-pressed=true] {
background: #7c5cff;
box-shadow: 0 0 0 2px #7c5cff55;
}
@media (prefers-reduced-motion: reduce) {
.btn-toggle {
animation: none !important;
transition: none !important;
}
}
toggle.js
document.querySelectorAll(".btn-toggle").forEach((el) => {
const flip = () => el.setAttribute("aria-pressed", String(el.getAttribute("aria-pressed") !== "true"));
el.addEventListener("click", flip);
return () => el.removeEventListener("click", flip);
});
toggle.tsx
import { useEffect, useRef } from "react";
import type React from "react";
import "./toggle.css"; // the CSS tab
function attach(el: HTMLElement) {
const flip = () => el.setAttribute("aria-pressed", String(el.getAttribute("aria-pressed") !== "true"));
el.addEventListener("click", flip);
return () => el.removeEventListener("click", flip);
}
export function BtnToggle({ children = "Follow", ...props }: React.ComponentProps<"button">) {
const ref = useRef<HTMLButtonElement>(null);
useEffect(() => attach(ref.current!), []);
return (
<button className="btn-toggle" type="button" aria-pressed="false" ref={ref} {...props}>
<svg className="btn-toggle__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
<path d="m12 4 2.5 5.2 5.7.8-4.1 4 1 5.7L12 17.2 6.9 19.7l1-5.7-4.1-4 5.7-.8Z" />
</svg>
<span>{children}</span>
</button>
);
}
toggle.html
<button class="btn-toggle relative inline-flex items-center justify-center gap-[0.5em] [box-sizing:border-box] [padding:11px_22px] [font-family:inherit] text-[15px] font-[600] [line-height:1.2] text-[#ffffff] bg-[#2f2f3a] [border:0px_solid_#8b6cff] rounded-[10px] [box-shadow:none] cursor-pointer no-underline whitespace-nowrap [transition:all_180ms_ease] focus-visible:[outline:2px_solid_#2f2f3a] focus-visible:[outline-offset:2px] disabled:[opacity:0.5] disabled:[pointer-events:none] [&[aria-pressed=true]]:bg-[#7c5cff] [&[aria-pressed=true]]:[box-shadow:0_0_0_2px_#7c5cff55] motion-reduce:[transition:none!important]" type="button" aria-pressed="false">
<svg class="w-[1.15em] h-[1.15em] [flex-shrink:0]" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="m12 4 2.5 5.2 5.7.8-4.1 4 1 5.7L12 17.2 6.9 19.7l1-5.7-4.1-4 5.7-.8Z"></path>
</svg>
<span>Follow</span>
</button>
<!-- Needs the script from the JS tab. -->