Field
A wrapper that gives any input, including native and third-party ones, a label, a hint and messages.
Example
<div class="stack"> <focsd-field label="Email" hint="We reply within one working day"> </focsd-field> <focsd-field label="Size" required> <select name="size"> <option>Small</option> <option>Medium</option> </select> </focsd-field> <focsd-field label="Coupon code" error="This code expired on October 1. Try a current code."> <input name="coupon" value="AUTUMN" /> </focsd-field> <focsd-field label="Delivery notes" warning="Add a gate code if the building has one."> <div slot="control" role="textbox" contenteditable="true" aria-multiline="true"></div> </focsd-field></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-field.js';</script>
<focsd-field label="Email" hint="We reply within one working day">…</focsd-field>import { Field } from '@focsd-dev/react';
export function Example() { return <Field label="Email" hint="We reply within one working day">…</Field>;}<script setup>import '@focsd-dev/components/focsd-field.js';</script>
<template> <focsd-field label="Email" hint="We reply within one working day">…</focsd-field></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-field.js';
// template<focsd-field label="Email" hint="We reply within one working day">…</focsd-field><script> import '@focsd-dev/components/focsd-field.js';</script>
<focsd-field label="Email" hint="We reply within one working day">…</focsd-field>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
label | label | string | — |
hint | hint | string | — |
error | error | string | undefined | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
required | required | boolean | false |
Slots
| Slot | Description |
|---|---|
(default) | The control (an input, select, textarea or a form-associated element). |
control | A control that is not a native or form-associated element, such as a role="textbox" element. |
label | The generated label. |
hint | The generated hint. |
message | The generated error, warning or success message. |
CSS parts
Style the inside of the component with ::part(), for example focsd-field::part(layout).
| Part | Description |
|---|---|
layout | The row or column holding the label and the body. |
label | The wrapper around the generated label and the required marker. |
body | The wrapper around the control, the hint and the message. |
marker | The required marker (hidden from assistive technology). |
announcer | The visually hidden status region that announces a changed message. |
Accessibility
- The label is a real
<label for>for labelable controls (native inputs and form-associated elements); for any other[slot="control"]element it setsaria-labelledby. - The hint and message are light-DOM siblings of the control, referenced by
aria-describedby(shadow roots cannot share IDs). The field keeps anyaria-describedbyids you set. errorusessetCustomValiditywhere the control has it, so it blocks submission, and setsaria-invalid; clearing it undoes both.warningandsuccessnever block.- A message that changes after the first render is announced politely; one present at first render is only described.
- The required marker is hidden from assistive technology: set
requiredon the control itself so the name and state are read. - One control per field: only the first is labeled and described, so use one field per control.
- The field has no role of its own. Author content is never replaced; generated nodes carry
data-focsd-field.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.