Label
A label in the hand, that dims with the control it names.
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";
export default function LabelDemo() {
return (
<Label>
<Checkbox seed="label-terms" />
Accept the terms
</Label>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ label"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
/**
* A plain <label>. `required` marks it with a red-pen asterisk (the control
* still needs its own `required`), `optional` says so in lighter ink.
*/
function Label({ className, children, required, optional, ...props }: ComponentProps<"label"> & { required?: boolean; optional?: boolean }) {
return (
<label
data-slot="label"
className={cn(
"flex items-center gap-2 text-base leading-tight select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50 peer-data-disabled:cursor-not-allowed peer-data-disabled:opacity-50",
className,
)}
{...props}
>
{children}
{required && (
<span aria-hidden="true" className="-ml-1 text-destructive">
*
</span>
)}
{optional && <span className="-ml-0.5 text-sm text-ink-3">(optional)</span>}
</label>
);
}
export { Label };Usage
import { Label } from "@/components/ui/label"
<Label htmlFor="email">Email</Label>Examples
With controls
Wrap a switch, radio or checkbox so the whole row is clickable; mark required fields; disabled controls dim their label.
import { Checkbox } from "@/components/ui/checkbox";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import { Switch } from "@/components/ui/switch";
export default function LabelControls() {
return (
<div className="grid w-full max-w-md gap-7">
<div className="grid gap-2">
<Label htmlFor="lc-name">
Name <span className="text-destructive">*</span>
</Label>
<Input id="lc-name" required placeholder="Required" seed="lc-name" />
</div>
<Label>
<Switch defaultChecked seed="lc-switch" />
Wrap the label around the control
</Label>
<RadioGroup defaultValue="a" aria-label="Choice" className="flex flex-wrap gap-6">
<Label>
<RadioGroupItem value="a" seed="lc-a" />
Option A
</Label>
<Label>
<RadioGroupItem value="b" seed="lc-b" />
Option B
</Label>
</RadioGroup>
<Label>
<Checkbox disabled seed="lc-disabled" />
Dims with its disabled control
</Label>
</div>
);
}Required and optional
A red-pen asterisk for required fields, a quiet note for optional ones.
"use client";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
export default function LabelRequired() {
return (
<form className="grid w-full max-w-sm gap-5" onSubmit={(event) => event.preventDefault()}>
<div className="grid gap-2">
<Label htmlFor="lr-name" required>
Name
</Label>
<Input id="lr-name" required placeholder="Ada Lovelace" seed="lr-name" />
</div>
<div className="grid gap-2">
<Label htmlFor="lr-email" required>
Email
</Label>
<Input id="lr-email" type="email" required placeholder="ada@example.com" seed="lr-email" />
</div>
<div className="grid gap-2">
<Label htmlFor="lr-note" optional>
Anything we should know
</Label>
<Input id="lr-note" placeholder="Left-handed, say" seed="lr-note" />
</div>
</form>
);
}API reference
- htmlFor
stringThe id of the control it names. Or wrap the control instead: the label lays it out beside the text.
- className
stringA plain <label>: every prop goes to it.
- requireddefault
false booleanMarks the label with a red-pen asterisk. The control still needs its own required.
- optionaldefault
false booleanWrites (optional) after the label, in lighter ink.