Label
Slant / Forms
A control's name, with a required mark, a hint and an action on the same line.
Demo
Usage
import { Field, Input, Label } from '@tuttn/slant'
// On its own, a Label names a control through htmlFor and the control's id.
<Label htmlFor="project" required>Project name</Label>
<Input id="project" required />
// A hint is read as part of the name: "Branch Optional".
<Label htmlFor="branch" hint="Optional">Branch</Label>
<Input id="branch" />
// An action sits at the inline end of the label's line, outside the <label>.
<Label htmlFor="password" action={<a href="/reset">Forgot password?</a>}>Password</Label>
<Input id="password" type="password" />
// disabled only dims the label: disable the control too.
<Label htmlFor="billing" disabled>Billing email</Label>
<Input id="billing" disabled />
// Inside a Field, use Field.Label: it takes hint and action, and wires itself.
<Field>
<Field.Label hint="Optional">Display name</Field.Label>
<Input />
</Field>Props
| Prop | Type | Default | Description |
|---|---|---|---|
required | boolean | — | Marks the control as required with a small mark after the text. The mark is visual only: the control itself says it is required. |
disabled | boolean | — | Dims the label to match a disabled control. |
hint | ReactNode | — | Muted text after the label's own, such as "Optional". It sits inside the label, so it is read as part of the control's name. |
action | ReactNode | — | Something to do about the control, such as a "Forgot password?" link, on the label's line at the inline end. It renders beside the label element, not inside it, so a link or button there stays its own control. |
…rest | LabelHTMLAttributes<HTMLLabelElement> | — | htmlFor, id, className, style, other attributes and the ref go on the <label>, also when an action puts it in a row with the action. |
Accessibility
- Label renders a native
<label>. On its own it names a control only throughhtmlFor, which must match the control'sid. A click on it then focuses the control, or presses a button control such as a switch. - The required mark is visual only (
aria-hidden). Putrequiredon the control as well, so the requirement is announced and the form checks it. - A
hintsits inside the<label>, so it is part of the control's name: "Branch Optional". Keep it to a word or two, and point the control'saria-describedbyat longer help. - An
actionrenders beside the<label>, not inside it, so a link or button there keeps its own name and its own Tab stop, and pressing it doesn't move focus to the control. disabledonly dims the label. It disables nothing: setdisabledon the control.