Label

Slant / Forms

A control's name, with a required mark, a hint and an action on the same line.

Demo

Usage

@tuttn/slant
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

PropTypeDefaultDescription
requiredboolean—Marks the control as required with a small mark after the text. The mark is visual only: the control itself says it is required.
disabledboolean—Dims the label to match a disabled control.
hintReactNode—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.
actionReactNode—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.
…restLabelHTMLAttributes<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 through htmlFor, which must match the control's id. 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). Put required on the control as well, so the requirement is announced and the form checks it.
  • A hint sits 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's aria-describedby at longer help.
  • An action renders 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.
  • disabled only dims the label. It disables nothing: set disabled on the control.