Label
Renders an accessible label associated with controls.
tsx
import { Checkbox } from "@/components/ui/checkbox"import { Label } from "@/components/ui/label"
export function LabelDemo() { return ( <div className="flex items-center space-x-2"> <Checkbox id="terms" /> <Label htmlFor="terms">Accept terms and conditions</Label> </div> )}
Installation
CLI
bash
npx fivui add label
Manual
Copy and paste the following code into your project.
bash
npm install @radix-ui/react-label
components/ui/label.tsx
"use client"
import * as React from "react"import * as LabelPrimitive from "@radix-ui/react-label"import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
const labelVariants = cva( "text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70")
const Label = React.forwardRef< React.ElementRef<typeof LabelPrimitive.Root>, React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root> & VariantProps<typeof labelVariants>>(({ className, ...props }, ref) => ( <LabelPrimitive.Root ref={ref} className={cn(labelVariants(), className)} {...props} />))Label.displayName = LabelPrimitive.Root.displayName
export { Label }
Usage
tsx
import { Label } from "@/components/ui/label"
tsx
<Label htmlFor="email">Your email address</Label>
Examples
Default
tsx
import { Checkbox } from "@/components/ui/checkbox"import { Label } from "@/components/ui/label"
export function LabelDemo() { return ( <div className="flex items-center space-x-2"> <Checkbox id="terms" /> <Label htmlFor="terms">Accept terms and conditions</Label> </div> )}
With Input
tsx
import { Input } from "@/components/ui/input"import { Label } from "@/components/ui/label"
export function LabelWithInput() { return ( <div className="grid w-full max-w-sm items-center gap-1.5"> <Label htmlFor="email">Email</Label> <Input type="email" id="email" placeholder="Email" /> </div> )}
With File Input
tsx
import { Input } from "@/components/ui/input"import { Label } from "@/components/ui/label"
export function LabelWithFile() { return ( <div className="grid w-full max-w-sm items-center gap-1.5"> <Label htmlFor="picture">Picture</Label> <Input id="picture" type="file" /> </div> )}
API Reference
Label
Prop | Type | Default | Description |
---|---|---|---|
htmlFor | string | - | The id of the element the label is associated with |
Label inherits all props from Radix UI Label
primitive.