Thursday, November 26, 2026

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/date-field.ts
    import type { Component } from "@effect/ui"
    import type { Html } from "@effect/ui/html"
    import type { DateField } from "@effect/ui-kit"
    import { cn } from "#lib/cn"
    
    /** What the kit's `DateField` passes to its `view` closure. A `DatePicker`'s parts fit too. */
    export type DateFieldParts = Pick<Component.Headless.Parts<typeof DateField>, "input" | "pattern">
    
    const base = "h-8.5 w-44 min-w-0 rounded-md border border-input bg-background px-3 text-[13px] text-foreground " +
      "tabular-nums shadow-sm outline-none transition-[border-color,box-shadow] duration-150 " +
      "placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/25 " +
      "data-[invalid]:border-destructive data-[invalid]:focus-visible:ring-destructive/25 " +
      "data-[readonly]:bg-muted data-[readonly]:shadow-none " +
      "data-[disabled]:cursor-not-allowed data-[disabled]:bg-muted data-[disabled]:opacity-60"
    
    const sizes = {
      default: "",
      sm: "h-7 w-36 rounded-sm px-2 text-xs",
      lg: "h-10 w-52 px-3.5 text-sm"
    } as const
    
    export type DateFieldSize = keyof typeof sizes
    
    /** The input's classes, for an `<input>` that goes through another part first, such as `field.control`. */
    export const dateFieldVariants = (
      options: { readonly size?: DateFieldSize | undefined; readonly class?: string | undefined } = {}
    ): string => cn(base, sizes[options.size ?? "default"], options.class)
    
    /**
     * The typed date. Its placeholder is the locale's pattern, such as `mm/dd/yyyy`.
     * Inside a `Field`, pass the decorated input as `element`:
     * `DateFieldInput(h, dateField, { element: field.control(h.input) })`.
     */
    export const DateFieldInput = <Msg>(
      h: Html.H<Msg>,
      dateField: DateFieldParts,
      options: {
        readonly size?: DateFieldSize | undefined
        readonly class?: string | undefined
        readonly element?: Html.H<Msg>["input"] | undefined
      } = {}
    ) =>
      dateField.input((options.element ?? h.input).class(dateFieldVariants(options))).attr(
        "placeholder",
        dateField.pattern
      )
    
  3. Import it

    import { dateFieldVariants, DateFieldInput } from "#ui/date-field"
    

Usage

Basic.ts
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { DateField } from "@effect/ui-kit"
import { DateTime, Schema } from "effect"
import { DateFieldInput } from "#ui/date-field"

const DateFieldBasicMessage = UiSchema.TaggedEnum({
  CommittedDateField: { date: Schema.NullOr(Schema.DateTimeUtc) }
})

// The field's dates are midnight UTC, so format them in UTC.
const longDate = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeZone: "UTC" })

export const DateFieldBasic = Html.component({
  name: "DateFieldBasic",
  model: Schema.Struct({ date: Schema.NullOr(Schema.DateTimeUtc) }),
  message: DateFieldBasicMessage
}).make({
  init: () => ({ date: DateTime.makeUnsafe("2026-11-26T00:00:00Z") }),
  update: {
    CommittedDateField: ($, { date }) => $.evolve({ date })
  },
  view: (h, model) =>
    DateField(h, {
      props: { value: model.date, locale: "en-US" },
      onEmit: {
        Committed: ({ date }) => DateFieldBasicMessage.CommittedDateField({ date })
      },
      view: (dateField) =>
        h.div.class("flex flex-col items-center gap-3").children(
          DateFieldInput(h, dateField).attr("aria-label", "Date"),
          h.p.class("m-0 text-xs text-muted-foreground tabular-nums").children(
            model.date === null ? "No date" : DateTime.formatIntl(model.date, longDate)
          )
        )
    })
})

Examples

ISO format

A locale that writes yyyy-mm-dd, and a hidden input that submits the ISO date under name.

published=2026-11-26

Locale formats

One date, four locales. Each field parses its own order, and ISO everywhere.

Label, description and error

Inside a Field, which names the input and shows the consumer's validation.

Invoices are due at the end of this day.

Sizes

Disabled and read-only

Disabled submits nothing; read-only stays focusable and selectable but commits nothing.

Anatomy

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

PartDescription
inputThe <input>. The kit wraps it in a display: contents span that also holds the hidden form input.
patternValue: the locale's pattern, such as dd/mm/yyyy, for a placeholder or hint.
value, inputValueValues: the normalized date and the text the input shows.
disabled, readOnly, requiredValues: the props, as booleans.

API reference

Props

PropTypeDescription
valueDateTime | nullThe date, or null for an empty field.
localestring | string[]Formats, parses and derives pattern.
namestringAdds a hidden input that submits the date as YYYY-MM-DD.
formstringThe id of the form that submits it, when outside one.
disabledbooleanDisables both inputs and commits nothing.
readOnlybooleanFocusable and selectable, but commits nothing.
requiredbooleanSets the native required attribute.

Events

EventDescription
Committed{ date }: blur or Enter with a parsed date that differs from value. Empty text commits null.

Data attributes

AttributeDescription
data-disabledOn input: the field is disabled.
data-readonlyOn input: the field is read-only.
data-requiredOn input: the field is required.
data-invalidOn input, from an enclosing Field whose invalid is true.

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