---
title: "Field"
description: "Field with accessible presentation and local state."
---

# Field

Field with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Field, FieldSet, FieldLegend, FieldGroup, FieldFrame, FieldContent, FieldLabel, FieldTitle, FieldDescription, FieldError, FieldSeparator from "@rightui/ui"
```

## Usage

Import Field, FieldSet from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use FieldFocus from "@rightui/ui"
use FontWeights from "@rightui/ui"
use uiFontFamily, Slider from "@rightui/ui"
use RadioGroup from "@rightui/ui"
use Switch from "@rightui/ui"
use Checkbox from "@rightui/ui"
use FieldSet, FieldLegend, FieldGroup, FieldFrame, FieldContent, FieldLabel, FieldDescription, FieldError, FieldSeparator from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state name = ""
  state email = ""
  state disabled = false
  state submitted = false
  derive errors = submitted and (not email.includes("@") or email.trim() == "") ? ["Enter a valid email address.", "Enter a valid email address.", "The address must contain @."] : []
  action editName(v: string)
    name = v
    saved = false
  action editEmail(v: string)
    email = v
    saved = false
  action validate
    submitted = true
    saved = name.trim() != "" and email.includes("@")
  state saved = false
  state enabled = true
  state frequency = "Daily"
  state volume = 50
  action setEnabled(v: bool)
    enabled = v
  action setFrequency(v: string)
    frequency = v
  action setVolume(v: number)
    volume = v
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      FieldSet(id="composition-fields", legendId="composition-legend", descriptionId="composition-help", disabled=disabled)
        FieldLegend(id="composition-legend", label="Profile information", variant="legend")
        FieldDescription(id="composition-help", description="This form validates locally without sending information.")
        FieldGroup(id="composition-group", nested=false)
          FieldFrame(id="composition-name-field", labelId="composition-name-label", descriptionId="composition-name-help", invalid=(submitted and name.trim() == ""), orientation="vertical")
            FieldLabel(id="composition-name-label", label="Name", controlId="composition-name", disabled=disabled)
            input class=FieldFocus id="composition-name" testId="composition-name" padding="ui.space-10" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-sm" background-color="ui.background" color="ui.foreground" value=name input=editName submit=validate disabled=disabled aria-labelledby="composition-name-label" aria-describedby="composition-name-help" aria-required=true aria-invalid=(submitted and name.trim() == "") font-family=uiFontFamily(uiFont) data-rightui-font="body"
            FieldDescription(id="composition-name-help", description="The name displayed on your profile.")
            FieldError(id="composition-name-error", messages=(submitted and name.trim() == "" ? ["Enter your name."] : []))
          FieldSeparator(id="composition-separator", label="Contact")
          FieldFrame(id="composition-email-field", labelId="composition-email-label", descriptionId="composition-email-help", invalid=(errors.length > 0), orientation="vertical")
            FieldContent(id="composition-email-content")
              FieldLabel(id="composition-email-label", label="E-mail", controlId="composition-email", disabled=disabled)
              FieldDescription(id="composition-email-help", description="Use an address containing @.")
            input class=FieldFocus id="composition-email" testId="composition-email" padding="ui.space-10" border-width="ui.border-width" border-style="solid" border-color="ui.border" border-radius="ui.radius-sm" background-color="ui.background" color="ui.foreground" type="email" value=email input=editEmail submit=validate disabled=disabled aria-labelledby="composition-email-label" aria-describedby="composition-email-help composition-email-error" aria-required=true aria-invalid=(errors.length > 0) font-family=uiFontFamily(uiFont) data-rightui-font="body"
            FieldError(id="composition-email-error", messages=errors)
      text "Responsive checkbox" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      FieldFrame(id="field-checkbox", labelId="field-checkbox-label", descriptionId="", invalid=false, orientation=(narrow ? "vertical" : "horizontal"))
        FieldLabel(id="field-checkbox-label", label="Checkbox preference", controlId="field-check-control", disabled=false)
        Checkbox(id="field-check-control", label="Notifications", checked=enabled, disabled=false, change=setEnabled)
      text "Responsive switch" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      FieldFrame(id="field-switch", labelId="field-switch-label", descriptionId="", invalid=false, orientation=(narrow ? "vertical" : "horizontal"))
        FieldLabel(id="field-switch-label", label="Switch preference", controlId="field-switch-control", disabled=false)
        Switch(id="field-switch-control", label="Email updates", checked=enabled, disabled=false, reduced=reduced, change=setEnabled)
      text "Responsive radio" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      FieldFrame(id="field-radio", labelId="field-radio-label", descriptionId="", invalid=false, orientation=(narrow ? "vertical" : "horizontal"))
        FieldLabel(id="field-radio-label", label="Radio preference", controlId="field-radio-control", disabled=false)
        RadioGroup(id="field-radio-control", label="Delivery", options=["Daily", "Weekly"], value=frequency, disabled=false, required=false, descriptionId="", change=setFrequency)
      text "Responsive slider" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      FieldFrame(id="field-slider", labelId="field-slider-label", descriptionId="", invalid=false, orientation=(narrow ? "vertical" : "horizontal"))
        FieldLabel(id="field-slider-label", label="Slider preference", controlId="field-slider-control", disabled=false)
        Slider(id="field-slider-control", label="Volume", value=volume, minimum=0, maximum=100, step=1, disabled=false, change=setVolume)

```

