coss.com Svelte

Field

A component that provides labelling and validation for form controls.

API Reference
Loading p-field-1 preview…

Installation

pnpm dlx shadcn-svelte@latest add https://coss-sv.vercel.app/r/field.json

Usage

<script lang="ts">
  import * as Field from "$lib/components/ui/field/index.js";
  import { Input } from "$lib/components/ui/input/index.js";
</script>

<Field.Root>
  <Field.Label>Name</Field.Label>
  <Input type="text" placeholder="Enter your name" />
  <Field.Description>Visible on your profile</Field.Description>
  <Field.Error>Please enter a valid name</Field.Error>
  <Field.Validity>
    {#snippet children(validity)}
      {#if validity.error}<p>{validity.error}</p>{/if}
    {/snippet}
  </Field.Validity>
</Field.Root>

API Reference

Field.Root

Root component. Styled wrapper for Field.Root from Shards UI with flex column layout.

Field.Label

Label for the field. Styled wrapper for Field.Label from Shards UI.

Field.Item

Container for field items. Styled wrapper for Field.Item from Shards UI.

Field.Description

Description text for the field. Styled wrapper for Field.Description from Shards UI with muted foreground color.

Field.Error

Error message for the field. Styled wrapper for Field.Error from Shards UI with destructive foreground color.

Field.Control

Alias for Field.Control from Shards UI. Use it to register a custom or third-party control with the field context so it participates in labelling, validation, and error state.

Textarea does not require Field.Control. It wraps the control internally and integrates with Field in the same way as Input and the other built-in controls.

Field.Validity

Alias for Field.Validity from Shards UI.

Examples

Required Field

Loading p-field-2 preview…

Disabled Field

Loading p-field-3 preview…

With Error

Enter an invalid email address and press enter to see the error.

Loading p-field-4 preview…

With Validity

Loading p-field-5 preview…

Input Group

Loading p-field-6 preview…

Autocomplete Field

Loading p-field-7 preview…

Combobox Field

Loading p-field-8 preview…

Combobox Multiple Field

Loading p-field-9 preview…

Textarea Field

Loading p-field-10 preview…

Select Field

Loading p-field-11 preview…

Checkbox Field

Loading p-field-12 preview…

Checkbox Group Field

Loading p-field-13 preview…

Radio Group Field

Loading p-field-14 preview…

Switch Field

Loading p-field-15 preview…

Slider Field

Loading p-field-16 preview…

Number Field

Loading p-field-17 preview…

Complete Form Example

Loading p-field-18 preview…