8.0k New

Field

Combine labels, controls, and help text to compose accessible form fields and grouped inputs.

Epicenter

Local-first, open source apps

Special Sponsor
Payment Method

All transactions are secure and encrypted

Enter your 16-digit card number

Billing Address

The billing address associated with your payment method

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import * as Select from "$lib/components/ui/select/index.js";
  import { Button } from "$lib/components/ui/button/index.js";
  import { Checkbox } from "$lib/components/ui/checkbox/index.js";
  import { Input } from "$lib/components/ui/input/index.js";
  import { Textarea } from "$lib/components/ui/textarea/index.js";
 
  const months = [
    { label: "01", value: "01" },
    { label: "02", value: "02" },
    { label: "03", value: "03" },
    { label: "04", value: "04" },
    { label: "05", value: "05" },
    { label: "06", value: "06" },
    { label: "07", value: "07" },
    { label: "08", value: "08" },
    { label: "09", value: "09" },
    { label: "10", value: "10" },
    { label: "11", value: "11" },
    { label: "12", value: "12" }
  ];
 
  const years = [
    { label: "2024", value: "2024" },
    { label: "2025", value: "2025" },
    { label: "2026", value: "2026" },
    { label: "2027", value: "2027" },
    { label: "2028", value: "2028" },
    { label: "2029", value: "2029" }
  ];
 
  let month = $state("");
  let year = $state("");
</script>
 
