Shown next to your comments and commits.

Installation

  1. Set up the project

    Once per project: the packages, Tailwind CSS, the theme tokens and the cn helper. Follow Installation.

  2. Copy the component into your project

    The kit owns behaviour, ARIA and data attributes. This file is the look, written with Tailwind classes, and it is yours to change.

    src/ui/field.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { Field } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What the kit's `Field` hands its `view` closure. */
    export type FieldParts = Component.Headless.Parts<typeof Field>
    
    interface ClassOption {
      readonly class?: string | undefined
    }
    
    const orientations = {
      vertical: "flex-col gap-1.5",
      horizontal: "flex-row items-center justify-between gap-4"
    } as const
    
    export type FieldOrientation = keyof typeof orientations
    
    /**
     * The field's root. `vertical` stacks the label, the control and the text
     * under it. `horizontal` puts a `FieldContent` on the left and a small
     * control, such as a switch, on the right.
     */
    export const FieldRoot = <Msg>(
      h: Html.H<Msg>,
      field: FieldParts,
      options: ClassOption & { readonly orientation?: FieldOrientation | undefined } = {}
    ) => field.root(h.div.class(cn("flex w-full min-w-0", orientations[options.orientation ?? "vertical"], options.class)))
    
    /** The column of label and description in a horizontal field. */
    export const FieldContent = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) =>
      h.div.class(cn("flex min-w-0 flex-col gap-0.5", options.class))
    
    export const FieldLabel = <Msg>(h: Html.H<Msg>, field: FieldParts, options: ClassOption = {}) =>
      field.label(
        h.label.class(cn(
          "text-[13px] leading-snug font-medium data-[disabled]:opacity-50 data-[invalid]:text-destructive",
          options.class
        ))
      )
    
    /** The name of a field drawn with `field.group`, such as a range slider or a one-time code. */
    export const FieldLegend = <Msg>(h: Html.H<Msg>, field: FieldParts, options: ClassOption = {}) =>
      field.legend(
        h.div.class(cn(
          "text-[13px] leading-snug font-medium data-[disabled]:opacity-50 data-[invalid]:text-destructive",
          options.class
        ))
      )
    
    /** Needs `described: true` in the field's props, so the control points at it. */
    export const FieldDescription = <Msg>(h: Html.H<Msg>, field: FieldParts, options: ClassOption = {}) =>
      field.description(
        h.p.class(cn("m-0 text-xs leading-normal text-muted-foreground data-[disabled]:opacity-50", options.class))
      )
    
    /** `undefined` unless the field is invalid and enabled: `FieldError(h, field)?.children(message)`. */
    export const FieldError = <Msg>(h: Html.H<Msg>, field: FieldParts, options: ClassOption = {}) =>
      field.error(h.div.class(cn("text-xs leading-normal text-destructive", options.class)))
    
  3. Import it

    import { FieldRoot, FieldContent, FieldLabel, FieldLegend, FieldDescription, FieldError } from "#ui/field"
    

Usage

Label and description.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Field } from "@effect/ui-kit"
import { Schema } from "effect"
import { FieldDescription, FieldLabel, FieldRoot } from "#ui/field"
import { Input } from "#ui/input"

const FieldBasicMessage = UiSchema.TaggedEnum({ UpdatedName: { value: Schema.String } })

export const FieldBasic = Html.component({
  name: "FieldBasic",
  model: Schema.Struct({ name: Schema.String }),
  message: FieldBasicMessage
}).make({
  init: () => ({ name: "" }),
  update: {
    UpdatedName: ($, { value }) => $.evolve({ name: value })
  },
  view: (h, model) =>
    h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
      Field(h, {
        props: { described: true, filled: model.name !== "" },
        view: (field) =>
          FieldRoot(h, field).children(
            FieldLabel(h, field).children("Display name"),
            Input(h, {
              element: field.control(h.input),
              props: { value: model.name },
              onUpdate: ({ value }) => FieldBasicMessage.UpdatedName({ value })
            }).attrs({ name: "name", placeholder: "Ada Lovelace" }),
            FieldDescription(h, field).children(
              "Shown next to your comments and commits."
            )
          )
      })
    )
})

Examples

Validation

Rules from Validation, mapped to the field with fieldValidationProps, with the error shown as you type.

The result shows once you start typing.

Required

A required field that is checked when it loses focus.

3 to 24 characters. Checked when you leave the field.

Click into the field and Tab away to see the error.

With a switch

A horizontal field whose control is a Switch's root.

Email me when someone mentions me.

A summary of activity every Monday.

With a slider

The field's control is the thumb's range input.

80%

Larger files that stay sharp.

Textarea and select

Any native control takes field.control.

0 of 120 characters

Disabled

A disabled field disables its control, marks every part and hides its error.

Managed by your organisation.

Anatomy

The parts the view closure receives. Apply each one to an element of your own.

PartDescription
rootAny element around the field, usually a <div>.
labelA <label>, pointed at the control's id.
controlAn <input>, <textarea>, <select> or <button>. Its result is another kit call's element.
descriptionAny element. Needs the described prop, so the control points at it.
errorAny element with role="alert". Returns undefined unless the field is invalid and enabled.
group / legend / memberFor a control made of several focusable elements, such as a range slider or an OTP field.
focusWithinAround a trigger and its popup, for a select or a combobox.

API reference

Props

PropTypeDescription
invalidbooleantrue marks the field invalid and shows the error, false marks it valid, left out it is neither.
disabledbooleanDisables the control, sets data-disabled on every part and hides the error.
describedbooleanThe field draws a description, so the control gets aria-describedby.
filledbooleanSets data-filled. You hold the value, so you say whether there is one.
dirtybooleanSets data-dirty. You know the value the field started with.
idstringThe control's id and the label's for. Generated when left out.

Data attributes

AttributeDescription
data-invalid / data-validOn every part. Neither while invalid is left out.
data-touchedThe control has lost focus at least once.
data-focusedThe control has focus.
data-filledFrom the filled prop.
data-dirtyFrom the dirty prop.
data-disabledThe field, or a Fieldset around it, is disabled.

The components this page's file and examples import.

  • Button: A native button that can stay focusable while disabled.
  • Input: A native text input whose value lives in your Model.
  • Slider: A controlled slider with one thumb or several, dragged or moved with the keyboard.

effectcn is built with Effect UI and @effect/ui-kit. Not affiliated with Effectful Technologies.