Selection
Select
A button that opens a list of Options to pick one or more from. It submits just like a <select>.
- Element
<slab-select>- Built on
[role=combobox] + [role=listbox]- As Markdown
- select.md
Example
Here's a styled version, <slab-styled-select>. 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-select label="Finish" placeholder="Pick a finish">
<slab-styled-option value="broom">Broom</slab-styled-option>
<slab-styled-option value="trowel">Hard trowel</slab-styled-option>
<slab-styled-option value="stamped">Stamped</slab-styled-option>
<slab-styled-option value="aggregate">Exposed aggregate</slab-styled-option>
<slab-styled-option value="polished" disabled>Polished</slab-styled-option>
</slab-styled-select>
<slab-select label="Finish" placeholder="Pick a finish">
<slab-option value="broom">Broom</slab-option>
<slab-option value="trowel">Hard trowel</slab-option>
<slab-option value="stamped">Stamped</slab-option>
<slab-option value="aggregate">Exposed aggregate</slab-option>
<slab-option value="polished" disabled>Polished</slab-option>
</slab-select>
Usage
pnpm add slab-elements lit
<script type="module">
import 'slab-elements/select.js';
</script>
<slab-select label="Country" name="country" placeholder="Choose one">
<slab-option value="ca">Canada</slab-option>
<slab-option value="mx">Mexico</slab-option>
<slab-option value="us">United States</slab-option>
</slab-select>
Its Options are the same ones Listbox uses.
Keyboard
| Key | What it does |
|---|---|
| Enter, Space, Up, Down, Home, End | Closed, opens it. |
| Alt+Down | Closed, opens it without moving the active Option. |
| A character | Closed, selects the next Option whose label starts with what’s typed, without opening it. |
| Enter, Space | Open, selects the active Option and closes, or toggles it and stays open when multiple is set. |
| Tab, Alt+Up | Open, selects the active Option and closes. Alt+Up keeps focus where it is. |
| Escape | Open, closes without selecting. |
Once it’s open, the rest of the keys are Listbox’s.
Behavior
A select with no visual styles: a button that opens a list of Options, to select one or more from.
It isn't a native <select>. A customizable one, with appearance: base-select, can hold rich options, but Firefox doesn't support it yet. And in any browser a <select> only counts the <option> elements in its own tree, so neither an Option nor anything slotted into it from the light DOM would ever be one of its options. So this is the APG's select-only combobox: an element with role="combobox" that opens a role="listbox" element in a popover. It submits, resets, restores and validates the way a <select> does.
Focus stays on the combobox the whole time. Its aria-activedescendant points at the active Option, so the user hears each Option as they move through them.
Closed, Enter, Space, the arrow keys, Home and End open it, and Alt with the Down arrow opens it without moving. Typing selects the next Option whose label starts with what was typed, without opening it, as a native <select> does. Open, the keys are a Listbox's, range selection with multiple included. Enter, Space and a click select an Option and close it, unless multiple is set, where they toggle the Option and leave it open. Tab selects the active Option and closes it, and Escape closes it without selecting anything. Alt with the Up arrow does what Tab does, without moving focus.
The popover is anchored to the combobox with CSS anchor positioning, the way Popover's is. placement picks the side and nothing else.
Style it through its parts and through :state(disabled), :state(error), :state(open), :state(placeholder-shown), and :state(required), and each Option through the states Option sets.
API
<slab-select>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
||
disabled
|
boolean |
false |
|
hide-label
|
boolean |
false |
|
multiple
|
boolean |
false |
|
name
|
string |
'' |
|
open
|
boolean |
false |
|
placeholder
|
string |
Shown in place of a value when no Option is selected. | |
placement
|
'bottom' | 'left' | 'right' | 'top' |
'bottom' |
The side of the combobox it opens on, when there's room. It flips when there isn't. |
required
|
boolean |
false |
|
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. |
version
|
string |
| Name | Type | Description |
|---|---|---|
form
Read-only
|
HTMLFormElement | null |
|
options
Read-only
|
Option[] |
|
selectedOptions
Read-only
|
Option[] |
Selected and enabled Options, in the order they appear. |
validity
Read-only
|
ValidityState |
|
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
|
|
focus(options?: FocusOptions): void
|
|
hide(): void
|
|
reportValidity(): boolean
|
|
resetValidityFeedback(): void
|
Clears the validity feedback without changing validity. |
setCustomValidity(message: string): void
|
|
setValidity(flags?: ValidityStateFlags, message?: string): void
|
|
show(): void
|
| Name | Type | Description |
|---|---|---|
input
|
Event |
|
change
|
Event |
| Name | Type | Description |
|---|---|---|
(default)
|
Option |
|
description
|
Element | string |
| Name | Description |
|---|---|
combobox
|
What opens the Options and shows what's selected |
description
|
Text about the Select, and its validity message when it has one |
label
|
The label |
listbox
|
The Options |
popover
|
What the Options open in |
validity-message
|
|
value
|
The selected Options' labels, or the placeholder when there are none |
Copy and own it
<slab-styled-select> isn't in the package. It's an example —
copy it into your project and change whatever you want. Under the
hood, it's just the headless element, subclassed, with
styles set. That's it. 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.
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.
select.ts
import { customElement } from 'lit/decorators.js';
import Select from 'slab-elements/select.js';
import styles from './select.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-select': StyledSelect;
}
}
/**
* Select, styled: Text Field's box with a chevron that turns over when it
* opens, and its Options in a panel that casts a hard shadow.
*/
@customElement('slab-styled-select')
export default class StyledSelect extends Select {
static override styles = [tokens, styles];
}
select.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: ' *' / '';
}
[part~='combobox'] {
align-items: center;
background-color: var(--paper);
border: var(--border);
box-sizing: border-box;
cursor: pointer;
display: flex;
gap: 0.75rem;
justify-content: space-between;
/*
A fixed height, shared with Text Field, so fields and the controls
beside them line up whatever the font.
*/
min-block-size: 3rem;
padding: 0 0.75rem;
transition:
border-color 100ms,
box-shadow 100ms;
/* The chevron: a corner of border, turned. */
&::after {
block-size: 0.5rem;
border-block-end: 2px solid currentcolor;
border-inline-end: 2px solid currentcolor;
content: '';
flex-shrink: 0;
inline-size: 0.5rem;
rotate: 45deg;
transition: rotate 150ms;
translate: 0 -0.125rem;
}
/*
Shadows only, without Text Field's lift. The combobox anchors the
popover, and the popover would move with it.
*/
&:focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
}
}
:host(:state(open)) [part~='combobox'] {
box-shadow: var(--shadow);
&::after {
rotate: 225deg;
translate: 0 0.125rem;
}
&:focus-visible {
box-shadow: 4px 4px 0 var(--ember);
}
}
:host(:state(placeholder-shown)) [part~='value'] {
color: var(--ash);
}
:host(:state(error)) [part~='combobox'] {
border-color: var(--ember-deep);
}
:host(:state(disabled)) [part~='combobox'] {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
cursor: not-allowed;
}
/* A closed popover isn't displayed, so this runs each time it opens. */
[part~='popover'] {
animation: drop 150ms ease-out;
background-color: var(--paper);
border: var(--border);
box-shadow: var(--shadow);
box-sizing: border-box;
color: var(--ink);
margin-block: 0.5rem;
max-block-size: 18rem;
min-inline-size: anchor-size(inline);
overflow-y: auto;
padding: 0.25rem 0;
}
/*
Spaced by what's in it rather than by a gap, so a description with
nothing in it takes no room, and a control beside the field lines up
with its bottom edge.
*/
[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: '■ ' / '';
}
}
@keyframes drop {
from {
opacity: 0;
translate: 0 -0.375rem;
}
}
@media (prefers-reduced-motion: reduce) {
[part~='combobox'],
[part~='combobox']::after {
transition: none;
}
[part~='popover'] {
animation: none;
}
}
`;
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);
}
`;