<div class="w-full max-w-md">
  <form>
    <Field.Group>
      <Field.Set>
        <Field.Legend>Payment Method</Field.Legend>
        <Field.Description
          >All transactions are secure and encrypted</Field.Description
        >
        <Field.Group>
          <Field.Field>
            <Field.Label for="checkout-7j9-card-name-43j"
              >Name on Card</Field.Label
            >
            <Input
              id="checkout-7j9-card-name-43j"
              placeholder="Evil Rabbit"
              required
            />
          </Field.Field>
          <Field.Field>
            <Field.Label for="checkout-7j9-card-number-uw1"
              >Card Number</Field.Label
            >
            <Input
              id="checkout-7j9-card-number-uw1"
              placeholder="1234 5678 9012 3456"
              required
            />
            <Field.Description
              >Enter your 16-digit card number</Field.Description
            >
          </Field.Field>
          <div class="grid grid-cols-3 gap-4">
            <Field.Field>
              <Field.Label for="checkout-exp-month-ts6">Month</Field.Label>
              <Select.Root type="single" items={months} bind:value={month}>
                <Select.Trigger id="checkout-exp-month-ts6">
                  <Select.Value placeholder="MM" />
                </Select.Trigger>
                <Select.Content>
                  <Select.Group>
                    {#each months as item (item.value)}
                      <Select.Item value={item.value} label={item.label}>
                        {item.label}
                      </Select.Item>
                    {/each}
                  </Select.Group>
                </Select.Content>
              </Select.Root>
            </Field.Field>
            <Field.Field>
              <Field.Label for="checkout-7j9-exp-year-f59">Year</Field.Label>
              <Select.Root type="single" items={years} bind:value={year}>
                <Select.Trigger id="checkout-7j9-exp-year-f59">
                  <Select.Value placeholder="YYYY" />
                </Select.Trigger>
                <Select.Content>
                  <Select.Group>
                    {#each years as item (item.value)}
                      <Select.Item value={item.value} label={item.label}>
                        {item.label}
                      </Select.Item>
                    {/each}
                  </Select.Group>
                </Select.Content>
              </Select.Root>
            </Field.Field>
            <Field.Field>
              <Field.Label for="checkout-7j9-cvv">CVV</Field.Label>
              <Input id="checkout-7j9-cvv" placeholder="123" required />
            </Field.Field>
          </div>
        </Field.Group>
      </Field.Set>
      <Field.Separator />
      <Field.Set>
        <Field.Legend>Billing Address</Field.Legend>
        <Field.Description>
          The billing address associated with your payment method
        </Field.Description>
        <Field.Group>
          <Field.Field orientation="horizontal">
            <Checkbox id="checkout-7j9-same-as-shipping-wgm" checked={true} />
            <Field.Label
              for="checkout-7j9-same-as-shipping-wgm"
              class="font-normal"
            >
              Same as shipping address
            </Field.Label>
          </Field.Field>
        </Field.Group>
      </Field.Set>
      <Field.Set>
        <Field.Group>
          <Field.Field>
            <Field.Label for="checkout-7j9-optional-comments"
              >Comments</Field.Label
            >
            <Textarea
              id="checkout-7j9-optional-comments"
              placeholder="Add any additional comments"
              class="resize-none"
            />
          </Field.Field>
        </Field.Group>
      </Field.Set>
      <Field.Field orientation="horizontal">
        <Button type="submit">Submit</Button>
        <Button variant="outline" type="button">Cancel</Button>
      </Field.Field>
    </Field.Group>
  </form>
</div>

Installation

pnpm dlx shadcn-svelte@latest add field

Usage

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
</script>
<Field.Set>
  <Field.Legend>Profile</Field.Legend>
  <Field.Description>This appears on invoices and emails.</Field.Description>
  <Field.Group>
    <Field.Field>
      <Field.Label for="name">Full name</Field.Label>
      <Input id="name" autocomplete="off" placeholder="Evil Rabbit" />
      <Field.Description
        >This appears on invoices and emails.</Field.Description
      >
    </Field.Field>
    <Field.Field>
      <Field.Label for="username">Username</Field.Label>
      <Input id="username" autocomplete="off" aria-invalid />
      <Field.Error>Choose another username.</Field.Error>
    </Field.Field>
    <Field.Field orientation="horizontal">
      <Switch id="newsletter" />
      <Field.Label for="newsletter">Subscribe to the newsletter</Field.Label>
    </Field.Field>
  </Field.Group>
</Field.Set>

Anatomy

The Field family is designed for composing accessible forms. A typical field is structured as follows:

<Field.Field>
  <Field.Label for="input-id">Label</Field.Label>
  <!-- Input, Select, Switch, etc. -->
  <Field.Description>Optional helper text.</Field.Description>
  <Field.Error>Validation message.</Field.Error>
</Field.Field>
  • Field.Field is the core wrapper for a single field.
  • Field.Content is a flex column that groups label and description. Not required if you have no description.
  • Wrap related fields with Field.Group, and use Field.Set with Field.Legend for semantic grouping.

Input

Choose a unique username for your account.

Must be at least 8 characters long.

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Input } from "$lib/components/ui/input/index.js";
</script>
 
<Field.Set class="w-full max-w-xs">
  <Field.Group>
    <Field.Field>
      <Field.Label for="username">Username</Field.Label>
      <Input id="username" type="text" placeholder="Max Leiter" />
      <Field.Description
        >Choose a unique username for your account.</Field.Description
      >
    </Field.Field>
    <Field.Field>
      <Field.Label for="password">Password</Field.Label>
      <Field.Description>Must be at least 8 characters long.</Field.Description>
      <Input id="password" type="password" placeholder="••••••••" />
    </Field.Field>
  </Field.Group>
</Field.Set>

Textarea

Share your thoughts about our service.

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Textarea } from "$lib/components/ui/textarea/index.js";
</script>
 
<Field.Set class="w-full max-w-xs">
  <Field.Group>
    <Field.Field>
      <Field.Label for="feedback">Feedback</Field.Label>
      <Textarea
        id="feedback"
        placeholder="Your feedback helps us improve..."
        rows={4}
      />
      <Field.Description
        >Share your thoughts about our service.</Field.Description
      >
    </Field.Field>
  </Field.Group>
</Field.Set>

Select

