Form
Checkbox Group
A set of Checkboxes with one label, one value, and one validity between them.
- Element
<slab-checkbox-group>- Built on
[role=group]- As Markdown
- checkbox-group.md
Example
Here's a styled version, <slab-styled-checkbox-group>. 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-checkbox-group label="Before the pour">
<slab-styled-checkbox label="The forms are level" value="forms" checked></slab-styled-checkbox>
<slab-styled-checkbox label="The rebar is tied" value="rebar" checked></slab-styled-checkbox>
<slab-styled-checkbox label="The vapor barrier is down" value="barrier"></slab-styled-checkbox>
<slab-styled-checkbox label="The inspector signed off" value="inspection"></slab-styled-checkbox>
<span slot="description">Check them off as the crew does.</span>
</slab-styled-checkbox-group>
<slab-checkbox-group label="Before the pour">
<slab-checkbox label="The forms are level" value="forms" checked></slab-checkbox>
<slab-checkbox label="The rebar is tied" value="rebar" checked></slab-checkbox>
<slab-checkbox label="The vapor barrier is down" value="barrier"></slab-checkbox>
<slab-checkbox label="The inspector signed off" value="inspection"></slab-checkbox>
<span slot="description">Check them off as the crew does.</span>
</slab-checkbox-group>
Usage
pnpm add slab-elements lit
<script type="module">
import 'slab-elements/checkbox-group.js';
</script>
<slab-checkbox-group label="Toppings" name="toppings">
<slab-checkbox label="Cheese" value="cheese"></slab-checkbox>
<slab-checkbox label="Onions" value="onions"></slab-checkbox>
</slab-checkbox-group>
Each item is a Checkbox.
Keyboard
| Key | What it does |
|---|---|
| Tab | Moves between the Checkboxes, each its own stop. |
| Space | Checks or unchecks the focused one. |
Behavior
A group of Checkboxes with no visual styles, with one label and one validity for all of them.
Checkboxes are found by class rather than by tag name, so your subclass of Checkbox can be named anything. The group's value is the value of every Checkbox that is checked and enabled, in the order they were checked.
Style it through its parts and through :state(disabled), :state(error), and :state(required). How the Checkboxes are arranged is layout, so it's left to the styles around them.
API
<slab-checkbox-group>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
||
disabled
|
boolean |
false |
|
hide-label
|
boolean |
false |
|
name
|
string |
'' |
|
required
|
boolean |
false |
|
value
|
string[] |
[] |
The value of every checked and enabled Checkbox, in the order they were checked. Not reflected, to match a native control. |
version
|
string |
| Name | Type | Description |
|---|---|---|
form
Read-only
|
HTMLFormElement | null |
|
validity
Read-only
|
ValidityState |
| Signature | Description |
|---|---|
checkValidity(): boolean
|
|
focus(options?: FocusOptions): void
|
|
reportValidity(): boolean
|
|
resetValidityFeedback(): void
|
Clears the validity feedback without changing validity. |
setCustomValidity(message: string): void
|
|
setValidity(flags?: ValidityStateFlags, message?: string): void
|
| Name | Type | Description |
|---|---|---|
(default)
|
Checkbox |
|
description
|
Element | string |
| Name | Description |
|---|---|
description
|
Text about the group, and its validity message when it has one |
group
|
The Checkboxes |
label
|
The label |
validity-message
|
Copy and own it
<slab-styled-checkbox-group> 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.
checkbox-group.ts
import { customElement } from 'lit/decorators.js';
import CheckboxGroup from 'slab-elements/checkbox-group.js';
import styles from './checkbox-group.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-checkbox-group': StyledCheckboxGroup;
}
}
/**
* Checkbox Group, styled: a slab-serif label over its Checkboxes, stacked.
*/
@customElement('slab-styled-checkbox-group')
export default class StyledCheckboxGroup extends CheckboxGroup {
static override styles = [tokens, styles];
}
checkbox-group.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.75rem;
}
:host(:state(required)) [part~='label']::after {
color: var(--ember-deep);
content: ' *' / '';
}
[part~='group'] {
display: grid;
gap: 0.75rem;
}
/*
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.75rem;
}
[part~='validity-message'] {
color: var(--ember-deep);
font-weight: 600;
margin-block-start: 0.75rem;
&::before {
content: '■ ' / '';
}
}
`;
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);
}
`;