Component · in flow
Time field
A time, typed or chosen from the kit's own lists, over a native time field that keeps the value.
Minutes and limits
14:37 stays in the list while the field has it.
From 22:00 to 06:00.
States
When we may call.
This time is taken.
You can change it later.
Languages
Plain HTML
React
When we come.
Changes:
Usage
Astro:
---
import TimeField from '@subfloor/astro/TimeField.astro'
---
<TimeField id="start" name="start" label="Start" value="14:30"
min="09:00" max="17:00" minuteStep={15} required />
<!-- In another language: set it on the page or around the field, and translate our words. -->
<div lang="cs">
<TimeField id="zacatek" name="zacatek" label="Začátek" labels={{
open: 'Vybrat čas', close: 'Zavřít', hour: 'Hodina', minute: 'Minuta',
clear: 'Vymazat', invalid: 'Zadejte čas, například {example}.',
}} />
</div>React:
import { TimeField } from '@subfloor/react'
// Always text such as '14:30', or '' for none. Never a Date.
<TimeField label="Start" value={start} onValueChange={setStart}
min="09:00" max="17:00" minuteStep={15} labels={labels} />Markup
<div class="sf-field">
<label class="sf-field__label" for="start">Start</label>
<!-- data-minute-step: 1 to 60, by default 5; data-labels: our words as JSON -->
<div class="sf-time" data-sf-time>
<input class="sf-field__control sf-time__native" type="time" id="start"
name="start" value="14:30">
</div>
</div>
<script type="module">
import { createTimeField, initTimeFields } from '@subfloor/core'
initTimeFields()
// After a script set the value: createTimeField(element).sync()
</script>Behaviour
- The value is always text such as
14:30, in 24 hours, whatever is shown. It is never aDate: a time has no day and no time zone. A native field, hidden behind ours, keeps it: forms, reset,required,minandmaxwork as usual. Aminaftermaxgoes over midnight. - The language of the page (
lang, also around the field) decides between 24 hours and AM and PM. Type a time as that language writes it, in 24 hours, or with AM or PM:14:30,1430,14or2:30 pm. Where the language has AM and PM,2:30alone could be either, so the field asks for a clear one. It is read on Enter and when the field is left. Text that cannot be read stays and says so; until it is corrected, the field has no value, and the form is not sent. - The lists have every hour, and minutes in steps of 5. Set another step, from 1 to 60, with
minuteStep. Any minute can be typed, and one that is not in the list stays there while the lists are open. - An hour counts at once and leads on to the minutes; a minute closes the lists. If the hour does not allow the minute, it takes the nearest one in the list that it does. In an empty field, choose the hour first.
- On a computer, the button or Alt and the down arrow opens the lists in a popover. On a phone or tablet (
(pointer: coarse)), a tap on the field opens them in a sheet, without the keyboard. Swipe it down by its header to close it. - In the lists, the up and down arrows, Home and End move between the times that can be chosen; Tab or the left and right arrows move between the lists. Enter or Space chooses, Escape closes. Closing keeps an hour that was chosen.
- Clear is there only when the field has a time and is not
required. - In React, use
onValueChange. It also gets a time entered before React hydrated, and the hour as soon as it is chosen.refis the native field; focusing it focuses ours.classNameis on both. - Our own words are English until you translate them with
labels. The browser’s own messages are in the language of the browser. - In plain HTML, call
sync()after a script set the value. It follows attributes, the form’s reset, andinputandchangeevents of the native field by itself. - Put the label beside the field, with
for, not around it, as for a date field. - Seconds, durations and time zones are not supported. Check the time on the server too.
Without JavaScript
The browser’s own time field and picker, with any minute. The kit’s field also waits while someone uses that one, and takes over when they leave it.
Customisation
:root {
--sf-time-lists-rows: 7.5;
--sf-time-lists-selected-bg: var(--brand); /* keep 3:1 against the surface */
--sf-time-lists-selected-color: white;
}| Token | Default |
|---|---|
--sf-touch-target | 44px |
--sf-popover-min-width | 12rem |
--sf-popover-max-width | min(22rem, 100vw - 2 * var(--sf-overlay-gutter)) |
--sf-popover-max-height | min(22rem, 70dvh) |
--sf-popover-offset | var(--sf-space-1) |
--sf-radius-popover | var(--sf-radius-control) |
--sf-shadow-popover | 0 8px 24px rgb(0 0 0 / 16%) |
--sf-field-border-color | #71717a |
--sf-field-font-size | 1rem |
--sf-field-gap | var(--sf-space-1) |
--sf-field-padding | var(--sf-space-3) |
--sf-field-short-width | 6rem |
--sf-field-suffix-gap | var(--sf-space-2) |
--sf-time-lists-option-size | var(--sf-touch-target) |
--sf-time-lists-rows | 5.5 |
--sf-time-lists-padding | 6px |
--sf-time-lists-selected-bg | var(--sf-button-primary-bg) |
--sf-time-lists-selected-color | var(--sf-button-primary-color) |
Background
Content 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.
Content 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.