Select your department or area of work.

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import * as Select from "$lib/components/ui/select/index.js";
 
  const items = [
    { label: "Engineering", value: "engineering" },
    { label: "Design", value: "design" },
    { label: "Marketing", value: "marketing" },
    { label: "Sales", value: "sales" },
    { label: "Customer Support", value: "support" },
    { label: "Human Resources", value: "hr" },
    { label: "Finance", value: "finance" },
    { label: "Operations", value: "operations" }
  ];
 
  let value = $state("");
</script>
 
<Field.Field class="w-full max-w-xs">
  <Field.Label for="department">Department</Field.Label>
  <Select.Root type="single" {items} bind:value>
    <Select.Trigger id="department">
      <Select.Value placeholder="Choose department" />
    </Select.Trigger>
    <Select.Content>
      <Select.Group>
        {#each items as item (item.value)}
          <Select.Item value={item.value} label={item.label}>
            {item.label}
          </Select.Item>
        {/each}
      </Select.Group>
    </Select.Content>
  </Select.Root>
  <Field.Description>Select your department or area of work.</Field.Description>
</Field.Field>

Slider

Price Range

Set your budget range ($200 - 800).

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Slider } from "$lib/components/ui/slider/index.js";
 
  let value = $state([200, 800]);
</script>
 
<Field.Field class="w-full max-w-xs">
  <Field.Title>Price Range</Field.Title>
  <Field.Description>
    Set your budget range ($<span class="font-medium tabular-nums"
      >{value[0]}</span
    >
    -
    <span class="font-medium tabular-nums">{value[1]}</span>).
  </Field.Description>
  <Slider
    type="multiple"
    bind:value
    max={1000}
    min={0}
    step={10}
    class="mt-2 w-full"
    aria-label="Price Range"
  />
</Field.Field>

Fieldset

Address Information

We need your address to deliver your order.

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Input } from "$lib/components/ui/input/index.js";
</script>
 
<Field.Set class="w-full max-w-sm">
  <Field.Legend>Address Information</Field.Legend>
  <Field.Description
    >We need your address to deliver your order.</Field.Description
  >
  <Field.Group>
    <Field.Field>
      <Field.Label for="street">Street Address</Field.Label>
      <Input id="street" type="text" placeholder="123 Main St" />
    </Field.Field>
    <div class="grid grid-cols-2 gap-4">
      <Field.Field>
        <Field.Label for="city">City</Field.Label>
        <Input id="city" type="text" placeholder="New York" />
      </Field.Field>
      <Field.Field>
        <Field.Label for="zip">Postal Code</Field.Label>
        <Input id="zip" type="text" placeholder="90502" />
      </Field.Field>
    </div>
  </Field.Group>
</Field.Set>

Checkbox

Show these items on the desktop

Select the items you want to show on the desktop.

Your Desktop & Documents folders are being synced with iCloud Drive. You can access them from other devices.

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Checkbox } from "$lib/components/ui/checkbox/index.js";
</script>
 
<Field.Group class="w-full max-w-xs">
  <Field.Set>
    <Field.Legend variant="label">Show these items on the desktop</Field.Legend>
    <Field.Description
      >Select the items you want to show on the desktop.</Field.Description
    >
    <Field.Group class="gap-3">
      <Field.Field orientation="horizontal">
        <Checkbox id="finder-pref-9k2-hard-disks-ljj" />
        <Field.Label for="finder-pref-9k2-hard-disks-ljj" class="font-normal">
          Hard disks
        </Field.Label>
      </Field.Field>
      <Field.Field orientation="horizontal">
        <Checkbox id="finder-pref-9k2-external-disks-1yg" />
        <Field.Label
          for="finder-pref-9k2-external-disks-1yg"
          class="font-normal"
        >
          External disks
        </Field.Label>
      </Field.Field>
      <Field.Field orientation="horizontal">
        <Checkbox id="finder-pref-9k2-cds-dvds-fzt" />
        <Field.Label for="finder-pref-9k2-cds-dvds-fzt" class="font-normal">
          CDs, DVDs, and iPods
        </Field.Label>
      </Field.Field>
      <Field.Field orientation="horizontal">
        <Checkbox id="finder-pref-9k2-connected-servers-6l2" />
        <Field.Label
          for="finder-pref-9k2-connected-servers-6l2"
          class="font-normal"
        >
          Connected servers
        </Field.Label>
      </Field.Field>
    </Field.Group>
  </Field.Set>
  <Field.Separator />
  <Field.Field orientation="horizontal">
    <Checkbox id="finder-pref-9k2-sync-folders-nep" checked />
    <Field.Content>
      <Field.Label for="finder-pref-9k2-sync-folders-nep">
        Sync Desktop & Documents folders
      </Field.Label>
      <Field.Description>
        Your Desktop & Documents folders are being synced with iCloud Drive. You
        can access them from other devices.
      </Field.Description>
    </Field.Content>
  </Field.Field>
