Form
Field
A label wired up to a control of your own. Bring any input, select, or element you like.
- Element
<slab-field>- Built on
<label>- As Markdown
- field.md
Example
Here's a styled version, <slab-styled-field>. 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-field>
<label>Mix strength</label>
<select slot="control">
<option>3,000 psi</option>
<option selected>4,000 psi</option>
<option>5,000 psi</option>
</select>
<span slot="description">Field labels a control of your own, here a native select.</span>
</slab-styled-field>
<slab-field>
<label>Mix strength</label>
<select slot="control">
<option>3,000 psi</option>
<option selected>4,000 psi</option>
<option>5,000 psi</option>
</select>
<span slot="description">Field labels a control of your own, here a native select.</span>
</slab-field>
Usage
pnpm add slab-elements lit
<script type="module">
import 'slab-elements/field.js';
</script>
<slab-field required>
<label>Website</label>
<input slot="control" type="url" />
<span slot="description">Where people can find you.</span>
</slab-field>
Behavior
A label and the control it names, with no visual styles.
Field wires the two together. Both are slotted, so they're in the same tree and no ID reference has to cross a shadow root: Field gives the control an id and points the label's for at it. A for or an id that's already set is left alone, so a consumer who wants to wire it themselves can.
Beyond that, what Field provides is a place for each piece and custom states for what a design needs to know about them: :state(disabled), :state(error), and :state(required).
required renders nothing. A design that wants an asterisk adds one from outside, through ::part(label). Layout is the same: where the label sits relative to its control is for the styles around it, not for this class.
hide-label hides the label visually and leaves it where screenreaders and a <label for> can still reach it.
API
<slab-field>
| Name | Type | Default | Description |
|---|---|---|---|
disabled
|
boolean |
false |
|
error
|
boolean |
false |
|
hide-label
|
boolean |
false |
|
required
|
boolean |
false |
|
version
|
string |
| Name | Type | Description |
|---|---|---|
(default)
|
Element | string |
|
control
|
Element |
|
description
|
Element | string |
| Name | Description |
|---|---|
control
|
The control the label names |
description
|
Text about the control, such as a hint or an error |
label
|
The label |
Copy and own it
<slab-styled-field> 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.
field.ts
import { customElement } from 'lit/decorators.js';
import Field from 'slab-elements/field.js';
import styles from './field.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-field': StyledField;
}
}
/**
* Field, styled: a slab-serif label over whatever control is slotted in,
* which takes Text Field's look.
*/
@customElement('slab-styled-field')
export default class StyledField extends Field {
static override styles = [tokens, styles];
}
field.styles.ts
import { css } from 'lit';
/*
Field labels a control of the consumer's own, slotted in, so the control is
styled through "::slotted()": Text Field's look, for an input or a select.
*/
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~='control'] {
display: grid;
}
::slotted([slot='control']) {
background-color: var(--paper);
block-size: 3rem;
border: var(--border);
border-radius: 0;
box-sizing: border-box;
color: inherit;
font: inherit;
padding: 0 0.75rem;
transition:
border-color 100ms,
box-shadow 100ms;
}
/* The outline is transparent rather than gone, for forced colors. */
::slotted([slot='control']:focus-visible) {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
}
:host(:state(error)) ::slotted([slot='control']) {
border-color: var(--ember-deep);
}
:host(:state(disabled)) ::slotted([slot='control']) {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
cursor: not-allowed;
}
[part~='description'] {
color: var(--ash);
font-size: 0.875rem;
}
::slotted([slot='description']) {
margin-block-start: 0.375rem;
}
:host(:state(error)) [part~='description'] {
color: var(--ember-deep);
font-weight: 600;
}
@media (prefers-reduced-motion: reduce) {
::slotted([slot='control']) {
transition: 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);
}
`;