Theme

Customize appearance

Components

Label

Associate a clear text label with a form control.

View component source ↗

Preview

A minimal example to get started.

Installation

Install this component and its dependencies in a project initialized with shadcn.

pnpm dlx shadcn@latest add https://ui.hyunjoon.net/r/label.json

Usage

This is the basic example. Use the Code tab above for the selected example. Adjust import aliases to match your project.

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

export default function Example() {
  return (
    <div className="grid w-full max-w-sm gap-2">
      <Label htmlFor="example-name">Name</Label>
      <Input id="example-name" placeholder="Alex Jordan" />
    </div>
  );
}

API reference

Derived from the installed component types. Native DOM props are also supported where applicable. A dash means no explicit default is documented. Some props depend on the selected mode.

Label
Label properties
PropTypeDefaultDescription
ref
React.Ref<HTMLLabelElement> | undefined
React.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).
htmlFor
string | undefined
string | undefined
—ID of the associated form control.
defaultChecked
boolean | undefined
boolean | undefined
—Initial checked state for an uncontrolled control.
defaultValue
string | number | readonly string[] | undefined
string | number | readonly string[] | undefined
—Initial value for an uncontrolled component.
className
string | undefined
string | undefined
—Additional CSS classes merged with the component styles.
id
string | undefined
string | undefined
—Unique identifier used to associate labels and descriptions.
children
React.ReactNode
React.ReactNode
—Content rendered inside this component.
onFocus
React.FocusEventHandler<HTMLLabelElement> | undefined
React.FocusEventHandler<HTMLLabelElement> | undefined
—Called when the control receives focus.
onBlur
React.FocusEventHandler<HTMLLabelElement> | undefined
React.FocusEventHandler<HTMLLabelElement> | undefined
—Called when the control loses focus.
onChange
React.ChangeEventHandler<HTMLLabelElement, Element> | undefined
React.ChangeEventHandler<HTMLLabelElement, Element> | undefined
—Called when the underlying form control changes; inspect the event for its value.
onKeyDown
React.KeyboardEventHandler<HTMLLabelElement> | undefined
React.KeyboardEventHandler<HTMLLabelElement> | undefined
—Called when a key is pressed while this element is focused.
onClick
React.MouseEventHandler<HTMLLabelElement> | undefined
React.MouseEventHandler<HTMLLabelElement> | undefined
—Called when the element is activated.

Usage notes

Match htmlFor with the input id. Keep the visible label available instead of relying on placeholder text alone.