Form
Time Field
A time field built on the native time input. The browser shows a 12- or 24-hour clock for the locale.
- Element
<slab-time-field>- Built on
<input type=time>- As Markdown
- time-field.md
Example
Here's a styled version, <slab-styled-time-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-time-field label="Pour time" min="06:00" max="14:00" step="900" value="07:30">
<span slot="description">Trucks leave the plant on the quarter hour.</span>
</slab-styled-time-field>
<slab-time-field label="Pour time" min="06:00" max="14:00" step="900" value="07:30">
<span slot="description">Trucks leave the plant on the quarter hour.</span>
</slab-time-field>
Usage
pnpm add slab-elements lit
<script type="module">
import 'slab-elements/time-field.js';
</script>
<slab-time-field label="Start time" name="start" min="09:00" max="17:00" required></slab-time-field>
Behavior
A time field with no visual styles, around a native <input type="time">, so it is typed into, stepped, and read the way one is. Any picker is the browser's own.
The value is the input's: a 24-hour "hh:mm" string, "hh:mm:ss" or "hh:mm:ss.sss" when it has seconds, or an empty one. Whether a person sees a 12-hour or a 24-hour clock is the browser's call for their locale, but what's submitted and read is always that one format.
The label and the input are both in the shadow root, so a plain for reference names it without crossing out of one. The host delegates focus, so clicking anywhere on it, or a <label for> pointing at it, focuses the input.
Validity is the native input's. required, min, max and step are passed through to it, and whatever it decides is mirrored onto the host, so valueMissing, rangeUnderflow, rangeOverflow, stepMismatch and badInput all mean what they mean on an <input>. That includes a max earlier than min, which is a range across midnight, such as a night shift. Nothing is computed twice.
Style it through its parts and through :state(disabled), :state(error), :state(readonly) and :state(required). Where the label sits relative to the input is layout, so it's left to the styles around it.
API
<slab-time-field>
| Name | Type | Default | Description |
|---|---|---|---|
label
Required
|
string |
||
autocomplete
|
HTMLInputElement['autocomplete'] |
||
disabled
|
boolean |
false |
|
hide-label
|
boolean |
false |
|
max
|
string |
The latest time allowed, as "hh:mm". Earlier than min, the range runs across midnight. |
|
min
|
string |
The earliest time allowed, as "hh:mm". Also where step counts from. |
|
name
|
string |
'' |
|
readonly
|
boolean |
false |
|
required
|
boolean |
false |
|
step
|
string |
In seconds, or "any". 60 when unset, so there's no seconds to fill in. Counted from min when there is one. |
|
value
|
string |
'' |
A 24-hour "hh:mm" string, with ":ss" and ".sss" when it has them. Like a native input, the attribute is the default value and the property is the current one, so the attribute doesn't change as the field is edited. |
version
|
string |
| Name | Type | Description |
|---|---|---|
form
Read-only
|
HTMLFormElement | null |
|
validity
Read-only
|
ValidityState |
|
valueAsDate
|
Date | null |
value as a Date on January 1, 1970, UTC, or null when empty, as on a native input. |
| Signature | Description |
|---|---|
checkValidity(): boolean
|
|
click(): void
|
|
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
|
|
showPicker(): void
|
Opens the browser's time picker, where it has one. It's the native input's, so it throws where that one does, such as without a user gesture. |
| Name | Type | Description |
|---|---|---|
input
|
Event |
|
change
|
Event |
| Name | Type | Description |
|---|---|---|
description
|
Element | string |
| Name | Description |
|---|---|
description
|
Text about the field, and its validity message when it has one |
input
|
|
label
|
The label |
validity-message
|
Copy and own it
<slab-styled-time-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.
time-field.ts
import { customElement } from 'lit/decorators.js';
import TimeField from 'slab-elements/time-field.js';
import textInput from './text-input.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-time-field': StyledTimeField;
}
}
/**
* Time Field, styled: Text Field's look, with the browser's own clock.
*/
@customElement('slab-styled-time-field')
export default class StyledTimeField extends TimeField {
static override styles = [tokens, textInput];
}
text-input.styles.ts
import { css } from 'lit';
/*
The look shared by everything typed into: Text Field, Textarea, Number, Date
Field, Time Field and Combobox, whose fields are "::part(input)", or
"::part(textarea)" for Textarea. File Drop starts from it too, for its label
and description.
*/
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~='input'],
[part~='textarea'] {
background-color: var(--paper);
block-size: 3rem;
border: var(--border);
border-radius: 0;
box-sizing: border-box;
color: inherit;
font: inherit;
/* A fixed height, shared with Select, so fields line up in a row. */
padding: 0 0.75rem;
transition:
background-color 100ms,
border-color 100ms,
box-shadow 100ms,
translate 100ms;
&::placeholder {
color: var(--ash);
}
/*
The outline is transparent rather than gone, so it still shows in forced
colors, where box shadows don't.
*/
&:focus-visible {
box-shadow: 4px 4px 0 var(--ember);
outline: 2px solid transparent;
translate: -2px -2px;
}
&:disabled {
background-color: var(--chalk);
border-style: dashed;
color: var(--ash);
cursor: not-allowed;
}
}
[part~='textarea'] {
block-size: auto;
min-block-size: 7rem;
padding: 0.625rem 0.75rem;
resize: vertical;
}
:host(:state(readonly)) [part~='input'],
:host(:state(readonly)) [part~='textarea'] {
background-color: var(--chalk);
}
:host(:state(error)) [part~='input'],
:host(:state(error)) [part~='textarea'] {
border-color: var(--ember-deep);
}
/*
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: '■ ' / '';
}
}
@media (prefers-reduced-motion: reduce) {
[part~='input'],
[part~='textarea'] {
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);
}
`;