Component · in flow
Form fields
Select, TextField and TextArea: native controls in one look, with a label, a hint and an error that assistive technology reads with them.
States
Needed for the price.
Choose a price group.
Enter a whole e-mail address.
Search and suffix
One caret
The Select's chevron, also on a Popover's or Menu's button with caret. It turns while open.
Anything here.
Astro
---
import Select from '@subfloor/astro/Select.astro'
import TextField from '@subfloor/astro/TextField.astro'
---
<Select id="group" name="group" label="Price group" value="shops"
options={[
{ value: 'shops', label: 'Shops' },
{ value: 'public', label: 'Public' },
]} />
<!-- or your own options and groups -->
<Select id="plan" name="plan" label="Plan">
<optgroup label="Paid"><option value="pro">Pro</option></optgroup>
</Select>
<TextField id="email" name="email" type="email" label="E-mail"
hint="We only use it to reply." error={errors.email} required />
<TextField id="q" name="q" type="search" label="Find a service" hideLabel
placeholder="Find a service" clearLabel="Clear the search" />
<TextField id="budget" name="budget" label="Budget" suffix="Kč" short />React
import { Select, TextArea, TextField } from '@subfloor/react'
<Select
label="Price group"
error={errors.group}
options={groups}
{...register('group')} // native props and ref go to the <select>
/>
<TextField label="E-mail" type="email" autoComplete="email" />
<TextArea label="Message" rows={6} />
// Controlled: onValueChange also gets what was entered before React hydrated.
<TextField type="search" label="Find a service" hideLabel
placeholder="Find a service" value={query} onValueChange={setQuery} />
// The same caret on a button
<Menu label="Actions" menuLabel="Actions" caret items={items} />Pick one to continue.
Size:
With a caret on its button.
For each order.
Changes:
Markup
<div class="sf-field">
<label class="sf-field__label" for="group">Price group</label>
<p class="sf-field__hint" id="group-hint">Sets the discount.</p>
<div class="sf-select">
<select class="sf-field__control sf-select__control" id="group" name="group"
aria-describedby="group-hint group-error" aria-invalid="true">…</select>
<span class="sf-caret" aria-hidden="true"></span>
</div>
<p class="sf-field__error" id="group-error" role="alert">Choose a group.</p>
</div>
<!-- Search. The label is hidden on screen. Keep the placeholder, a space if you have none:
without the attribute, the clear button also shows while the field is empty. -->
<div class="sf-field">
<label class="sf-field__label" for="q" data-hidden>Find a service</label>
<div class="sf-field__search">
<input class="sf-field__control" type="search" id="q" placeholder=" ">
<button type="button" class="sf-field__clear" aria-label="Clear">×</button>
</div>
</div>
<!-- A short field (data-short) with a suffix. -->
<div class="sf-field" data-short>
<label class="sf-field__label" for="budget">Budget</label>
<div class="sf-field__row">
<input class="sf-field__control" id="budget" aria-describedby="budget-suffix">
<span class="sf-field__suffix" id="budget-suffix">Kč</span>
</div>
</div>
<script type="module">
import { initClearButtons } from '@subfloor/core'
// Makes the clear buttons work, and shows them in place of the browser's.
initClearButtons()
</script>Behaviour
- The controls are native: forms, validation, autofill, the keyboard and
<optgroup>work as usual. Native attributes, andclass, go to the control;fieldClass(React:fieldClassName) to the wrapper. - The label names the control; the hint, the error and your own
aria-describedbydescribe it. An error marks itaria-invalid(even if you pass another value) and is an alert, for an error that appears later. One already there when the page loads, as after a server-side check, may not be announced on its own; it is still read with its field. - In React,
onValueChangegets each new value, and once one entered before React hydrated.onChangemisses that one, and a controlled field then loses it at its next render. A parent that keeps its value gets it back. - A search field has a clear button while it has text that can be changed. It empties the field, sends
inputandchange, and gives focus back. Name it withclearLabel. It replaces the browser’s own button, which only some browsers have. In plain HTML, give the field aplaceholder, a space if you have none, or the button also shows while it is empty. React and Astro do this for you. Escape stays the browser’s: some clear the field with it. - For a date, use DateField, and for a time TimeField.
suffixis beside the field, below it where there is no room, and also describes it.shortis for a few characters.hideLabelhides the label on screen only; it is still the name. A placeholder does not replace a label: it goes once there is text. Use it only where the purpose is clear without the label.- The closed select looks the same everywhere. The open list has the kit's look where the browser supports it (
appearance: base-select), also on phones. Elsewhere it is the browser's own list, or the phone's picker (Firefox, Safari before 27). - Where only the closed box is styled, a long value is cut with an ellipsis; where the list is styled too, it wraps. Either way it stays clear of the caret.
- Controls are at least 44px tall with text of at least 16px, so iOS does not zoom in. An error has a mark and a thicker border, not only a colour.
Without JavaScript
Everything on this page works except the turning caret on Popover and Menu buttons and the clear button of search fields, which need the script. There the browser’s own clear button stays, if it has one. React fields need React to render them.
Customisation
:root {
--sf-field-border-color: #64748b; /* keep 3:1 against the background */
--sf-field-short-width: 8rem;
--sf-caret-size: 1.25rem;
--sf-caret-color: var(--brand);
}| Token | Default |
|---|---|
--sf-radius-control | 8px |
--sf-color-danger | #b91c1c |
--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-caret-size | 1rem |
--sf-caret-gap | var(--sf-space-2) |
--sf-caret-color | currentcolor |
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.