Skip to content
2k
On This Page

Label

Renders an accessible label associated with controls.

import { Checkbox } from "~/components/ui/checkbox"
import { Label } from "~/components/ui/label"

export default function LabelDemo() {
  return (
    <div class="flex items-center gap-2">
      <Checkbox id="terms" />
      <Label for="terms">Accept terms and conditions</Label>
    </div>
  )
}

For form fields, use the Field component which includes built-in label, description, and error handling.

Installation

pnpm dlx solidui-cli@latest add label

Copy and paste the following code into your project.

components/ui/label.tsx
import { type ComponentProps } from "@solidjs/web"

import { cn } from "~/lib/utils"

type LabelProps = ComponentProps<"label">

const Label = (props: LabelProps) => (
  <label
    data-slot="label"
    {...props}
    class={cn(
      "gap-2 text-sm leading-none font-medium group-data-[disabled=true]:opacity-50 peer-disabled:opacity-50 flex items-center select-none group-data-[disabled=true]:pointer-events-none peer-disabled:cursor-not-allowed",
      props.class
    )}
  />
)

export { Label, type LabelProps }

Update the import paths to match your project setup.

Usage

import { Label } from "~/components/ui/label"
<Label for="email">Email address</Label>

Label in Field

For form fields, use the Field component which includes built-in FieldLabel, FieldDescription, and FieldError components.

<Field>
  <FieldLabel for="email">Your email address</FieldLabel>
  <Input id="email" />
</Field>
Payment Method

All transactions are secure and encrypted

Enter your 16-digit card number

Billing Address

The billing address associated with your payment method

import { For } from "solid-js"

import { Button } from "~/components/ui/button"
import { Checkbox } from "~/components/ui/checkbox"
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel,
  FieldLegend,
  FieldSeparator,
  FieldSet
} from "~/components/ui/field"
import { Input } from "~/components/ui/input"
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue
} from "~/components/ui/select"
import { Textarea } from "~/components/ui/textarea"

const months = [
  "01",
  "02",
  "03",
  "04",
  "05",
  "06",
  "07",
  "08",
  "09",
  "10",
  "11",
  "12"
]
const years = ["2024", "2025", "2026", "2027", "2028", "2029"]

export default function FieldDemo() {
  return (
    <div class="w-full max-w-md">
      <form>
        <FieldGroup>
          <FieldSet>
            <FieldLegend>Payment Method</FieldLegend>
            <FieldDescription>
              All transactions are secure and encrypted
            </FieldDescription>
            <FieldGroup>
              <Field>
                <FieldLabel for="checkout-card-name">Name on Card</FieldLabel>
                <Input
                  id="checkout-card-name"
                  placeholder="Evil Rabbit"
                  required
                />
              </Field>
              <Field>
                <FieldLabel for="checkout-card-number">Card Number</FieldLabel>
                <Input
                  id="checkout-card-number"
                  placeholder="1234 5678 9012 3456"
                  required
                />
                <FieldDescription>
                  Enter your 16-digit card number
                </FieldDescription>
              </Field>
              <div class="grid grid-cols-3 gap-4">
                <Field>
                  <FieldLabel for="checkout-exp-month">Month</FieldLabel>
                  <Select<string> placeholder="MM">
                    <SelectTrigger
                      aria-label="MM"
                      class="w-full"
                      id="checkout-exp-month"
                    >
                      <SelectValue />
                    </SelectTrigger>
                    <SelectContent>
                      <For each={months}>
                        {(option) => (
                          <SelectItem value={option}>{option}</SelectItem>
                        )}
                      </For>
                    </SelectContent>
                  </Select>
                </Field>
                <Field>
                  <FieldLabel for="checkout-exp-year">Year</FieldLabel>
                  <Select<string> placeholder="YYYY">
                    <SelectTrigger
                      aria-label="YYYY"
                      class="w-full"
                      id="checkout-exp-year"
                    >
                      <SelectValue />
                    </SelectTrigger>
                    <SelectContent>
                      <For each={years}>
                        {(option) => (
                          <SelectItem value={option}>{option}</SelectItem>
                        )}
                      </For>
                    </SelectContent>
                  </Select>
                </Field>
                <Field>
                  <FieldLabel for="checkout-cvv">CVV</FieldLabel>
                  <Input id="checkout-cvv" placeholder="123" required />
                </Field>
              </div>
            </FieldGroup>
          </FieldSet>
          <FieldSeparator />
          <FieldSet>
            <FieldLegend>Billing Address</FieldLegend>
            <FieldDescription>
              The billing address associated with your payment method
            </FieldDescription>
            <FieldGroup>
              <Field orientation="horizontal">
                <Checkbox id="checkout-same-as-shipping" defaultChecked />
                <FieldLabel class="font-normal" for="checkout-same-as-shipping">
                  Same as shipping address
                </FieldLabel>
              </Field>
            </FieldGroup>
          </FieldSet>

          <FieldSet>
            <FieldGroup>
              <Field>
                <FieldLabel for="checkout-comments">Comments</FieldLabel>
                <Textarea
                  class="resize-none"
                  id="checkout-comments"
                  placeholder="Add any additional comments"
                />
              </Field>
            </FieldGroup>
          </FieldSet>

          <Field orientation="horizontal">
            <Button type="submit">Submit</Button>
            <Button type="button" variant="outline">
              Cancel
            </Button>
          </Field>
        </FieldGroup>
      </form>
    </div>
  )
}

API Reference

See the HTML label element documentation for more information.