Skip to content

Label

The Label component provides proper form field labeling with accessibility features, required indicators, and consistent typography for form elements.

Basic Usage

Required Fields

Optional Fields

With Description

Choose a unique username that will be visible to other users

Error State

Sizes

Font Weights

Intent Variants

Accessibility

The Label component follows WCAG 2.1 AA guidelines:

  • Proper Association: Uses for attribute to associate with form controls
  • Required Indicators: Clear visual and screen reader indication of required fields
  • Error Messages: Properly associated error messages with aria-describedby
  • Screen Reader Support: Error messages announced with role="alert" and aria-live="polite"
  • Semantic Markup: Uses native <label> element for maximum compatibility
  • Focus Management: Labels enhance focus behavior of associated inputs

Properties

PropertyTypeDefaultDescription
forstringundefinedID of the form control this label is associated with
requiredbooleanfalseShows a red asterisk (*) to indicate required field
optionalbooleanfalseShows “(optional)” text for optional fields
descriptionstringundefinedHelper text displayed below the label
errorstringundefinedError message displayed below the label
size"small" | "medium" | "large""medium"Size of the label text
weight"normal" | "medium" | "semibold" | "bold""medium"Font weight of the label
intent"default" | "muted" | "error" | "success""default"Visual style variant
classstringundefinedAdditional CSS classes

Features

Form Association

The Label component automatically associates with form controls using the for attribute, improving accessibility and user experience.

Required Field Indicators

When required={true} is set, a red asterisk (*) is displayed and announced to screen readers as “required”.

Optional Field Indicators

When optional={true} is set, “(optional)” text is displayed to clarify that the field is not required.

Error Integration

Error messages are properly associated with the label using aria-describedby and announced to screen readers with appropriate ARIA attributes.

Description Support

Helper text can be provided to give users additional context about what to enter in the associated form field.

Examples

Complete Form Field

---
import Label from '@uicg/cli/components/Label/label.astro'
import Input from '@uicg/cli/components/Input/input.astro'
---
<div class="space-y-2">
<Label
for="email-field"
required
description="We'll never share your email with anyone else"
>
Email Address
</Label>
<Input
id="email-field"
type="email"
placeholder="Enter your email"
required
/>
</div>

Form with Validation

---
import Label from '@uicg/cli/components/Label/label.astro'
import Input from '@uicg/cli/components/Input/input.astro'
---
<form>
<div class="space-y-4">
<div>
<Label
for="username"
required
error={errors.username}
description="Must be 3-20 characters long"
>
Username
</Label>
<Input
id="username"
name="username"
intent={errors.username ? 'danger' : 'default'}
required
/>
</div>
<div>
<Label for="bio" optional>Biography</Label>
<Textarea id="bio" name="bio" rows={4} />
</div>
</div>
</form>