## API Reference

| Property | Type |
| --- | --- |
| FieldSet.id | string |
| FieldSet.legendId | string |
| FieldSet.descriptionId | string |
| FieldSet.disabled | bool |
| FieldLegend.id | string |
| FieldLegend.label | string |
| FieldLegend.variant | string |
| FieldGroup.id | string |
| FieldGroup.nested | bool |
| FieldFrame.id | string |
| FieldFrame.labelId | string |
| FieldFrame.descriptionId | string |
| FieldFrame.invalid | bool |
| FieldFrame.orientation | string |
| FieldContent.id | string |
| FieldLabel.id | string |
| FieldLabel.label | string |
| FieldLabel.controlId | string |
| FieldLabel.disabled | bool |
| FieldTitle.id | string |
| FieldTitle.title | string |
| FieldTitle.disabled | bool |
| FieldDescription.id | string |
| FieldDescription.description | string |
| FieldError.id | string |
| FieldError.messages | string[] |
| FieldSeparator.id | string |
| FieldSeparator.label | string |
| Field.id | string |
| Field.description | string |
| Field.error | string |

## Source

File: packages/ui/forms.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use Control, Field as FieldStyle from "./styles.contract"
component Label
  props
    id: string
    text: string
    required: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  view
    text (text + (required ? " *" : "")) id=id font-size="ui.text-sm" font-weight=uiWeights.medium color="ui.foreground" font-family=uiFontFamily(uiFont) data-rightui-font="body"

component Field
  props
    id: string
    description: string
    error: string
  inject
    uiFont: string
  slot
  view
    column gap="ui.space-8" min-width="0px" role="group" aria-describedby=(id + "-hint")
      children
      text (error != "" ? error : description) id=(id + "-hint") testId=(id + "-hint") font-size="ui.text-xs" line-height="ui.leading-normal" color=(error != "" ? "ui.danger" : "ui.muted-foreground") font-family=uiFontFamily(uiFont) data-rightui-font="body"

component Textarea
  props
    id: string
    label: string
    value: string
    placeholder: string
    error: string
    disabled: bool
    edit: action
  inject
    uiFont: string
  view
    column gap="ui.space-8" min-width="0px"
      Label(id=id + "-label", text=label, required=false)
      TextareaControl(id=id, label=label, value=value, placeholder=placeholder, error=error, descriptionId=id + "-error", disabled=disabled, edit=edit)
      text error id=(id + "-error") font-size="ui.text-xs" color="ui.danger" display=(error == "" ? "none" : "block") font-family=uiFontFamily(uiFont) data-rightui-font="body"

component RadioGroup
  props
    id: string
    label: string
    options: string[]
    value: string
    disabled: bool
    required: bool
    descriptionId: string
    change: action
  view
    RadioGroupAdvanced(id=id, label=label, options=options, value=value, disabled=disabled, required=required, descriptionId=descriptionId, change=change, variant="default")

component RadioGroupAdvanced
  props
    id: string
    label: string
    options: string[]
    value: string
    disabled: bool
    required: bool
    descriptionId: string
    change: action
    variant: string
  inject
    uiWeights: FontWeights
    uiFont: string
  action pick(i: number)
    if not disabled
      match options.at(i)
        case some(v)
          change(v)
          focus(id + "-" + i.toString())
        case none
          preventDefault()
  action keys(k: string)
    if not disabled and options.length > 0 and (k == "ArrowDown" or k == "ArrowRight" or k == "ArrowUp" or k == "ArrowLeft" or k == "Home" or k == "End")
      preventDefault()
      let index = max(0, options.indexOf(value))
      pick(k == "Home" ? 0 : k == "End" ? options.length - 1 : (index + (k == "ArrowDown" or k == "ArrowRight" ? 1 : options.length - 1)) % options.length)
  view
    view id=id testId=id role="radiogroup" aria-label=label aria-required=required aria-describedby=descriptionId
      display="flex" flex-direction=(variant == "segmented" ? "row" : "column") flex-wrap=(variant == "segmented" ? "wrap" : "nowrap") min-width="0px"
      gap=(variant == "segmented" ? "ui.space-4" : "ui.space-12") padding=(variant == "segmented" ? "ui.space-4" : "0px")
      border-radius=(variant == "segmented" ? "ui.radius-control" : "0px") background-color=(variant == "segmented" ? "ui.muted" : "transparent")
      for option, i in options key=option
        button id=(id + "-" + i.toString()) testId=(id + "-" + i.toString()) class=Control font-weight=uiWeights.medium role="radio" aria-checked=(value == option)
          tabindex=(value == option or (options.indexOf(value) == -1 and i == 0) ? 0 : -1) disabled=disabled press=pick(i) key=keys
          display="flex" align-items="center" justify-content=(variant == "segmented" ? "center" : "flex-start") gap="ui.space-8" flex=(variant == "segmented" ? "1 1 auto" : "0 1 auto")
          padding=(variant == "segmented" ? "ui.space-6 ui.space-8" : "0px") min-height=(variant == "segmented" ? "ui.control-sm" : "ui.space-20")
          border-width=(variant == "segmented" ? "ui.border-width" : "0px") border-style="solid" border-color=(variant == "segmented" and value == option ? "ui.tab-active-border" : "transparent")
          background-color=(variant == "segmented" and value == option ? "ui.tab-active" : "transparent") color="ui.foreground" box-shadow=(variant == "segmented" and value == option ? "ui.shadow-control" : "none")
          if variant != "segmented"
            RadioIndicator(checked=(value == option))
          text option font-size=(variant == "segmented" ? "ui.text-xs" : "ui.text-sm") text-align=(variant == "segmented" ? "center" : "left") font-family=uiFontFamily(uiFont) data-rightui-font="body"

