Inputs
Learn how to style input fields, date and sensitive inputs, checkboxes, radiobuttons, segmented controls, choice pickers, toggles, sliders, steppers and file uploads.
Input Fields
To style an input simply add a .form-control class.
All input fields must either have an associated label or have an aria-label attribute. Optionally, they can have a description text. Use .form-label for labels, and .form-text for input description text. Also, labels can be hidden (but still accessible by screen readers) by adding .visually-hidden class.
You can use layout classes on inputs or on wrapper elements to create custom layouts.
Simple Input
Input help text
View codeHide code
<label for="input-example-1" class="form-label required">Form label</label>
<input type="text" id="input-example-1" class="form-control w-100" placeholder="Input field" required aria-describedby="input-help-1">
<p class="form-text" id="input-help-1">Input help text</p>Search Input
Use type="search" on the input — the search icon is added automatically
View codeHide code
<label for="input-example-3" class="form-label">Form label</label>
<input type="search" id="input-example-3" class="form-control w-100" placeholder="Search" aria-describedby="input-help-3">
<p class="form-text" id="input-help-3">Use <code>type="search"</code> on the input</p>Date Input
A date input is type="date" with a calendar icon at the start: wrap the input in a .pos-rel container, add .pl-40 to the input, and place the icon button with .pos-abs.top-8.left-8. The browser shows the date in the person's local format and opens its own date picker. Hide the browser's calendar icon with the CSS, and open the picker from the button with the script: see the CSS and JS tabs in the code below.
Input help text
View codeHide code
<label for="input-date" class="form-label">Start date</label>
<div class="pos-rel">
<input type="date" id="input-date" class="form-control w-100 pl-40" value="2026-11-11" aria-describedby="input-date-help">
<button type="button" class="pos-abs pa-0 top-8 left-8" aria-label="Open date picker" tabindex="-1" data-date-picker="input-date">
<svg class="icon size-24 text-muted" aria-hidden="true"><use href="#common-calendar"/></svg>
</button>
</div>
<p class="form-text" id="input-date-help">Input help text</p>
/* Hide the browser's own calendar icon (Chrome, Edge, Safari) */
input[type="date"]::-webkit-calendar-picker-indicator {
display: none;
}
// Open the browser's date picker from the calendar button and on click in the input
document.querySelectorAll("[data-date-picker]").forEach((button) => {
const input = document.getElementById(button.dataset.datePicker);
const open = () => { try { input.showPicker(); } catch (e) { input.focus(); } };
button.addEventListener("click", open);
input.addEventListener("click", open);
});
Sensitive Input
Use a sensitive input for private data, such as an ID number or an account number. It has a lock icon at the start and hides what people type with type="password". The icon doesn't block clicks, so clicking it focuses the input.
Input help text
View codeHide code
<label for="input-secure" class="form-label">Student ID number</label>
<div class="pos-rel">
<input type="password" id="input-secure" class="form-control w-100 pl-40" value="A12345678" autocomplete="off" aria-describedby="input-secure-help">
<svg class="icon size-24 text-muted pos-abs top-8 left-8" aria-hidden="true"><use href="#common-lock"/></svg>
</div>
<p class="form-text" id="input-secure-help">Input help text</p>Invalid Input
Error description. Add .is-invalid to the input and to the description
View codeHide code
<label for="input-example-4" class="form-label required">Form label</label>
<input type="text" id="input-example-4" class="form-control is-invalid w-100" placeholder="Invalid input" required aria-invalid="true" aria-describedby="input-help-4">
<p class="form-text is-invalid" id="input-help-4">Error description. Add <code>.is-invalid</code> to the input and to the description</p>Valid Input
Add .is-valid to the input and to the description
View codeHide code
<label for="input-example-5" class="form-label required">Form label</label>
<input type="text" id="input-example-5" class="form-control is-valid w-100" placeholder="Valid input" required aria-describedby="input-help-5">
<p class="form-text is-valid" id="input-help-5">Add <code>.is-valid</code> to the input and to the description</p>Disabled Input
Input help text
View codeHide code
<label for="input-example-6" class="form-label required">Form label</label>
<input type="text" id="input-example-6" class="form-control w-100" placeholder="Input field" required disabled aria-describedby="input-help-6">
<p class="form-text" id="input-help-6">Input help text</p>Select
Avoid a select for only two or three options, because people have to open it to see them. If the option is yes or no, use a toggle or a checkbox. If the labels are short, use a segmented control, and if they're longer, use radiobuttons.
View codeHide code
<select class="form-control w-100" aria-label="Example select">
<option value="option1" selected>Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>Checkboxes
Stack checkboxes in a column on small screens and put them in a row from the md breakpoint (680px): .d-flex.flex-column.flex-md-row.flex-wrap.
A checkbox is indeterminate when it stands for a group whose items are only partly selected, e.g. a “Select all” checkbox above a list with some items checked. It shows a dash. There's no HTML attribute for this state: set input.indeterminate = true in your script (see the JS tab). Clicking an indeterminate checkbox checks it.
View codeHide code
<div class="d-flex flex-column flex-md-row flex-wrap gap-x-24 gap-y-8">
<label class="form-checkbox">
<input type="checkbox" name="checkbox-name">
<span class="checkbox mr-8"></span>
<span>Checkbox label</span>
</label>
<label class="form-checkbox">
<input type="checkbox" name="checkbox-name" checked>
<span class="checkbox mr-8"></span>
<span>Checkbox label</span>
</label>
<label class="form-checkbox">
<input type="checkbox" name="checkbox-name" data-indeterminate>
<span class="checkbox mr-8"></span>
<span>Indeterminate</span>
</label>
</div>
// There's no HTML attribute for the indeterminate state: set it in JS.
document.querySelectorAll("input[data-indeterminate]").forEach((input) => {
input.indeterminate = true;
});
Radiobuttons
Stack radiobuttons in a column on small screens and put them in a row from the md breakpoint (680px): .d-flex.flex-column.flex-md-row.flex-wrap. Each label has 8px padding at the top and bottom, so the column needs no gap.
Don't use radiobuttons for a yes or no choice: use a toggle or a checkbox instead.
View codeHide code
<div class="radio-group d-flex flex-column flex-md-row flex-wrap gap-x-24" role="radiogroup" aria-label="Radio group label">
<label class="form-radio">
<input type="radio" name="radio-name" value="1">
<span>Radio label</span>
</label>
<label class="form-radio">
<input type="radio" name="radio-name" value="2">
<span>Radio label</span>
</label>
</div>Radiobuttons with Descriptions
Stack the options with .d-flex.flex-column and put a description under each label: make the label's <span> .d-block and add the description in a <small class="d-block text-md text-quiet">. Use <small>, not another <span>: the radio circle is drawn on every <span> inside the label. The circle stays centered on the label and description together.
Frequency
View codeHide code
<div>
<p id="frequency-label" class="mb-4">Frequency</p>
<div class="radio-group d-flex flex-column" role="radiogroup" aria-labelledby="frequency-label">
<label class="form-radio">
<input type="radio" name="frequency" value="daily" checked>
<span class="d-block">Daily digest<small class="d-block text-md text-quiet">Sent at 6 PM, institution time.</small></span>
</label>
<label class="form-radio">
<input type="radio" name="frequency" value="weekly">
<span class="d-block">Weekly digest<small class="d-block text-md text-quiet">Sent at 6 PM on Fridays, institution time.</small></span>
</label>
<label class="form-radio">
<input type="radio" name="frequency" value="instant">
<span class="d-block">As matches are found<small class="d-block text-md text-quiet">Batched in 1-hour intervals.</small></span>
</label>
</div>
</div>Segmented Controls
A segmented control is a compact set of radiobuttons for switching between views or options, e.g. grouping a chart by day, week or month. The options sit on a gray track (.d-inline-flex.gap-4.bg-n1.rounded). Each option is a small button: the selected one is .secondary.ba.bg-white with .text-base on its text, the others are .borderless.
Use a segmented control to change how content on the page or in a card is filtered or displayed. Don't use it as tabs that change the content of the whole page: to switch between pages, use Tabs. To let people pick an option, e.g. in a form, use Radiobuttons.
A segmented control doesn't wrap, so use it only for up to three options with short labels. For more options or longer labels, use a select.
One option must always be selected: mark the default one as selected in the markup, and don't leave the control empty.
Use role="radiogroup" and role="radio" with aria-checked. When the selection changes, swap the classes and update aria-checked and tabindex, so the arrow keys move between options like in a native radio group: see the JS tab.
Text
View codeHide code
<div class="d-inline-flex gap-4 bg-n1 rounded" role="radiogroup" aria-label="Group by" data-segmented>
<button type="button" role="radio" aria-checked="true" class="btn sm secondary ba bg-white"><span class="text-base">Day</span></button>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn sm borderless"><span>Week</span></button>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn sm borderless"><span>Month</span></button>
</div>
// Segmented controls ([role=radiogroup][data-segmented]): the checked option is .secondary.ba.bg-white
// with .text-base on its text (if it has text); the others are .borderless.
// Arrow keys move the selection, like native radio buttons.
document.querySelectorAll("[data-segmented]").forEach((group) => {
const options = [...group.querySelectorAll("[role=radio]")];
const select = (option, focus) => {
options.forEach((o) => {
const on = o === option;
o.setAttribute("aria-checked", String(on));
o.tabIndex = on ? 0 : -1;
["secondary", "ba", "bg-white"].forEach((c) => o.classList.toggle(c, on));
o.classList.toggle("borderless", !on);
o.querySelector("span:not(.visually-hidden)")?.classList.toggle("text-base", on);
});
if (focus) option.focus();
};
options.forEach((o, i) => {
o.addEventListener("click", () => select(o));
o.addEventListener("keydown", (e) => {
const step = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[e.key];
if (!step) return;
e.preventDefault();
select(options[(i + step + options.length) % options.length], true);
});
});
});
Icons Only
Icon-only options are 32×32px; the selected one is 1px wider because of its border. Name each one with a <span class="visually-hidden"> placed before the icon (after the icon, the label adds 4px to the button width), so screen readers read "Phone", "Tablet" and "Desktop". Give each one a tooltip with the same name (data-tooltip, see Tooltips).
View codeHide code
<div class="d-inline-flex gap-4 bg-n1 rounded" role="radiogroup" aria-label="Preview size" data-segmented>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn sm borderless" data-tooltip="Phone"><span class="visually-hidden">Phone</span><svg class="icon" aria-hidden="true"><use href="#common-cellphone"/></svg></button>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn sm borderless" data-tooltip="Tablet"><span class="visually-hidden">Tablet</span><svg class="icon" aria-hidden="true"><use href="#common-tablet"/></svg></button>
<button type="button" role="radio" aria-checked="true" class="btn sm secondary ba bg-white" data-tooltip="Desktop"><span class="visually-hidden">Desktop</span><svg class="icon" aria-hidden="true"><use href="#common-monitor"/></svg></button>
</div>
// Segmented controls ([role=radiogroup][data-segmented]): the checked option is .secondary.ba.bg-white
// with .text-base on its text (if it has text); the others are .borderless.
// Arrow keys move the selection, like native radio buttons.
document.querySelectorAll("[data-segmented]").forEach((group) => {
const options = [...group.querySelectorAll("[role=radio]")];
const select = (option, focus) => {
options.forEach((o) => {
const on = o === option;
o.setAttribute("aria-checked", String(on));
o.tabIndex = on ? 0 : -1;
["secondary", "ba", "bg-white"].forEach((c) => o.classList.toggle(c, on));
o.classList.toggle("borderless", !on);
o.querySelector("span:not(.visually-hidden)")?.classList.toggle("text-base", on);
});
if (focus) option.focus();
};
options.forEach((o, i) => {
o.addEventListener("click", () => select(o));
o.addEventListener("keydown", (e) => {
const step = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[e.key];
if (!step) return;
e.preventDefault();
select(options[(i + step + options.length) % options.length], true);
});
});
});
Choice Pickers
A choice picker is a set of radiobuttons that look like buttons, e.g. for picking a time slot. Each option is a .btn.md (40px) or .btn.sm (32px) button with role="radio": the selected one is .primary, the others are .secondary. Put them in a wrapping row with .d-flex.flex-wrap.gap-8.
Use a choice picker when the options are short and people should see them all at once, e.g. in a card. For options in a form, use radiobuttons. To switch how content is displayed, use a segmented control.
Name the group with role="radiogroup" and aria-labelledby. Only the selected option is in the tab order (tabindex="-1" on the others; if nothing is selected yet, leave the first one without it). The script in the JS tab selects an option on click and moves the selection with the arrow keys.
Medium
Tour time
View codeHide code
<div>
<p id="choice-time-label" class="mb-8">Tour time</p>
<div class="d-flex flex-wrap gap-8" role="radiogroup" aria-labelledby="choice-time-label" data-choice-picker>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn md secondary"><span>10–10:30 AM</span></button>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn md secondary"><span>11–11:30 AM</span></button>
<button type="button" role="radio" aria-checked="true" class="btn md primary"><span>2–2:30 PM</span></button>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn md secondary"><span>3:30–4 PM</span></button>
</div>
</div>
// Choice pickers ([role=radiogroup][data-choice-picker]): buttons with role="radio". The checked
// option is .primary, the others .secondary. Arrow keys move the selection, like native radio buttons.
document.querySelectorAll("[data-choice-picker]").forEach((group) => {
const options = [...group.querySelectorAll("[role=radio]")];
const select = (option, focus) => {
options.forEach((o) => {
const on = o === option;
o.setAttribute("aria-checked", String(on));
o.tabIndex = on ? 0 : -1;
o.classList.toggle("primary", on);
o.classList.toggle("secondary", !on);
});
if (focus) option.focus();
};
options.forEach((o, i) => {
o.addEventListener("click", () => select(o));
o.addEventListener("keydown", (e) => {
const step = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[e.key];
if (!step) return;
e.preventDefault();
select(options[(i + step + options.length) % options.length], true);
});
});
});
Small
Use .btn.sm (32px) where space is tight, e.g. in a narrow card or next to other small controls. Use the same size for all options in a group.
Tour time
View codeHide code
<div>
<p id="choice-time-small-label" class="text-md mb-8">Tour time</p>
<div class="d-flex flex-wrap gap-8" role="radiogroup" aria-labelledby="choice-time-small-label" data-choice-picker>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn sm secondary"><span>10–10:30 AM</span></button>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn sm secondary"><span>11–11:30 AM</span></button>
<button type="button" role="radio" aria-checked="true" class="btn sm primary"><span>2–2:30 PM</span></button>
<button type="button" role="radio" aria-checked="false" tabindex="-1" class="btn sm secondary"><span>3:30–4 PM</span></button>
</div>
</div>
// Choice pickers ([role=radiogroup][data-choice-picker]): buttons with role="radio". The checked
// option is .primary, the others .secondary. Arrow keys move the selection, like native radio buttons.
document.querySelectorAll("[data-choice-picker]").forEach((group) => {
const options = [...group.querySelectorAll("[role=radio]")];
const select = (option, focus) => {
options.forEach((o) => {
const on = o === option;
o.setAttribute("aria-checked", String(on));
o.tabIndex = on ? 0 : -1;
o.classList.toggle("primary", on);
o.classList.toggle("secondary", !on);
});
if (focus) option.focus();
};
options.forEach((o, i) => {
o.addEventListener("click", () => select(o));
o.addEventListener("keydown", (e) => {
const step = { ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 }[e.key];
if (!step) return;
e.preventDefault();
select(options[(i + step + options.length) % options.length], true);
});
});
});
Toggles
View codeHide code
<div class="d-flex">
<label class="toggle align-self-center mr-12">
<input type="checkbox" name="toggle-name" id="toggle-id">
<span class="toggle-btn"></span>
</label>
<label for="toggle-id">Toggle label</label>
</div>Toggle with a Description
Put the label and the description in a column next to the toggle. Link the description to the input with aria-describedby, so screen readers read it after the label.
Use the underlined text style to display links.
View codeHide code
<div class="toggle-container d-flex align-items-center">
<label class="toggle mr-12">
<input type="checkbox" name="toggle-name" id="toggle-underline" aria-describedby="toggle-underline-help">
<span class="toggle-btn"></span>
</label>
<div class="d-flex flex-column justify-content-center">
<label for="toggle-underline">Underline links</label>
<p class="text-md text-quiet text-wrap-all" id="toggle-underline-help">Use the underlined text style to display links.</p>
</div>
</div>Sliders
A slider is a range input with the slider attribute. It fills the track up to the current value. It comes in one size: a 4px track and a 16px knob.
The visible knob is smaller than the thumb you can grab: the touch target is 24×24px, which meets the WCAG 2.2 minimum target size.
Place it in a flex container and add .flex-grow: the slider has negative side margins so the knob lines up with the ends of the track, and it needs to grow into that space. Show the current value next to the label with an <output>. A short script sets the --value variable that fills the track.
View codeHide code
<div class="d-flex justify-content-between align-items-center">
<label for="slider-speed" class="text-lg">Speed</label>
<output for="slider-speed" class="text-md text-quiet">1.0x</output>
</div>
<div class="d-flex">
<input type="range" slider class="flex-grow" id="slider-speed" min="0" max="2" step="0.1" value="1">
</div>
input[type="range"][slider] {
--value: 0%; /* how much of the track is filled; set by the script below */
--height: 20px;
--track-height: 4px;
--thumb-size: 24px; /* touch target */
--knob-size: calc(var(--thumb-size) * 0.65); /* visible white knob */
--overhang: calc((var(--thumb-size) - var(--knob-size)) / 2);
width: 100%;
height: var(--height);
margin-left: calc(var(--overhang) * -1);
margin-right: calc(var(--overhang) * -1);
position: relative;
appearance: none;
outline: none;
background: transparent;
}
/* Track (Chrome, Safari, Edge) */
input[type="range"][slider]::before {
content: "";
position: absolute;
top: 50%;
left: var(--overhang);
width: calc(100% - var(--overhang) * 2);
height: var(--track-height);
translate: 0 -50%;
border-radius: 999rem;
background: linear-gradient(90deg, var(--input-color) var(--value), var(--n2-transp) var(--value), var(--n2-transp) 100%);
}
input[type="range"][slider]::-webkit-slider-runnable-track {
height: var(--track-height);
}
/* Track (Firefox) */
input[type="range"][slider]::-moz-range-track {
height: var(--track-height);
margin-left: var(--overhang);
margin-right: var(--overhang);
border-radius: 999rem;
background: linear-gradient(90deg, var(--input-color) var(--value), var(--n2-transp) var(--value), var(--n2-transp) 100%);
}
/* Thumb: a large touch target that draws a smaller white knob with a shadow */
input[type="range"][slider]::-webkit-slider-thumb {
position: relative;
width: var(--thumb-size);
height: var(--thumb-size);
margin-top: calc((var(--track-height) / 2) + (var(--thumb-size) / 2) * -1);
appearance: none;
border: none;
border-radius: 50%;
cursor: pointer;
background:
radial-gradient(circle at 50% 50%, white 0%, white 46%, rgba(0, 0, 0, 0.08) 49%, transparent 58%),
radial-gradient(circle at 50% 58%, rgba(0, 0, 0, 0.17) 0%, transparent 59%);
}
input[type="range"][slider]::-moz-range-thumb {
position: relative;
left: calc(var(--thumb-size) / 4);
width: var(--thumb-size);
height: var(--thumb-size);
border: none;
border-radius: 50%;
cursor: pointer;
background:
radial-gradient(circle at 50% 50%, white 0%, white 46%, rgba(0, 0, 0, 0.08) 49%, transparent 58%),
radial-gradient(circle at 50% 58%, rgba(0, 0, 0, 0.17) 0%, transparent 59%);
}
input[type="range"][slider]:focus-visible::-webkit-slider-thumb {
box-shadow: 0 0 0 2px var(--focus-outline-color);
}
input[type="range"][slider]:focus-visible::-moz-range-thumb {
box-shadow: 0 0 0 2px var(--focus-outline-color);
}
// Fill the track up to the current value
document.querySelectorAll("input[type=range][slider]").forEach((input) => {
const update = () => {
const min = Number(input.min) || 0;
const max = input.max === "" ? 100 : Number(input.max);
input.style.setProperty("--value", ((input.valueAsNumber - min) / (max - min)) * 100 + "%");
};
input.addEventListener("input", update);
update();
});
Steppers
A stepper changes a number by one, e.g. the number of guests or tickets. It's a gray group (.bg-n1.rounded) of two borderless icon-only buttons with the value between them. It comes in two sizes: small uses .btn.xs buttons (28px) and 14px text (.text-md), medium uses .btn.md buttons (40px) and 16px text.
Show the value in an <output aria-live="polite"> so screen readers announce the change, and name the group with role="group" and aria-labelledby. Set the limits with data-min and data-max. Pressing and holding a button keeps changing the value: it starts repeating after half a second and speeds up after ten steps.
The value uses font-variant-numeric: tabular-nums;: all digits have the same width, so the stepper doesn't change width as the number changes. At a limit, the button gets .disabled and aria-disabled="true" instead of the disabled attribute, so it keeps keyboard focus (and its tooltip). Give the buttons “Decrease” and “Increase” tooltips (data-tooltip, see Tooltips).
Small
View codeHide code
<div class="d-flex align-items-center gap-12">
<span id="stepper-small-label" class="text-md">Tickets</span>
<div class="d-inline-flex align-items-center bg-n1 rounded" role="group" aria-labelledby="stepper-small-label" data-stepper data-min="1" data-max="10">
<button type="button" class="btn xs borderless disabled" aria-disabled="true" aria-label="Decrease" data-tooltip="Decrease"><svg class="icon" aria-hidden="true" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M4 12a1 1 0 0 1 1-1h14a1 1 0 1 1 0 2H5a1 1 0 0 1-1-1z" fill-rule="evenodd"/></svg></button>
<output class="px-8 text-md" style="font-variant-numeric: tabular-nums;" aria-live="polite">1</output>
<button type="button" class="btn xs borderless" aria-disabled="false" aria-label="Increase" data-tooltip="Increase"><svg class="icon" aria-hidden="true" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M12 4a1 1 0 0 1 1 1v6h6a1 1 0 1 1 0 2h-6v6a1 1 0 1 1-2 0v-6H5a1 1 0 1 1 0-2h6V5a1 1 0 0 1 1-1z" fill-rule="evenodd"/></svg></button>
</div>
</div>
// Steppers ([data-stepper]): the buttons change the value between data-min and data-max.
// At a limit the button gets .disabled and aria-disabled, so it keeps keyboard focus.
// Press and hold a button to keep stepping: it repeats after 500ms, then every 100ms, then faster.
document.querySelectorAll("[data-stepper]").forEach((stepper) => {
const [decrease, increase] = stepper.querySelectorAll("button");
const output = stepper.querySelector("output");
const min = Number(stepper.dataset.min ?? -Infinity);
const max = Number(stepper.dataset.max ?? Infinity);
let timer;
const update = (value) => {
output.textContent = value;
[[decrease, value <= min], [increase, value >= max]].forEach(([button, off]) => {
button.classList.toggle("disabled", off);
button.setAttribute("aria-disabled", String(off));
});
};
// Returns false at a limit, which ends the repeat.
const step = (by) => {
const value = Number(output.textContent) + by;
if (value < min || value > max) return false;
update(value);
return true;
};
const stop = () => clearTimeout(timer);
[[decrease, -1], [increase, 1]].forEach(([button, by]) => {
button.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
stop();
if (!step(by)) return;
let delay = 100, count = 0;
const repeat = () => {
if (!step(by)) return;
if (++count === 10) delay = 50; // speed up after 10 steps
timer = setTimeout(repeat, delay);
};
timer = setTimeout(repeat, 500);
});
["pointerup", "pointerleave", "pointercancel"].forEach((type) => button.addEventListener(type, stop));
button.addEventListener("contextmenu", (event) => event.preventDefault()); // long press on touch screens
// Pointer presses are handled above; a click with detail 0 comes from the keyboard (Enter or Space).
button.addEventListener("click", (event) => {
if (event.detail === 0) step(by);
});
});
update(Number(output.textContent));
});
Medium
View codeHide code
<div class="d-flex align-items-center gap-12">
<span id="stepper-medium-label">Guests</span>
<div class="d-inline-flex align-items-center bg-n1 rounded" role="group" aria-labelledby="stepper-medium-label" data-stepper data-min="1" data-max="8">
<button type="button" class="btn md borderless" aria-disabled="false" aria-label="Decrease" data-tooltip="Decrease"><svg class="icon" aria-hidden="true" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M4 12a1 1 0 0 1 1-1h14a1 1 0 1 1 0 2H5a1 1 0 0 1-1-1z" fill-rule="evenodd"/></svg></button>
<output class="px-8" style="font-variant-numeric: tabular-nums;" aria-live="polite">2</output>
<button type="button" class="btn md borderless" aria-disabled="false" aria-label="Increase" data-tooltip="Increase"><svg class="icon" aria-hidden="true" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path clip-rule="evenodd" d="M12 4a1 1 0 0 1 1 1v6h6a1 1 0 1 1 0 2h-6v6a1 1 0 1 1-2 0v-6H5a1 1 0 1 1 0-2h6V5a1 1 0 0 1 1-1z" fill-rule="evenodd"/></svg></button>
</div>
</div>
// Steppers ([data-stepper]): the buttons change the value between data-min and data-max.
// At a limit the button gets .disabled and aria-disabled, so it keeps keyboard focus.
// Press and hold a button to keep stepping: it repeats after 500ms, then every 100ms, then faster.
document.querySelectorAll("[data-stepper]").forEach((stepper) => {
const [decrease, increase] = stepper.querySelectorAll("button");
const output = stepper.querySelector("output");
const min = Number(stepper.dataset.min ?? -Infinity);
const max = Number(stepper.dataset.max ?? Infinity);
let timer;
const update = (value) => {
output.textContent = value;
[[decrease, value <= min], [increase, value >= max]].forEach(([button, off]) => {
button.classList.toggle("disabled", off);
button.setAttribute("aria-disabled", String(off));
});
};
// Returns false at a limit, which ends the repeat.
const step = (by) => {
const value = Number(output.textContent) + by;
if (value < min || value > max) return false;
update(value);
return true;
};
const stop = () => clearTimeout(timer);
[[decrease, -1], [increase, 1]].forEach(([button, by]) => {
button.addEventListener("pointerdown", (event) => {
if (event.button !== 0) return;
stop();
if (!step(by)) return;
let delay = 100, count = 0;
const repeat = () => {
if (!step(by)) return;
if (++count === 10) delay = 50; // speed up after 10 steps
timer = setTimeout(repeat, delay);
};
timer = setTimeout(repeat, 500);
});
["pointerup", "pointerleave", "pointercancel"].forEach((type) => button.addEventListener(type, stop));
button.addEventListener("contextmenu", (event) => event.preventDefault()); // long press on touch screens
// Pointer presses are handled above; a click with detail 0 comes from the keyboard (Enter or Space).
button.addEventListener("click", (event) => {
if (event.detail === 0) step(by);
});
});
update(Number(output.textContent));
});
File Upload
A file upload is a drop zone (.dropzone-button) that opens the file picker on click and accepts files dragged onto it. Its text goes in .text-1 and .text-2. The stylesheet handles the states: a blue border and text on hover and while files are dragged over it (add .dragover in your script), a focus ring, and a faded look when it's disabled.
Keep the real <input type="file"> hidden and open it from the drop zone. Name the whole control with role="group" and aria-labelledby, and link the size limit to the drop zone with aria-describedby.
Each file shows up as a chip above the drop zone (the space below the chips is a margin on each chip, so an empty list adds none): a loader while it uploads, then a page icon. Long names are cut at 150px and shown in full in the title. The button on the chip cancels the upload or removes the file; give it a hidden label with the file name and a “Cancel” or “Remove” tooltip (data-tooltip, see Tooltips). The script switches the tooltip when the upload finishes.
Default
Pick or drop files to try it. The script in the JS tab adds the chips; replace its fakeUpload with your own upload request.
Attachments
View codeHide code
<div role="group" aria-labelledby="upload-files-label" data-file-upload>
<p id="upload-files-label" class="form-label required mb-4">Attachments</p>
<input type="file" id="upload-files-input" class="hidden" multiple tabindex="-1">
<ul class="d-flex flex-wrap gap-x-8 list-unstyled" data-file-list>
</ul>
<button type="button" class="dropzone-button" aria-describedby="upload-files-help">
<svg class="icon size-32" aria-hidden="true"><use href="#common-upload"/></svg>
<span class="text-1">Drag and drop files here or click to browse</span>
<span class="text-2" id="upload-files-help">Max file size: 200 MB</span>
</button>
<template>
<li class="d-flex align-items-center pa-8 mb-8 ba rounded bg-white">
<span class="d-flex align-items-center justify-content-center size-24 mr-8"><span class="loader d-flex" role="status" aria-label="Uploading" data-uploading><svg class="size-20" viewBox="0 0 66 66" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><g><animateTransform attributeName="transform" type="rotate" values="0 33 33;270 33 33" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/><circle fill="none" stroke-width="11%" stroke-linecap="round" cx="33" cy="33" r="30" stroke-dasharray="187" stroke-dashoffset="610"><animateTransform attributeName="transform" type="rotate" values="0 33 33;135 33 33;450 33 33" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/><animate attributeName="stroke-dashoffset" values="187;46.75;187" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/></circle></g></svg></span><span class="d-flex hidden" data-uploaded><svg class="icon size-24 color-brand" aria-hidden="true"><use href="#common-page"/></svg></span></span>
<span class="text-md text-truncate mr-4" style="max-width: 150px;" data-file-name></span>
<button type="button" class="btn sm borderless" data-tooltip="Cancel"><span class="visually-hidden"></span><svg class="icon" aria-hidden="true"><use href="#internal-close"/></svg></button>
</li>
</template>
</div>
// File upload ([data-file-upload]): the drop zone opens the hidden file input, and files can also
// be dropped on it. Each file gets a chip from the <template>: a loader while it uploads, then a
// page icon. The button on the chip cancels the upload or removes the file.
// Replace fakeUpload with your own upload request.
const fakeUpload = (file) => new Promise((resolve) => setTimeout(resolve, 1500));
document.querySelectorAll("[data-file-upload]").forEach((upload) => {
const input = upload.querySelector("input[type=file]");
const zone = upload.querySelector(".dropzone-button");
const list = upload.querySelector("[data-file-list]");
const template = upload.querySelector("template");
const addFile = (file) => {
const chip = template.content.firstElementChild.cloneNode(true);
const name = chip.querySelector("[data-file-name]");
const button = chip.querySelector("button");
const label = button.querySelector(".visually-hidden");
name.textContent = file.name;
name.title = file.name;
label.textContent = `Cancel upload of ${file.name}`;
button.dataset.tooltip = "Cancel";
button.addEventListener("click", () => chip.remove());
list.append(chip);
fakeUpload(file).then(() => {
if (!chip.isConnected) return; // canceled
chip.querySelector("[data-uploading]").classList.add("hidden");
chip.querySelector("[data-uploaded]").classList.remove("hidden");
label.textContent = `Remove ${file.name}`;
button.dataset.tooltip = "Remove";
});
};
zone.addEventListener("click", () => input.click());
input.addEventListener("change", () => {
[...input.files].forEach(addFile);
input.value = ""; // lets people pick the same file again
});
zone.addEventListener("dragover", (e) => {
e.preventDefault();
zone.classList.add("dragover");
});
zone.addEventListener("dragleave", () => zone.classList.remove("dragover"));
zone.addEventListener("drop", (e) => {
e.preventDefault();
zone.classList.remove("dragover");
[...e.dataTransfer.files].forEach(addFile);
});
});
With Files
The first file is still uploading; the others are uploaded.
Attachments
- Photo 1.jpg
- File 2.pdf
- Archived folder with a really long name.zip
View codeHide code
<div role="group" aria-labelledby="upload-progress-label" data-file-upload>
<p id="upload-progress-label" class="form-label required mb-4">Attachments</p>
<input type="file" id="upload-progress-input" class="hidden" multiple tabindex="-1">
<ul class="d-flex flex-wrap gap-x-8 list-unstyled" data-file-list>
<li class="d-flex align-items-center pa-8 mb-8 ba rounded bg-white">
<span class="d-flex align-items-center justify-content-center size-24 mr-8"><span class="loader d-flex" role="status" aria-label="Uploading"><svg class="size-20" viewBox="0 0 66 66" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><g><animateTransform attributeName="transform" type="rotate" values="0 33 33;270 33 33" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/><circle fill="none" stroke-width="11%" stroke-linecap="round" cx="33" cy="33" r="30" stroke-dasharray="187" stroke-dashoffset="610"><animateTransform attributeName="transform" type="rotate" values="0 33 33;135 33 33;450 33 33" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/><animate attributeName="stroke-dashoffset" values="187;46.75;187" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/></circle></g></svg></span></span>
<span class="text-md text-truncate mr-4" style="max-width: 150px;" title="Photo 1.jpg">Photo 1.jpg</span>
<button type="button" class="btn sm borderless" data-tooltip="Cancel"><span class="visually-hidden">Cancel upload of Photo 1.jpg</span><svg class="icon" aria-hidden="true"><use href="#internal-close"/></svg></button>
</li>
<li class="d-flex align-items-center pa-8 mb-8 ba rounded bg-white">
<span class="d-flex align-items-center justify-content-center size-24 mr-8"><svg class="icon size-24 color-brand" aria-hidden="true"><use href="#common-page"/></svg></span>
<span class="text-md text-truncate mr-4" style="max-width: 150px;" title="File 2.pdf">File 2.pdf</span>
<button type="button" class="btn sm borderless" data-tooltip="Remove"><span class="visually-hidden">Remove File 2.pdf</span><svg class="icon" aria-hidden="true"><use href="#internal-close"/></svg></button>
</li>
<li class="d-flex align-items-center pa-8 mb-8 ba rounded bg-white">
<span class="d-flex align-items-center justify-content-center size-24 mr-8"><svg class="icon size-24 color-brand" aria-hidden="true"><use href="#common-page"/></svg></span>
<span class="text-md text-truncate mr-4" style="max-width: 150px;" title="Archived folder with a really long name.zip">Archived folder with a really long name.zip</span>
<button type="button" class="btn sm borderless" data-tooltip="Remove"><span class="visually-hidden">Remove Archived folder with a really long name.zip</span><svg class="icon" aria-hidden="true"><use href="#internal-close"/></svg></button>
</li>
</ul>
<button type="button" class="dropzone-button" aria-describedby="upload-progress-help">
<svg class="icon size-32" aria-hidden="true"><use href="#common-upload"/></svg>
<span class="text-1">Drag and drop files here or click to browse</span>
<span class="text-2" id="upload-progress-help">Max file size: 200 MB</span>
</button>
<template>
<li class="d-flex align-items-center pa-8 mb-8 ba rounded bg-white">
<span class="d-flex align-items-center justify-content-center size-24 mr-8"><span class="loader d-flex" role="status" aria-label="Uploading" data-uploading><svg class="size-20" viewBox="0 0 66 66" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"><g><animateTransform attributeName="transform" type="rotate" values="0 33 33;270 33 33" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/><circle fill="none" stroke-width="11%" stroke-linecap="round" cx="33" cy="33" r="30" stroke-dasharray="187" stroke-dashoffset="610"><animateTransform attributeName="transform" type="rotate" values="0 33 33;135 33 33;450 33 33" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/><animate attributeName="stroke-dashoffset" values="187;46.75;187" begin="0s" dur="1.4s" fill="freeze" repeatCount="indefinite"/></circle></g></svg></span><span class="d-flex hidden" data-uploaded><svg class="icon size-24 color-brand" aria-hidden="true"><use href="#common-page"/></svg></span></span>
<span class="text-md text-truncate mr-4" style="max-width: 150px;" data-file-name></span>
<button type="button" class="btn sm borderless" data-tooltip="Cancel"><span class="visually-hidden"></span><svg class="icon" aria-hidden="true"><use href="#internal-close"/></svg></button>
</li>
</template>
</div>Disabled
After the form is submitted, disable the drop zone and remove the buttons from the chips.
Attachments
- Photo 1.jpg
- File 2.pdf
- Archived folder with a really long name.zip
View codeHide code
<div role="group" aria-labelledby="upload-disabled-label">
<p id="upload-disabled-label" class="form-label required mb-4">Attachments</p>
<ul class="d-flex flex-wrap gap-x-8 list-unstyled">
<li class="d-flex align-items-center pa-8 mb-8 ba rounded bg-white">
<span class="d-flex align-items-center justify-content-center size-24 mr-8"><svg class="icon size-24 color-brand" aria-hidden="true"><use href="#common-page"/></svg></span>
<span class="text-md text-truncate pr-4" style="max-width: 150px;" title="Photo 1.jpg">Photo 1.jpg</span>
</li>
<li class="d-flex align-items-center pa-8 mb-8 ba rounded bg-white">
<span class="d-flex align-items-center justify-content-center size-24 mr-8"><svg class="icon size-24 color-brand" aria-hidden="true"><use href="#common-page"/></svg></span>
<span class="text-md text-truncate pr-4" style="max-width: 150px;" title="File 2.pdf">File 2.pdf</span>
</li>
<li class="d-flex align-items-center pa-8 mb-8 ba rounded bg-white">
<span class="d-flex align-items-center justify-content-center size-24 mr-8"><svg class="icon size-24 color-brand" aria-hidden="true"><use href="#common-page"/></svg></span>
<span class="text-md text-truncate pr-4" style="max-width: 150px;" title="Archived folder with a really long name.zip">Archived folder with a really long name.zip</span>
</li>
</ul>
<button type="button" class="dropzone-button" aria-describedby="upload-disabled-help" disabled>
<svg class="icon size-32" aria-hidden="true"><use href="#common-upload"/></svg>
<span class="text-1">Drag and drop files here or click to browse</span>
<span class="text-2" id="upload-disabled-help">Max file size: 200 MB</span>
</button>
</div>