Skip to content
All components
Forms

Label

Give a form control a visible name that is programmatically associated with it, so clicking the text also focuses or toggles the control.

An accessible label built on Radix Label.

Installation

tdpxl is not an npm dependency of your app. The @tdpxl/cli command (not published to npm yet) copies the source into your project, with the registry items and npm packages it imports.

npx @tdpxl/cli add label

Then import it from @/components/ui/label. New project? Start with the installation guide.

Examples

Each example is one branch of the official demo, rendered in its own frame. The code is that branch as you would write it, with imports pointing at the default install paths.

With input

Associate a label with the actual text input. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"

export function LabelWithInput() {
  const id = React.useId()

  return (
    <div className="flex w-full max-w-sm flex-col gap-6">
      <div className="flex flex-col gap-1.5">
        <Label htmlFor="label-demo-name">Project name</Label>
        <Input id="label-demo-name" placeholder="my-ui" />
      </div>
    </div>
  )
}

With checkbox

Associate a label with its checkbox control. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

export function LabelWithCheckbox() {
  const id = React.useId()

  return (
    <div className="flex w-full max-w-sm flex-col gap-6">
      <div className="flex items-center gap-2">
        <Checkbox id="label-demo-public" />
        <Label htmlFor="label-demo-public">Make it public</Label>
      </div>
    </div>
  )
}

Required marker

Mark a required field in the label text, not with color alone. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"

export function LabelRequiredMarker() {
  const id = React.useId()

  return (
    <div className="flex w-full max-w-sm flex-col gap-6">
      <div className="flex flex-col gap-1.5">
        <Label htmlFor={`${id}-full-name`}>
          Full name
          <span className="text-paragraph-sm text-sub-600">(required)</span>
        </Label>
        <Input id={`${id}-full-name`} autoComplete="name" required />
      </div>
    </div>
  )
}

With switch

Name a switch with a label placed before it in a setting row. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"

export function LabelWithSwitch() {
  const id = React.useId()

  return (
    <div className="flex w-full max-w-sm flex-col gap-6">
      <div className="flex items-center justify-between gap-4">
        <Label htmlFor={`${id}-digest`}>Weekly summary email</Label>
        <Switch id={`${id}-digest`} defaultChecked />
      </div>
    </div>
  )
}

Disabled control

Dim a label that follows a disabled checkbox through its peer state. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

export function LabelDisabledControl() {
  const id = React.useId()

  return (
    <div className="flex w-full max-w-sm flex-col gap-6">
      <div className="flex items-center gap-2">
        <Checkbox id={`${id}-archived`} disabled />
        <Label htmlFor={`${id}-archived`}>Include archived projects</Label>
      </div>
    </div>
  )
}

Wrapping text

Let a long checkbox label wrap beside its control without truncation. Preserves the complete example's required parts, labels and providers. The example's local state remains owned by that composition.

"use client"

import * as React from "react"

import { Checkbox } from "@/components/ui/checkbox"

import { Label } from "@/components/ui/label"

export function LabelWrappingText() {
  const id = React.useId()

  return (
    <div className="flex w-full max-w-sm flex-col gap-6">
      <div className="flex items-start gap-2">
        <Checkbox id={`${id}-consent`} className="mt-0.5" />
        <Label htmlFor={`${id}-consent`} className="block">
          Allow us to use anonymous usage data from this workspace to improve search results and suggestions
        </Label>
      </div>
    </div>
  )
}

API reference

Generated from the component's TypeScript signatures. Inherited DOM and primitive props are listed separately.

Label

Give a form control a visible associated name.