component Toggle
  props
    id: string
    label: string
    pressed: bool
    disabled: bool
    reduced: bool
    change: action
  inject
    uiWeights: FontWeights
    uiFont: string
  action toggle
    if not disabled
      change(not pressed)
  view
    button id=id testId=id class=Control font-weight=uiWeights.medium aria-pressed=pressed disabled=disabled press=toggle
      padding="0px ui.space-8" min-width="ui.space-36" height="ui.space-36" border-width="ui.border-width" border-style="solid" border-color="ui.border" background-color=(pressed ? "ui.muted" : "transparent") color="ui.foreground"
      transition=(reduced ? "none" : "background-color ui.duration-fast ui.easing")
      text label font-family=uiFontFamily(uiFont) data-rightui-font="body"

component ToggleGroup
  props
    id: string
    label: string
    options: string[]
    selected: string[]
    disabled: bool
    reduced: bool
    change: action
  inject
    uiWeights: FontWeights
    uiFont: string
  action toggle(value: string)
    if not disabled
      change(selected.includes(value) ? selected.filter(item => item != value) : selected.concat([value]))
  action keys(i: number, k: string)
    if k == "Enter" or k == " "
      preventDefault()
      match options.at(i)
        case some(v)
          toggle(v)
        case none
          preventDefault()
    else if k == "ArrowRight" or k == "ArrowLeft" or k == "Home" or k == "End"
      preventDefault()
      let next = (k == "Home" ? 0 : k == "End" ? options.length - 1 : (i + (k == "ArrowRight" ? 1 : options.length - 1)) % max(1, options.length))
      focus(id + "-" + next.toString())
  view
    row id=id testId=id role="group" aria-label=label gap="ui.space-4" flex-wrap="wrap"
      for option, i in options key=option
        button id=(id + "-" + i.toString()) testId=(id + "-" + i.toString()) class=Control font-weight=uiWeights.medium aria-pressed=selected.includes(option) disabled=disabled press=toggle(option) key=keys(i)
          padding="0px ui.space-8" min-width="ui.space-36" height="ui.space-36" border-width="ui.border-width" border-style="solid" border-color="ui.border" background-color=(selected.includes(option) ? "ui.muted" : "transparent") color="ui.foreground"
          transition=(reduced ? "none" : "background-color ui.duration-fast ui.easing")
          text option font-family=uiFontFamily(uiFont) data-rightui-font="body"

component TextareaControl
  props
    id: string
    label: string
    value: string
    placeholder: string
    error: string
    descriptionId: string
    disabled: bool
    edit: action
  inject
    uiFont: string
  view
    textarea id=id testId=id class=FieldStyle value=value input=edit placeholder=placeholder disabled=disabled font-family=uiFontFamily(uiFont) data-rightui-font="body"
      aria-label=label aria-invalid=(error != "") aria-describedby=descriptionId
      min-height="ui.space-64" max-height="20rem" resize="vertical"

component RadioControl
  props
    id: string
    label: string
    checked: bool
    disabled: bool
    tabIndex: number
    press: action
    keys: action
  inject
    uiWeights: FontWeights
  view
    button id=id testId=id class=Control font-weight=uiWeights.medium role="radio" aria-label=label aria-checked=checked tabindex=tabIndex disabled=disabled press=press key=keys
      display="flex" align-items="center" justify-content="center" padding="0px" min-height="ui.touch-target" min-width="ui.touch-target" border="0" background-color="transparent" color="ui.foreground"
      RadioIndicator(checked=checked)

component RadioIndicator
  props
    checked: bool
  view
    view width="ui.space-16" height="ui.space-16" flex-shrink=0 border-radius="ui.radius-full" border-width="ui.border-width" border-style="solid" border-color=(checked ? "ui.primary" : "ui.border") display="flex" align-items="center" justify-content="center" aria-hidden=true
      if checked
        view width="ui.space-8" height="ui.space-8" border-radius="ui.radius-full" background-color="ui.primary"

```