</Field.Group>

Radio

Subscription Plan

Yearly and lifetime plans offer significant savings.

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import * as RadioGroup from "$lib/components/ui/radio-group/index.js";
 
  let plan = $state("monthly");
</script>
 
<Field.Set class="w-full max-w-xs">
  <Field.Legend variant="label">Subscription Plan</Field.Legend>
  <Field.Description
    >Yearly and lifetime plans offer significant savings.</Field.Description
  >
  <RadioGroup.Root bind:value={plan}>
    <Field.Field orientation="horizontal">
      <RadioGroup.Item value="monthly" id="plan-monthly" />
      <Field.Label for="plan-monthly" class="font-normal"
        >Monthly ($9.99/month)</Field.Label
      >
    </Field.Field>
    <Field.Field orientation="horizontal">
      <RadioGroup.Item value="yearly" id="plan-yearly" />
      <Field.Label for="plan-yearly" class="font-normal"
        >Yearly ($99.99/year)</Field.Label
      >
    </Field.Field>
    <Field.Field orientation="horizontal">
      <RadioGroup.Item value="lifetime" id="plan-lifetime" />
      <Field.Label for="plan-lifetime" class="font-normal"
        >Lifetime ($299.99)</Field.Label
      >
    </Field.Field>
  </RadioGroup.Root>
</Field.Set>

Switch

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Switch } from "$lib/components/ui/switch/index.js";
</script>
 
<Field.Field orientation="horizontal" class="w-fit">
  <Field.Label for="2fa">Multi-factor authentication</Field.Label>
  <Switch id="2fa" />
</Field.Field>

Choice Card

Wrap Field.Field components inside Field.Label to create selectable field groups. This works with RadioGroup.Item, Checkbox and Switch components.

Compute Environment

Select the compute environment for your cluster.

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import * as RadioGroup from "$lib/components/ui/radio-group/index.js";
 
  let computeEnvironment = $state("kubernetes");
</script>
 
<Field.Group class="w-full max-w-xs">
  <Field.Set>
    <Field.Legend variant="label">Compute Environment</Field.Legend>
    <Field.Description
      >Select the compute environment for your cluster.</Field.Description
    >
    <RadioGroup.Root bind:value={computeEnvironment}>
      <Field.Label for="kubernetes-r2h">
        <Field.Field orientation="horizontal">
          <Field.Content>
            <Field.Title>Kubernetes</Field.Title>
            <Field.Description
              >Run GPU workloads on a K8s cluster.</Field.Description
            >
          </Field.Content>
          <RadioGroup.Item value="kubernetes" id="kubernetes-r2h" />
        </Field.Field>
      </Field.Label>
      <Field.Label for="vm-z4k">
        <Field.Field orientation="horizontal">
          <Field.Content>
            <Field.Title>Virtual Machine</Field.Title>
            <Field.Description
              >Access a cluster to run GPU workloads.</Field.Description
            >
          </Field.Content>
          <RadioGroup.Item value="vm" id="vm-z4k" />
        </Field.Field>
      </Field.Label>
    </RadioGroup.Root>
  </Field.Set>
