Skip to Content
Zuri
Components

Label

A native label that names a form control, with required and optional markers, three looks and three sizes.

Live preview
Theme
Preview width

Use Label when you place a control yourself. If you also want a hint, an error and automatic id wiring, use Field instead.

Installation

pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/label.json

Usage

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

<Label htmlFor="phone">Phone number</Label>
<Input id="phone" />

For a phone number

A phone number usually needs a country. The demo at the top pairs a Label with Phone input, which has a searchable country dropdown. Give the number field the label's id, as with any input.

<Label htmlFor="phone">Phone number</Label>
<PhoneInput id="phone" name="phone" />

For a plain local number, use a normal Input with type="tel" instead.

Looks

Live preview
Theme
Preview width
  • required adds a red asterisk. It is decorative, so also put required on the input itself; that is what assistive technology reads.
  • optional adds "(optional)" after the text. It is part of the label, so screen readers read it too. Marking the few optional fields is often clearer than starring every required one.
  • variant: default, caps (small spaced capitals in the same font, for dense forms) or muted.
  • size: sm, md or lg. Match it to the input's size.
<Label htmlFor="name" required>Full name</Label>
<Label htmlFor="nick" optional>Nickname</Label>
<Label htmlFor="phone" variant="caps" size="sm">Phone number</Label>

Props

Label extends all native <label> props and spreads them onto the element.

Props
PropTypeDefaultDescription
htmlForstring–The id of the control it names. Without it (or nesting), the label names nothing.
requiredboolean–Adds a decorative red asterisk. Put `required` on the control too.
optionalboolean–Adds "(optional)" after the text. Ignored when `required` is set.
variant"default" | "caps" | "muted""default"The look of the text.
size"sm" | "md" | "lg""md"The text size.
classNamestring–Merged with the built-in classes; yours win on conflicts.

Accessibility

  • It is a native <label>, so clicking it focuses the control and screen readers announce it as the control's name.
  • A label is required for every input. A placeholder is not a label.
  • The asterisk is hidden from assistive technology; the control's own required attribute carries the meaning. Don't rely on colour alone: the asterisk is a shape, and "(optional)" is text.

Tokens used

--foreground, --muted-foreground, --danger.

Known limitations

  • It does not check that it is associated with a control; that is up to you.

Source

import { cva, type VariantProps } from "class-variance-authority";import type { ComponentProps } from "react";import { cn } from "@/lib/utils";const labelVariants = cva("font-medium", {  variants: {    variant: {      default: "text-foreground",      // Small spaced capitals, for dense forms and settings screens. Same font as the rest.      caps: "uppercase tracking-wider text-foreground",      muted: "text-muted-foreground",    },    size: {      sm: "text-xs leading-none",      md: "text-sm leading-none",      lg: "text-base leading-tight",    },  },  defaultVariants: { variant: "default", size: "md" },});export interface LabelProps extends ComponentProps<"label">, VariantProps<typeof labelVariants> {  /**   * Adds a red asterisk. It is decorative: put `required` on the control itself so assistive   * technology hears it. Wins over `optional` if both are set.   */  required?: boolean;  /** Adds "(optional)" after the text. It is part of the label, so it is read out too. */  optional?: boolean;}/** A native `<label>`; associate it with a control via `htmlFor`, or wrap the control. */export function Label({  className,  variant,  size,  required,  optional,  children,  ...props}: LabelProps) {  return (    // biome-ignore lint/a11y/noLabelWithoutControl: association is the caller's job (htmlFor or nesting).    <label className={cn(labelVariants({ variant, size }), className)} {...props}>      {children}      {required ? (        <span aria-hidden="true" className="ms-0.5 text-danger">          *        </span>      ) : optional ? (        // A real space, not only margin, so the spoken name reads "Nickname (optional)".        <>          {" "}          <span className="font-normal normal-case tracking-normal text-muted-foreground">            (optional)          </span>        </>      ) : null}    </label>  );}export { labelVariants };

On this page