Selection
Listbox
A list of Options to pick one or more from, right on the page instead of in a popup.
- Element
<slab-listbox>- Built on
[role=listbox]
Example
Here's a styled version, <slab-styled-listbox>. It's not
in the package — it's an example of what you can build with the
headless element. Copy it and make it yours at the
bottom of this page. Switch to "As it ships" to see the same markup
with the headless element and no CSS at all. That's your starting
point.
<slab-styled-listbox label="Crew" multiple>
<slab-styled-option value="ana" selected>Ana, finisher</slab-styled-option>
<slab-styled-option value="ben">Ben, pump operator</slab-styled-option>
<slab-styled-option value="chen">Chen, form setter</slab-styled-option>
<slab-styled-option value="dee">Dee, laborer</slab-styled-option>
<slab-styled-option value="eli" disabled>Eli, on leave</slab-styled-option>
</slab-styled-listbox>
<slab-listbox label="Crew" multiple>
<slab-option value="ana" selected>Ana, finisher</slab-option>
<slab-option value="ben">Ben, pump operator</slab-option>
<slab-option value="chen">Chen, form setter</slab-option>
<slab-option value="dee">Dee, laborer</slab-option>
<slab-option value="eli" disabled>Eli, on leave</slab-option>
</slab-listbox>
Usage
pnpm add slab-elements lit
import 'slab-elements/listbox.js';
import 'slab-elements/option.js';
import 'slab-elements/option-group.js';
export function MyListbox() {
return (
<slab-listbox label="Size" name="size">
<slab-option value="s">Small</slab-option>
<slab-option value="m" selected>Medium</slab-option>
<slab-option value="l">Large</slab-option>
</slab-listbox>
);
}
MyListbox.jsx React 19 or later, which sets props on custom elements as properties.
import 'slab-elements/listbox.js';
import 'slab-elements/option.js';
import 'slab-elements/option-group.js';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-listbox')
export class MyListbox extends LitElement {
render() {
return html`
<slab-listbox label="Size" name="size">
<slab-option value="s">Small</slab-option>
<slab-option value="m" selected>Medium</slab-option>
<slab-option value="l">Large</slab-option>
</slab-listbox>
`;
}
}
my-listbox.ts Slab is built on Lit, so its elements are at home in Lit's templates.
<script setup>
import 'slab-elements/listbox.js';
import 'slab-elements/option.js';
import 'slab-elements/option-group.js';
</script>
<template>
<slab-listbox label="Size" name="size">
<slab-option value="s">Small</slab-option>
<slab-option value="m" selected>Medium</slab-option>
<slab-option value="l">Large</slab-option>
</slab-listbox>
</template>
MyListbox.vue Let Vue's compiler know slab- tags are custom elements with isCustomElement.
<script>
import 'slab-elements/listbox.js';
import 'slab-elements/option.js';
import 'slab-elements/option-group.js';
</script>
<slab-listbox label="Size" name="size">
<slab-option value="s">Small</slab-option>
<slab-option value="m" selected>Medium</slab-option>
<slab-option value="l">Large</slab-option>
</slab-listbox>
MyListbox.svelte Svelte binds properties and events to custom elements like any other element.
import 'slab-elements/listbox.js';
import 'slab-elements/option.js';
import 'slab-elements/option-group.js';
export const MyListbox = <template>
<slab-listbox label="Size" name="size">
<slab-option value="s">Small</slab-option>
<slab-option value="m" selected>Medium</slab-option>
<slab-option value="l">Large</slab-option>
</slab-listbox>
</template>;
MyListbox.gjs Ember sets attributes, and a dynamic value like {{this.max}} sets the property when the element has one. {{on}} listens for any event by its name.
<script type="module">
import 'slab-elements/listbox.js';
import 'slab-elements/option.js';
import 'slab-elements/option-group.js';
</script>
<slab-listbox label="Size" name="size">
<slab-option value="s">Small</slab-option>
<slab-option value="m" selected>Medium</slab-option>
<slab-option value="l">Large</slab-option>
</slab-listbox>
index.html Any bundler (or an import map) takes care of the imports.
Option, documented here, is also what Select and Combobox use.
To group Options under a heading, put them in a slab-option-group with a label. It’s a [role=group] named by its label, and its Options are still the Listbox’s, so the keys and selection work across groups as across the rest. Select and Combobox take Option Groups too, and Combobox hides one while its filter hides every Option in it.
<slab-listbox label="Produce">
<slab-option-group label="Fruit">
<slab-option>Apple</slab-option>
<slab-option>Banana</slab-option>
</slab-option-group>
<slab-option-group label="Vegetables">
<slab-option>Carrot</slab-option>
</slab-option-group>
</slab-listbox>
Long lists
For thousands of Options, set virtual. Only the Options in or near view are laid out, and spacers take the room of the rest, so the scrollbar is the size it would be with all of them. The Options are still all there, so the keys, typeahead, selection and the form value reach every one, and the list scrolls to whichever becomes active. Each Option that’s rendered gets aria-posinset and aria-setsize, so a screenreader still says “4 of 5,000”.
Give ::part(listbox) a block size so it scrolls. Options are taken to be the same height as each other, which is measured from the ones on screen.
<style>
slab-listbox::part(listbox) {
block-size: 20rem;
}
</style>
<slab-listbox label="City" virtual>
<slab-option>Aachen</slab-option>
<slab-option>Aalborg</slab-option>
<!-- And thousands more -->
</slab-listbox>
Keyboard
| Key | What it does |
|---|---|
| Up, Down | Moves the active Option, stopping at each end and skipping disabled ones. |
| Home, End | Moves to the first or last Option. |
| A character | Moves to the next Option whose label starts with what’s typed. |
| Enter, Space | Selects the active Option, or toggles it when multiple is set. |
With multiple, Shift with Up or Down moves and toggles, Shift with Space
selects from the last selected Option to the active one, Control or Command
with Shift and Home or End selects from the active one to the first or last,
and Control or Command with A selects every Option, or none.
Behavior
<slab-listbox>
A list of Options to select one or more from, with no visual styles.
There's no native listbox: <select> draws its own options, and they can't be anything but text. So this is a <div role="listbox"> and ARIA does the rest. Not a <ul>: a list may only hold list items, which neither a slot nor an Option is, and the role would replace its semantics anyway.
Options are found by class rather than by tag name, so your subclass of Option can be named anything.
Focus stays on the listbox, and the active Option is pointed at with aria-activedescendant rather than focused, the way the APG's own listbox does it. It's what lets a Combobox keep focus in its input while the user moves through the Options, and a Select keep it on its button. Read or set the active Option with activeOption.
The arrow keys, Home and End move the active Option, stopping at each end and skipping disabled Options. Typing moves it to the next Option whose label starts with what was typed. Space, Enter, and a click select it, or toggle it when multiple is set. Then, as in the APG's multi-select listbox, Shift with an arrow key moves and toggles, Shift with Space selects every Option from the one last selected to the active one, Control or Command with Shift and Home or End selects every Option from the active one to the first or last and moves there, and Control or Command with A selects every Option, or none when all of them already are.
Style it through its parts and through :state(disabled), :state(error), and :state(required), and each Option through :state(active), :state(disabled), and :state(selected).
<slab-option>
An Option with no visual styles. The host is the option, so style it directly.
It's never focused. Focus stays on its Listbox, which points at the active Option with aria-activedescendant, so a Select or a Combobox can keep focus where it is while the user moves through the Options.
Its Listbox sets :state(active). It sets :state(disabled), which is also set while its Listbox is disabled, and :state(selected) itself.
<slab-option-group>
An Option Group with no visual styles: a named [role=group] of Options, such as "Fruit" and "Vegetables", inside a Listbox, Select or Combobox.
The host is the group, so style it directly. Its Options are still its Listbox's, Select's or Combobox's, so the arrow keys, typeahead and selection work across them as across the rest. A Combobox hides an Option Group while its filter hides every Option in it.
Its label is rendered, for sighted users, and hidden from screenreaders, which hear it as the group's name from aria-label instead. hide-label hides it visually too.
API
<slab-listbox>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
Names it. It's the accessible name, and the visible label where there is one. | |
disabled
|
boolean |
false |
Disables it, as disabled does a native control: the user can't focus or use it, and a form control's value isn't submitted. |
hide-label
|
boolean |
false |
Hides the label visually and leaves it as the accessible name, for a design that shows the label some other way. |
multiple
|
boolean |
false |
Lets more than one be selected at a time. |
name
|
string |
'' |
The name its value is submitted under, as a native control's name. |
required
|
boolean |
false |
Makes it invalid while it has no value, as required does a native control. |
virtual
|
boolean |
false |
Lays out only the Options in or near view, for a Listbox with thousands. Give ::part(listbox) a block size so it scrolls. Options are taken to be the same height as each other. |
value
|
string |
'' |
The value of the first selected and enabled Option. Setting it selects the Option with that value, and only that one. Not reflected, to match a native control. |
custom-validity
|
string |
Makes it invalid with this message, as setCustomValidity() does, and shows the message at once rather than once validity is reported, for an error the page already knows about, such as one from a server. Removing it, or setting it to an empty string, makes it valid again, unless something else makes it invalid. |
| Name | Type | Description |
|---|---|---|
defaultValue
|
string |
The value it selects when none of its Options has selected, and goes back to when its form resets. It's the value attribute, so setting it sets the attribute, and value with it. |
activeOption
|
Option | null |
The Option the user is on, which Space, Enter and typeahead act from. It's pointed at rather than focused, so focus stays where it is. |
form
Read-only
|
HTMLFormElement | null |
The form it belongs to, or null. |
labels
Read-only
|
NodeList |
The <label> elements associated with it, as a native control's labels: each <label for> that names its id, and a <label> around it. |
options
Read-only
|
Option[] |
Its Options, in order. |
selectedOptions
Read-only
|
Option[] |
Selected and enabled Options, in the order they appear. |
validationMessage
Read-only
|
string |
What's wrong, as the validity message says it: the message given to setCustomValidity() or setValidity(), or else the browser's own. Empty while it's valid. |
validity
Read-only
|
ValidityState |
Its ValidityState, as a native control's. |
willValidate
Read-only
|
boolean |
Whether its validity is checked when its form submits. It isn't while it's disabled or read-only. |
values
|
string[] |
The value of every selected and enabled Option, in the order they appear. Setting it selects the Options with those values, and only those. |
| Signature | Description |
|---|---|
checkValidity(): boolean
|
Whether its value is valid. Dispatches "invalid" when it isn't, as a native control's does. |
focus(options?: FocusOptions): void
|
Focuses it, or the part of it that takes focus. |
reportValidity(): boolean
|
Whether its value is valid. When it isn't, dispatches "invalid" and shows the validity message, as a native control's does. |
resetValidityFeedback(): void
|
Clears the validity feedback without changing validity. |
setCustomValidity(message: string): void
|
Makes it invalid with a message of your own, or valid again with an empty one, as a native control's does. |
setValidity(flags?: ValidityStateFlags, message?: string): void
|
Sets its validity and message, as ElementInternals.setValidity() does, for checks of your own beyond setCustomValidity(). |
| Name | Type | Description |
|---|---|---|
input
|
Event |
As the user changes its value, as a native control's does. Setting it from code doesn't dispatch it. |
change
|
Event |
When the user changes its value, as a native control's does. Setting it from code doesn't dispatch it. |
invalid
|
Event |
When a check finds its value invalid, as a native control's does. Cancel it to leave the validity message unshown, after a blur, a submit or reportValidity(), and focus where it is. |
| Name | Type | Description |
|---|---|---|
(default)
|
Option | OptionGroup |
|
description
|
Element | string |
Text that describes it, such as a hint or what the validity message means. It's its accessible description. |
validity-message
|
Element | string |
Shown in place of the validity message's text while it's invalid and the message shows, for wording of your own, a translation, or a link. It's part of its accessible description, and validationMessage stays the browser's or setCustomValidity()'s. |
| Name | Description |
|---|---|
description
|
Text about the Listbox, and its validity message when it has one |
label
|
The label |
listbox
|
The Options |
validity-message
|
The validity message, which says why the value is invalid |
| Name | Description |
|---|---|
:state(disabled)
|
While it's disabled. |
:state(error)
|
While it's invalid and its validity message shows, which is once its validity is reported, as when its form is submitted. |
:state(required)
|
While it's required, for styles such as an asterisk. |
<slab-option>
| Name | Type | Default | Description |
|---|---|---|---|
label
|
string |
Names the Option in place of its text, and is what typeahead matches against. | |
disabled
|
boolean |
false |
Disables it, as disabled does a native control: the user can't focus or use it, and a form control's value isn't submitted. |
selected
|
boolean |
false |
Like a native option, the attribute is the default state and the property is the current one. Style :state(selected) rather than [selected]. |
value
|
string |
'' |
Its label, or its text when it has no label, when not set. The same as a native option. |
| Name | Type | Description |
|---|---|---|
defaultSelected
|
boolean |
Whether it starts selected, and is selected again when its form resets. It's the selected attribute, as a native option's defaultSelected is, so setting it sets the attribute, and selected with it. |
text
Read-only
|
string |
What the user reads as the Option's name: its label, or its text when it has no label. |
| Name | Type | Description |
|---|---|---|
(default)
|
Element | string |
The label, plus anything else a design puts in an Option |
| Name | Description |
|---|---|
:state(active)
|
While it's the active one, which the keyboard moves through while focus stays on its container. |
:state(disabled)
|
While it's disabled. |
:state(selected)
|
While it's selected. |
<slab-option-group>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
Names it. It's the accessible name, and the visible label where there is one. | |
hide-label
|
boolean |
false |
Hides the label visually and leaves it as the accessible name, for a design that shows the label some other way. |
| Name | Type | Description |
|---|---|---|
(default)
|
Option |
| Name | Description |
|---|---|
label
|
The label |
Copy and own it
<slab-styled-listbox> isn't in the package. It's an example —
copy it into your project and change whatever you want. Keyboard,
focus, ARIA, forms, and events all come from the headless element,
and the styles only reach in through ::part() and
:state(), same as yours would.
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-listbox> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Each component registers its headless element and imports its CSS. Use it like the element: its props are the element's attributes and properties.
Listbox.jsx
import 'slab-elements/listbox.js';
import './listbox.css';
import 'slab-elements/option.js';
import './option.css';
import 'slab-elements/option-group.js';
import './option-group.css';
export function Listbox(props) {
return <slab-listbox {...props} />;
}
export function Option(props) {
return <slab-option {...props} />;
}
export function OptionGroup(props) {
return <slab-option-group {...props} />;
}
listbox.css
slab-listbox {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
slab-listbox::part(label) {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
slab-listbox:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
/*
Focus stays on the listbox and the active Option shows where the keyboard
is, so the listbox's own focus is the shadow, as Text Field's is.
*/
slab-listbox::part(listbox) {
background-color: var(--paper);
border: var(--border);
max-block-size: 16rem;
overflow-y: auto;
padding: 0.25rem 0;
transition: box-shadow 100ms;
}
slab-listbox::part(listbox):focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
}
slab-listbox:state(error)::part(listbox) {
border-color: var(--ember-deep);
}
slab-listbox:state(disabled)::part(listbox) {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
}
/*
Spaced by what's in it rather than by a gap, so a description with
nothing in it takes no room.
*/
slab-listbox::part(description) {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
slab-listbox > [slot='description'] {
margin-block-start: 0.375rem;
}
slab-listbox::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-listbox::part(validity-message)::before {
content: '■ ' / '';
}
@media (prefers-reduced-motion: reduce) {
slab-listbox::part(listbox) {
transition: none;
}
}
option.css
slab-option {
align-items: center;
border-inline-start: 4px solid transparent;
cursor: pointer;
display: flex;
font-family: var(--font-body);
gap: 0.75rem;
justify-content: space-between;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
/*
Before ":state(active)", which wins when both apply. Hovering doesn't make
an Option active, as in the APG's select-only combobox, so the keyboard's
place stays visible beside the pointer's.
*/
slab-option:hover {
background-color: var(--chalk);
}
slab-option:state(active) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
slab-option:state(selected) {
font-weight: 600;
}
/* The check: a corner of border, turned. */
slab-option:state(selected)::after {
block-size: 0.625rem;
border-block-end: 2px solid currentcolor;
border-inline-end: 2px solid currentcolor;
content: '';
flex-shrink: 0;
inline-size: 0.3125rem;
rotate: 45deg;
translate: 0 -0.125rem;
}
slab-option:state(disabled) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
slab-option:state(disabled):state(active) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
slab-option {
transition: none;
}
}
option-group.css
slab-option-group {
border-block-start: var(--border);
}
slab-option-group::part(label) {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
tokens.css
:root {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
Under the hood, it's just the headless element, subclassed,
with styles set. That's it. To use it, copy
these files, swap the tag in @customElement()
for one of your own, and import it.
tokens.styles.ts holds the colors and fonts
every styled example shares, so you only need it once.
listbox.ts
import { customElement } from 'lit/decorators.js';
import Listbox from 'slab-elements/listbox.js';
import styles from './listbox.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-listbox': StyledListbox;
}
}
/**
* Listbox, styled: its Options in an ink-bordered box that casts an ember
* shadow while it's focused.
*/
@customElement('slab-styled-listbox')
export default class StyledListbox extends Listbox {
static override styles = [tokens, styles];
}
listbox.styles.ts
import { css } from 'lit';
export default css`
:host {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
[part~='label'] {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
:host(:state(required)) [part~='label']::after {
color: var(--ember-deep);
content: ' *' / '';
}
/*
Focus stays on the listbox and the active Option shows where the keyboard
is, so the listbox's own focus is the shadow, as Text Field's is.
*/
[part~='listbox'] {
background-color: var(--paper);
border: var(--border);
max-block-size: 16rem;
overflow-y: auto;
padding: 0.25rem 0;
transition: box-shadow 100ms;
&:focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
}
}
:host(:state(error)) [part~='listbox'] {
border-color: var(--ember-deep);
}
:host(:state(disabled)) [part~='listbox'] {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
}
/*
Spaced by what's in it rather than by a gap, so a description with
nothing in it takes no room.
*/
[part~='description'] {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
::slotted([slot='description']) {
margin-block-start: 0.375rem;
}
[part~='validity-message'] {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
&::before {
content: '■ ' / '';
}
}
@media (prefers-reduced-motion: reduce) {
[part~='listbox'] {
transition: none;
}
}
`;
option.ts
import { customElement } from 'lit/decorators.js';
import Option from 'slab-elements/option.js';
import styles from './option.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-option': StyledOption;
}
}
/**
* Option, styled: a row that inverts to paper on ink while it's active, with
* an ember bar on its edge, and a check while it's selected.
*/
@customElement('slab-styled-option')
export default class StyledOption extends Option {
static override styles = [tokens, styles];
}
option.styles.ts
import { css } from 'lit';
export default css`
:host {
align-items: center;
border-inline-start: 4px solid transparent;
cursor: pointer;
display: flex;
font-family: var(--font-body);
gap: 0.75rem;
justify-content: space-between;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
/*
Before ":state(active)", which wins when both apply. Hovering doesn't make
an Option active, as in the APG's select-only combobox, so the keyboard's
place stays visible beside the pointer's.
*/
:host(:hover) {
background-color: var(--chalk);
}
:host(:state(active)) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
:host(:state(selected)) {
font-weight: 600;
}
/* The check: a corner of border, turned. */
:host(:state(selected))::after {
block-size: 0.625rem;
border-block-end: 2px solid currentcolor;
border-inline-end: 2px solid currentcolor;
content: '';
flex-shrink: 0;
inline-size: 0.3125rem;
rotate: 45deg;
translate: 0 -0.125rem;
}
:host(:state(disabled)) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
:host(:state(disabled):state(active)) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
:host {
transition: none;
}
}
`;
option-group.ts
import { customElement } from 'lit/decorators.js';
import OptionGroup from 'slab-elements/option-group.js';
import styles from './option-group.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-option-group': StyledOptionGroup;
}
}
/**
* Option Group, styled: a small ash label over its Options, ruled off from
* the rows before it.
*/
@customElement('slab-styled-option-group')
export default class StyledOptionGroup extends OptionGroup {
static override styles = [tokens, styles];
}
option-group.styles.ts
import { css } from 'lit';
export default css`
:host {
border-block-start: var(--border);
}
[part~='label'] {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
`;
tokens.styles.ts
import { css } from 'lit';
/*
The styled components' palette and type, taken from the logo: ink for the
courses, ember for the base, paper behind them.
Each is a short alias for a "--slab-styled-*" property, with the default as
its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
which every shadow root inherits. Were the defaults declared under those
names here instead, each host's own declaration would shadow the page's.
Each default is "light-dark()", so the components follow the page's
"color-scheme": "dark" for dark, "light" for light, and "light dark" to
follow the operating system. Ink and paper trade places in the dark, so
ink is always the text and paper always what's behind it. Contrast is given
as light, then dark.
*/
export default css`
:host {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
`;
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-listbox> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Each component registers its headless element and imports its CSS. Use it like the element: its props are the element's attributes and properties.
Listbox.vue
<script setup>
import 'slab-elements/listbox.js';
import './listbox.css';
</script>
<template>
<slab-listbox><slot /></slab-listbox>
</template>
Option.vue
<script setup>
import 'slab-elements/option.js';
import './option.css';
</script>
<template>
<slab-option><slot /></slab-option>
</template>
OptionGroup.vue
<script setup>
import 'slab-elements/option-group.js';
import './option-group.css';
</script>
<template>
<slab-option-group><slot /></slab-option-group>
</template>
listbox.css
slab-listbox {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
slab-listbox::part(label) {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
slab-listbox:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
/*
Focus stays on the listbox and the active Option shows where the keyboard
is, so the listbox's own focus is the shadow, as Text Field's is.
*/
slab-listbox::part(listbox) {
background-color: var(--paper);
border: var(--border);
max-block-size: 16rem;
overflow-y: auto;
padding: 0.25rem 0;
transition: box-shadow 100ms;
}
slab-listbox::part(listbox):focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
}
slab-listbox:state(error)::part(listbox) {
border-color: var(--ember-deep);
}
slab-listbox:state(disabled)::part(listbox) {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
}
/*
Spaced by what's in it rather than by a gap, so a description with
nothing in it takes no room.
*/
slab-listbox::part(description) {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
slab-listbox > [slot='description'] {
margin-block-start: 0.375rem;
}
slab-listbox::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-listbox::part(validity-message)::before {
content: '■ ' / '';
}
@media (prefers-reduced-motion: reduce) {
slab-listbox::part(listbox) {
transition: none;
}
}
option.css
slab-option {
align-items: center;
border-inline-start: 4px solid transparent;
cursor: pointer;
display: flex;
font-family: var(--font-body);
gap: 0.75rem;
justify-content: space-between;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
/*
Before ":state(active)", which wins when both apply. Hovering doesn't make
an Option active, as in the APG's select-only combobox, so the keyboard's
place stays visible beside the pointer's.
*/
slab-option:hover {
background-color: var(--chalk);
}
slab-option:state(active) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
slab-option:state(selected) {
font-weight: 600;
}
/* The check: a corner of border, turned. */
slab-option:state(selected)::after {
block-size: 0.625rem;
border-block-end: 2px solid currentcolor;
border-inline-end: 2px solid currentcolor;
content: '';
flex-shrink: 0;
inline-size: 0.3125rem;
rotate: 45deg;
translate: 0 -0.125rem;
}
slab-option:state(disabled) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
slab-option:state(disabled):state(active) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
slab-option {
transition: none;
}
}
option-group.css
slab-option-group {
border-block-start: var(--border);
}
slab-option-group::part(label) {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
tokens.css
:root {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-listbox> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Import listbox.js once, such
as in your root layout, and use
<slab-listbox> as is. A Svelte component
around it would take slot="…" content as its
own slot and drop it.
listbox.js
import 'slab-elements/listbox.js';
import './listbox.css';
import 'slab-elements/option.js';
import './option.css';
import 'slab-elements/option-group.js';
import './option-group.css';
listbox.css
slab-listbox {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
slab-listbox::part(label) {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
slab-listbox:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
/*
Focus stays on the listbox and the active Option shows where the keyboard
is, so the listbox's own focus is the shadow, as Text Field's is.
*/
slab-listbox::part(listbox) {
background-color: var(--paper);
border: var(--border);
max-block-size: 16rem;
overflow-y: auto;
padding: 0.25rem 0;
transition: box-shadow 100ms;
}
slab-listbox::part(listbox):focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
}
slab-listbox:state(error)::part(listbox) {
border-color: var(--ember-deep);
}
slab-listbox:state(disabled)::part(listbox) {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
}
/*
Spaced by what's in it rather than by a gap, so a description with
nothing in it takes no room.
*/
slab-listbox::part(description) {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
slab-listbox > [slot='description'] {
margin-block-start: 0.375rem;
}
slab-listbox::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-listbox::part(validity-message)::before {
content: '■ ' / '';
}
@media (prefers-reduced-motion: reduce) {
slab-listbox::part(listbox) {
transition: none;
}
}
option.css
slab-option {
align-items: center;
border-inline-start: 4px solid transparent;
cursor: pointer;
display: flex;
font-family: var(--font-body);
gap: 0.75rem;
justify-content: space-between;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
/*
Before ":state(active)", which wins when both apply. Hovering doesn't make
an Option active, as in the APG's select-only combobox, so the keyboard's
place stays visible beside the pointer's.
*/
slab-option:hover {
background-color: var(--chalk);
}
slab-option:state(active) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
slab-option:state(selected) {
font-weight: 600;
}
/* The check: a corner of border, turned. */
slab-option:state(selected)::after {
block-size: 0.625rem;
border-block-end: 2px solid currentcolor;
border-inline-end: 2px solid currentcolor;
content: '';
flex-shrink: 0;
inline-size: 0.3125rem;
rotate: 45deg;
translate: 0 -0.125rem;
}
slab-option:state(disabled) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
slab-option:state(disabled):state(active) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
slab-option {
transition: none;
}
}
option-group.css
slab-option-group {
border-block-start: var(--border);
}
slab-option-group::part(label) {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
tokens.css
:root {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}
The same styles, written from outside the element, so
there's nothing to subclass. The CSS styles every
<slab-listbox> on the page. Import
tokens.css, the colors and fonts every styled
example shares, once, wherever your global styles go.
Each component registers its headless element and imports its CSS. Use it like the element: its attributes and its content go to the element.
Listbox.gjs
import 'slab-elements/listbox.js';
import './listbox.css';
export const Listbox = <template>
<slab-listbox ...attributes>{{yield}}</slab-listbox>
</template>;
Option.gjs
import 'slab-elements/option.js';
import './option.css';
export const Option = <template>
<slab-option ...attributes>{{yield}}</slab-option>
</template>;
OptionGroup.gjs
import 'slab-elements/option-group.js';
import './option-group.css';
export const OptionGroup = <template>
<slab-option-group ...attributes>{{yield}}</slab-option-group>
</template>;
listbox.css
slab-listbox {
color: var(--ink);
display: grid;
font-family: var(--font-body);
}
slab-listbox::part(label) {
font-family: var(--font-display);
font-size: 1.0625rem;
font-weight: 600;
margin-block-end: 0.375rem;
}
slab-listbox:state(required)::part(label)::after {
color: var(--ember-deep);
content: ' *' / '';
}
/*
Focus stays on the listbox and the active Option shows where the keyboard
is, so the listbox's own focus is the shadow, as Text Field's is.
*/
slab-listbox::part(listbox) {
background-color: var(--paper);
border: var(--border);
max-block-size: 16rem;
overflow-y: auto;
padding: 0.25rem 0;
transition: box-shadow 100ms;
}
slab-listbox::part(listbox):focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
}
slab-listbox:state(error)::part(listbox) {
border-color: var(--ember-deep);
}
slab-listbox:state(disabled)::part(listbox) {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
}
/*
Spaced by what's in it rather than by a gap, so a description with
nothing in it takes no room.
*/
slab-listbox::part(description) {
color: var(--ash);
display: grid;
font-size: 0.875rem;
}
slab-listbox > [slot='description'] {
margin-block-start: 0.375rem;
}
slab-listbox::part(validity-message) {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.375rem;
}
slab-listbox::part(validity-message)::before {
content: '■ ' / '';
}
@media (prefers-reduced-motion: reduce) {
slab-listbox::part(listbox) {
transition: none;
}
}
option.css
slab-option {
align-items: center;
border-inline-start: 4px solid transparent;
cursor: pointer;
display: flex;
font-family: var(--font-body);
gap: 0.75rem;
justify-content: space-between;
padding: 0.5rem 0.875rem 0.5rem 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
color 100ms;
}
/*
Before ":state(active)", which wins when both apply. Hovering doesn't make
an Option active, as in the APG's select-only combobox, so the keyboard's
place stays visible beside the pointer's.
*/
slab-option:hover {
background-color: var(--chalk);
}
slab-option:state(active) {
background-color: var(--ink);
border-inline-start-color: var(--ember);
color: var(--paper);
}
slab-option:state(selected) {
font-weight: 600;
}
/* The check: a corner of border, turned. */
slab-option:state(selected)::after {
block-size: 0.625rem;
border-block-end: 2px solid currentcolor;
border-inline-end: 2px solid currentcolor;
content: '';
flex-shrink: 0;
inline-size: 0.3125rem;
rotate: 45deg;
translate: 0 -0.125rem;
}
slab-option:state(disabled) {
color: var(--ash);
cursor: not-allowed;
text-decoration: line-through;
}
slab-option:state(disabled):state(active) {
background-color: var(--chalk);
color: var(--ash);
}
@media (prefers-reduced-motion: reduce) {
slab-option {
transition: none;
}
}
option-group.css
slab-option-group {
border-block-start: var(--border);
}
slab-option-group::part(label) {
color: var(--ash);
font-size: 0.75rem;
font-weight: 600;
letter-spacing: 0.08em;
padding: 0.5rem 0.875rem 0.25rem 1rem;
text-transform: uppercase;
}
tokens.css
:root {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
/*
Forced colors, such as Windows' contrast themes, replace every color that
isn't a system color, so ember fills and paper marks all become the same
background, and a checked Checkbox looks like an unchecked one. The palette
becomes the user's system colors instead, which are kept. Ember is the
highlight, the color of what's selected, and ember-deep, being for text and
for paper text on it, is the text color.
*/
@media (forced-colors: active) {
:host {
--ash: GrayText;
--chalk: Canvas;
--ember: Highlight;
--ember-deep: CanvasText;
--ink: CanvasText;
--paper: Canvas;
}
}