Label props
PropTypeDefaultDescription
inputMode"search" | "text" | "none" | "tel" | "url" | "email" | "numeric" | "decimal" | undefined—Hints at the type of data that might be entered by the user while editing the element or its contents
aria-describedbystring | undefined—Identifies the element (or elements) that describes the object.
aria-invalidboolean | "false" | "true" | "grammar" | "spelling" | undefined—Indicates the entered value does not conform to the format expected by the application.
aria-labelstring | undefined—Defines a string value that labels the current element.
aria-labelledbystring | undefined—Identifies the element (or elements) that labels the current element.
childrenReact.ReactNode—Represents all of the things React can render.
Inherited props (16)
Label inherited props
PropTypeDefaultDescription
formstring | undefined—ID of the form associated with this control, including when it is rendered outside that form element.
classNamestring | undefined—Additional classes for this part. Preserve contrast, visible focus and responsive content when adapting styles.
defaultCheckedboolean | undefined—Initial checked state for a control supporting uncontrolled selection. Inherited DOM typing does not give a layout wrapper checkbox behavior.
defaultValuestring | number | readonly string[] | undefined—Initial value for a control supporting uncontrolled state. Inherited DOM typing alone does not give a layout wrapper input behavior.
autoFocusboolean | undefined—Request automatic focus when the element mounts or the primitive opens, according to its source behavior. Choose initial focus deliberately in context.
idstring | undefined—Stable DOM identifier used by labels, descriptions or other explicit associations.
roleReact.AriaRole | undefined—Semantic role appropriate to the rendered element. Preserve required relationships and keyboard behavior.
onFocusReact.FocusEventHandler<HTMLLabelElement> | undefined—Receive the native focus event. Preserve the component's existing keyboard and focus behavior.
onBlurReact.FocusEventHandler<HTMLLabelElement> | undefined—Receive the native blur event when focus leaves this control.
onChangeReact.ChangeEventHandler<HTMLLabelElement, Element> | undefined—Receive the native change event; read the actual control value or checked state from its event target.
onKeyDownReact.KeyboardEventHandler<HTMLLabelElement> | undefined—Receive keyboard events. Preserve built-in keyboard behavior unless deliberately implementing an accessible alternative.
onClickReact.MouseEventHandler<HTMLLabelElement> | undefined—Receive activation events. The consumer owns the action performed and its result.
onSelectReact.ReactEventHandler<HTMLLabelElement> | undefined—Handle the selection event defined by this part's source signature. DOM text-selection events differ from menu or command activation callbacks.
asChildboolean | undefined—Merge this part into one child instead of its default element. Preserve that child's semantics, ref support and accessibility attributes.
htmlForstring | undefined—ID of the actual form control named by this label.
refReact.Ref<HTMLLabelElement> | undefined—Allows getting a ref to the component instance. Once the component unmounts, React will set ref.current to null (or call the ref with null if you passed a callback ref).

Accessibility & usage

From registry/ui/label.meta.json (authored guidance; human review may still be pending). Behavior is checked by Storybook tests, not certified here.

When to use

  • Naming any input, textarea, select trigger, checkbox, switch or radio item through htmlFor and the control id.
  • Short labels placed next to checkboxes, switches and radio items, where the label also enlarges the click target.

When not to use

  • Group names for a set of controls; use a fieldset legend or a visible heading referenced by aria-labelledby.
  • Help text, hints or errors; use FieldDescription or FieldError.
  • Inside Form; FormLabel already wires htmlFor to the generated control id.

Anatomy

Label
The Radix label element. Clicking it focuses or toggles the control named by htmlFor; double-click text selection is prevented. It dims when a preceding peer control is disabled or a parent group has data-disabled="true".

Keyboard & interaction

  • Clicking or tapping the label activates the associated control: focuses inputs, toggles checkboxes and switches, selects radio items.
  • The label itself is not focusable and has no keyboard behavior.
  • The label text becomes the control's accessible name; keep visible text and name identical so speech input users can say what they see (WCAG 2.5.3).

Responsive behavior

  • Above the control for text inputs on every screen size; beside the control for checkboxes, switches and radios.
  • Long labels wrap; align the control to the first line (items-start) so wrapped text stays next to it.
  • A label beside a small control enlarges the effective target; keep at least 24 CSS px of spacing between neighboring targets.

Content guidelines

  • Short noun phrases in sentence case ('Billing email'), no trailing colon.
  • For checkboxes and switches, describe the 'on' state ('Send me product updates', 'Autosave drafts').
  • Put '(required)' or '(optional)' in the label text when it applies, so it is part of the accessible name.
  • Translate labels with the rest of the interface; do not abbreviate to save space.

Alternatives

  • Field — FieldLabel plus hint and error layout for a complete field.
  • Form — FormLabel, which associates itself with the React Hook Form control.

Resources