Field
Connects a label, a control, a hint and an error with the right ids and ARIA.
SourceStyles source@monoline/react 0.5.0Markdown for AI toolsstable
- Built on native elements
- 3 parts, named exports
- The label is bound to the control by id, so it has an accessible name.
- Hint and error are linked with aria-describedby; an error sets aria-invalid and is announced via role=alert.
- required sets the native required attribute on the control.
- Styles in one file:
03-form-fields.css
Import
import '@monoline/css';
import {Field} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add field
Usage
import {Field, Input} from '@monoline/react';
<div style={{maxWidth: 360}}>
<Field label="Email" hint="We never share it." required>
<Input type="email" placeholder="you@example.com" />
</Field>
</div>
Anatomy
Import the parts you need and nest them like this.
import {Field, invalidAttrs, useFieldControl} from '@monoline/react';
<Field>
<invalidAttrs />
<useFieldControl />
</Field>
Examples
Label and hint
A lone checkbox with an error
Leave the Field label for the group (here "Terms") and let the checkbox carry its own text. The Field supplies the error slot and aria-invalid.
Props
FieldProps
| Prop | Type | Default | Description |
|---|---|---|---|
label required | ReactNode | – | The visible label of the control. Required so every control has a name. |
hint | ReactNode | – | Help text shown under the control. |
error | ReactNode | – | Error message. Its presence marks the control invalid and it is announced politely. Inside a `Form` with `errors`, leave it out and set `name` instead. |
name | string | – | The `name` of the control. Inside a `Form` that has `errors`, the Field shows the message for this name, so you pass each error once. |
required | boolean | false | Marks the control as required: adds a required marker to the label and sets `required` on the control. Default false. |
children required | ReactNode | – | The control: Input, Textarea, Select trigger, etc. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[aria-checked="true"] | 03-form-fields.css |
[aria-invalid="true"] | 03-form-fields.css |
[aria-pressed="true"] | 03-form-fields.css |
[data-valid="true"] | 11-field-states.css |
CSS classes
| Class | Stylesheet |
|---|---|
.field | 03-form-fields.css |
.field-hint | 03-form-fields.css |
.input | 03-form-fields.css |
.input-bare | 03-form-fields.css |
.input-end | 03-form-fields.css |
.input-lg | 03-form-fields.css |
.input-sm | 03-form-fields.css |
.input-wrap | 03-form-fields.css |
.muted | 03-form-fields.css |
.seg | 03-form-fields.css |
.seg-opt | 03-form-fields.css |
.select | 03-form-fields.css |
.setting | 03-form-fields.css |
.switch | 03-form-fields.css |
.textarea | 03-form-fields.css |
.field-error | 11-field-states.css |
.field-ok | 11-field-states.css |
.field-required | 11-field-states.css |
.form-row | 11-field-states.css |
.input | 11-field-states.css |
.input-affix | 11-field-states.css |
.input-group | 11-field-states.css |
.select | 11-field-states.css |
.textarea | 11-field-states.css |
Customization
Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.
app.css
.field {
border-color: var(--accent);
}
.field[aria-checked="true"] {
background: var(--wash);
}
Accessibility
- The label is bound to the control by id, so it has an accessible name.
- Hint and error are linked with aria-describedby; an error sets aria-invalid and is announced via role=alert.
- required sets the native required attribute on the control.
- Inside a Form with errors, a Field with a matching name shows that error once; the form summary announces it, so the inline text is not a second alert.