rapidUI

Label

Accessible form label built on Base UI Label.

Installation

pnpm dlx shadcn@latest add https://rdf.dev.360online.com/r/label.json

Installs to @/components/ui/label.

Manual installation

Copy the source into your project:

components/ui/label.tsx
'use client'

import type * as React from 'react'
import { cn } from '@/lib/utils'

function Label({ className, ...props }: React.ComponentProps<'label'>) {
	return (
		// biome-ignore lint/a11y/noLabelWithoutControl: the control is associated by the consumer via htmlFor or nesting
		<label
			data-slot="label"
			className={cn(
				'flex items-center gap-2 text-sm leading-none font-medium text-foreground select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:opacity-50 peer-disabled:cursor-not-allowed peer-disabled:opacity-50',
				className,
			)}
			{...props}
		/>
	)
}

export { Label }

A label component that renders a native label element, providing an accessible way to associate text labels with form controls.

Features

  • A plain native label element (no primitive dependency)
  • Accessible association with form controls via htmlFor
  • Laid out as flex items-center gap-2, so icons and badges line up inline
  • Dims automatically after a disabled peer control (peer-disabled)
  • Dims inside a group with data-disabled="true" (as used by Field)
  • Required field indication via className

Usage

import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
import { Checkbox } from '@/components/ui/checkbox'

// Basic usage
<Label>Label text</Label>

// With input
<div className="grid gap-1.5">
  <Label htmlFor="email">Email</Label>
  <Input id="email" type="email" />
</div>

// With checkbox
<div className="flex items-center space-x-2">
  <Checkbox id="terms" />
  <Label htmlFor="terms">Accept terms</Label>
</div>

// Required field
<Label htmlFor="email" className="after:text-destructive after:content-['*']">
  Email
</Label>

// Disabled state (group)
<div className="group grid gap-1.5" data-disabled="true">
  <Label htmlFor="name">Name</Label>
  <Input id="name" disabled />
</div>

// Disabled state (peer)
<div className="flex items-center gap-2">
  <Checkbox id="terms" className="peer" disabled />
  <Label htmlFor="terms">Accept terms</Label>
</div>

Examples

Default

A label associated with an input through htmlFor.

With Input

Labels on an address block.

With Checkbox

A label next to a checkbox, with and without a hint below it.

With Switch

A label and a hint for a switch in a settings row.

With Radio Group

One label names the group (via aria-labelledby), and each option has its own label.

Required Field

Mark required fields with a className, or mark the optional ones in text.

Disabled State

A label inside a group with data-disabled="true".

Peer Disabled

A label after a disabled peer control.

With Icon

Badges and an info tooltip lined up next to the label.

Props

Label accepts all native label attributes, including:

  • htmlFor: ID of the associated form control
  • className: Additional CSS classes

It has no custom variants.

Accessibility

The Label component follows WAI-ARIA guidelines and best practices:

  • Uses native HTML label element
  • Provides proper form control association
  • Maintains focus management
  • Supports screen readers
  • Communicates required and disabled states
  • Ensures proper color contrast

Best Practices

  1. Association:

    • Always associate labels with form controls
    • Use consistent IDs for form elements
    • Maintain clear relationships
  2. Content:

    • Use clear, concise text
    • Include units when necessary
    • Indicate required fields consistently
    • Consider internationalization
  3. Styling:

    • Maintain sufficient contrast
    • Use appropriate text size
    • Consider mobile viewports
    • Handle long text gracefully
  4. Accessibility:

    • Provide clear visual feedback
    • Support keyboard navigation
    • Use semantic markup
    • Consider screen reader users
  5. Implementation:

    • Group related form elements
    • Handle disabled states
    • Consider form validation
    • Maintain consistent spacing
  • Input
  • Checkbox
  • RadioGroup
  • Select
  • Switch
  • Form
  • Field