</Field.Group>

Field Group

Stack Field components with Field.Group. Add Field.Separator to divide them.

Get notified when ChatGPT responds to requests that take time, like research or image generation.

Get notified when tasks you've created have updates. Manage tasks

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Checkbox } from "$lib/components/ui/checkbox/index.js";
</script>
 
<Field.Group class="w-full max-w-xs">
  <Field.Set>
    <Field.Label>Responses</Field.Label>
    <Field.Description>
      Get notified when ChatGPT responds to requests that take time, like
      research or image generation.
    </Field.Description>
    <Field.Group data-slot="checkbox-group">
      <Field.Field orientation="horizontal">
        <Checkbox id="push" checked disabled />
        <Field.Label for="push" class="font-normal"
          >Push notifications</Field.Label
        >
      </Field.Field>
    </Field.Group>
  </Field.Set>
  <Field.Separator />
  <Field.Set>
    <Field.Label>Tasks</Field.Label>
    <Field.Description>
      Get notified when tasks you've created have updates.
      <a href="##">Manage tasks</a>
    </Field.Description>
    <Field.Group data-slot="checkbox-group">
      <Field.Field orientation="horizontal">
        <Checkbox id="push-tasks" />
        <Field.Label for="push-tasks" class="font-normal"
          >Push notifications</Field.Label
        >
      </Field.Field>
      <Field.Field orientation="horizontal">
        <Checkbox id="email-tasks" />
        <Field.Label for="email-tasks" class="font-normal"
          >Email notifications</Field.Label
        >
      </Field.Field>
    </Field.Group>
  </Field.Set>
</Field.Group>

Responsive Layout

  • Vertical fields: Default orientation stacks label, control, and helper text—ideal for mobile-first layouts.
  • Horizontal fields: Set orientation="horizontal" on Field to align the label and control side-by-side. Pair with Field.Content to keep descriptions aligned.
  • Responsive fields: Set orientation="responsive" for automatic column layouts inside container-aware parents. Apply @container/field-group classes on Field.Group to switch orientations at specific breakpoints.
Profile

Fill in your profile information.

Provide your full name for identification

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Button } from "$lib/components/ui/button/index.js";
  import { Input } from "$lib/components/ui/input/index.js";
</script>
 
<div class="w-full max-w-lg">
  <form>
    <Field.Set>
      <Field.Legend>Profile</Field.Legend>
      <Field.Description>Fill in your profile information.</Field.Description>
      <Field.Group>
        <Field.Field orientation="responsive">
          <Field.Content>
            <Field.Label for="name">Name</Field.Label>
            <Field.Description
              >Provide your full name for identification</Field.Description
            >
          </Field.Content>
          <Input id="name" placeholder="Evil Rabbit" required />
        </Field.Field>
        <Field.Field orientation="responsive">
          <Button type="submit">Submit</Button>
          <Button type="button" variant="outline">Cancel</Button>
        </Field.Field>
      </Field.Group>
    </Field.Set>
  </form>
</div>

Validation and Errors

  • Add data-invalid to Field.Field to switch the entire block into an error state.
  • Add aria-invalid on the input itself for assistive technologies.
  • Render Field.Error immediately after the control or inside Field.Content to keep error messages aligned with the field.
<Field.Field data-invalid>
  <Field.Label for="email">Email</Field.Label>
  <Input id="email" type="email" aria-invalid />
  <Field.Error>Enter a valid email address.</Field.Error>
</Field.Field>

Accessibility

  • Field.Set and Field.Legend keep related controls grouped for keyboard and assistive tech users.
  • Field outputs role="group" so nested controls inherit labeling from Field.Label and Field.Legend when combined.
  • Apply Field.Separator sparingly to ensure screen readers encounter clear section boundaries.