Input
A native text input whose value lives in your Model.
Nothing typed yet
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Input } from "#ui/input"
const InputBasicMessage = UiSchema.TaggedEnum({ UpdatedEmail: { value: Schema.String } })
export const InputBasic = Html.component({
name: "InputBasic",
model: Schema.Struct({ email: Schema.String }),
message: InputBasicMessage
}).make({
init: () => ({ email: "" }),
update: {
UpdatedEmail: ($, { value }) => $.evolve({ email: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
Input(h, {
props: { value: model.email },
onUpdate: ({ value }) => InputBasicMessage.UpdatedEmail({ value })
})
.attrs({ type: "email", placeholder: "Email", "aria-label": "Email" }),
h.p.class("text-xs text-muted-foreground tabular-nums").children(
model.email === "" ? "Nothing typed yet" : `In the Model: ${model.email}`
)
)
})
Installation
Set up the project
Once per project: the packages, Tailwind CSS, the theme tokens and the
cnhelper. Follow Installation.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/input.ts import type { Html } from "@effect/ui/html" import { Input as KitInput, type InputElement, type InputProps } from "@effect/ui-kit" import { cn } from "#lib/cn" const base = "block h-8.5 w-full min-w-0 rounded-md border border-input bg-background px-3 text-[13px] text-foreground shadow-sm outline-none " + "transition-[border-color,box-shadow] duration-150 placeholder:text-muted-foreground " + "focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/20 " + "aria-invalid:border-destructive aria-invalid:focus-visible:ring-destructive/20 " + "data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 " + "[&[readonly]]:bg-muted [&[readonly]]:text-muted-foreground [&[readonly]]:shadow-none " + "[&[type=file]]:cursor-pointer [&[type=file]]:pl-0 [&[type=file]]:leading-8 [&[type=file]]:text-muted-foreground " + "file:mr-3 file:h-8 file:cursor-pointer file:border-0 file:border-r file:border-solid file:border-input file:bg-muted file:px-3 " + "file:text-[13px] file:font-medium file:text-foreground" const sizes = { default: "", sm: "h-7 rounded-sm px-2.5 text-xs", lg: "h-10 px-3.5 text-sm" } as const export type InputSize = keyof typeof sizes interface ClassOption { readonly class?: string | undefined } /** The input's classes, for a control that should look like one: a textarea, a native select, a file input. */ export const inputVariants = ( options: { readonly size?: InputSize | undefined; readonly class?: string | undefined } = {} ): string => cn(base, sizes[options.size ?? "default"], options.class) /** * The kit's Input with a look. Pass the value as `props` and what an edit * sends as `onUpdate`, and chain the native attributes: * `Input(h, { props: { value }, onUpdate }).attrs({ name: "email", type: "email" })`. * In a field, pass `element: field.control(h.input)`. */ export const Input = <Msg, R = never>( h: { readonly input: InputElement<Msg> }, options: { readonly size?: InputSize | undefined readonly class?: string | undefined readonly element?: InputElement<Msg, R> | undefined readonly props: InputProps readonly onUpdate?: ((event: { readonly value: string }) => NoInfer<Msg> | undefined) | undefined } ): InputElement<Msg, R> => KitInput<Msg, R>(h, { element: options.element, props: options.props, onUpdate: options.onUpdate }) .class(inputVariants(options)) /** * Draws the border around an input and what sits beside it: an `InputIcon`, * an `InputAddon` or an `InputAction`. The input inside loses its own border. */ export const InputGroup = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) => h.div.class(cn( "flex h-8.5 w-full items-center rounded-md border border-input bg-background shadow-sm", "transition-[border-color,box-shadow] duration-150 focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/20", "has-[input[aria-invalid=true]]:border-destructive has-[input[data-disabled]]:opacity-50", "[&>input]:h-full [&>input]:flex-1 [&>input]:border-0 [&>input]:bg-transparent [&>input]:shadow-none [&>input]:focus-visible:ring-0", options.class )) /** A leading icon. Draw an SVG inside it. */ export const InputIcon = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) => h.span .class(cn("ml-2.5 inline-flex shrink-0 text-muted-foreground [&_svg]:size-[15px] [&+input]:pl-2", options.class)) .attr("aria-hidden", "true") /** A muted block of text at either end: a protocol, a unit, a currency. */ export const InputAddon = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) => h.span.class(cn( "inline-flex shrink-0 items-center self-stretch bg-muted px-2.5 text-[13px] whitespace-nowrap text-muted-foreground", "first:rounded-l-[calc(var(--radius-md)-1px)] first:border-r first:border-input", "last:rounded-r-[calc(var(--radius-md)-1px)] last:border-l last:border-input", options.class )) /** A small button inside the group, such as one that reveals a password. Give it an `aria-label`. */ export const InputAction = <Msg>(h: Html.H<Msg>, options: ClassOption = {}) => h.button .class(cn( "mr-1 inline-flex size-6.5 shrink-0 items-center justify-center rounded-sm text-muted-foreground", "transition-colors duration-150 hover:bg-accent hover:text-foreground [&_svg]:size-[15px]", options.class )) .attr("type", "button")Import it
import { inputVariants, Input, InputGroup, InputIcon, InputAddon, InputAction } from "#ui/input"
Usage
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Input } from "#ui/input"
const InputBasicMessage = UiSchema.TaggedEnum({ UpdatedEmail: { value: Schema.String } })
export const InputBasic = Html.component({
name: "InputBasic",
model: Schema.Struct({ email: Schema.String }),
message: InputBasicMessage
}).make({
init: () => ({ email: "" }),
update: {
UpdatedEmail: ($, { value }) => $.evolve({ email: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
Input(h, {
props: { value: model.email },
onUpdate: ({ value }) => InputBasicMessage.UpdatedEmail({ value })
})
.attrs({ type: "email", placeholder: "Email", "aria-label": "Email" }),
h.p.class("text-xs text-muted-foreground tabular-nums").children(
model.email === "" ? "Nothing typed yet" : `In the Model: ${model.email}`
)
)
})
Examples
With label
A label pointing at the input's id, and a hint it is described by.
Your profile will be at effect.website/ada
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Input } from "#ui/input"
const InputWithLabelMessage = UiSchema.TaggedEnum({ UpdatedUsername: { value: Schema.String } })
export const InputWithLabel = Html.component({
name: "InputWithLabel",
model: Schema.Struct({ username: Schema.String }),
message: InputWithLabelMessage
}).make({
init: () => ({ username: "ada" }),
update: {
UpdatedUsername: ($, { value }) => $.evolve({ username: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
h.div.children(
h.label.class("mb-1.5 block text-[13px] font-medium").attr("for", "input-username").children("Username"),
Input(h, {
props: { value: model.username },
onUpdate: ({ value }) => InputWithLabelMessage.UpdatedUsername({ value })
})
.attrs({
id: "input-username",
name: "username",
autocomplete: "username",
"aria-describedby": "input-username-hint"
}),
h.p.class("mt-1.5 text-xs text-muted-foreground data-[invalid]:text-destructive").attr("id", "input-username-hint").children(
`Your profile will be at effect.website/${model.username === "" ? "…" : model.username}`
)
)
)
})
Sizes
Three heights from a data-size attribute.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Input } from "#ui/input"
const sizes = ["sm", "default", "lg"] as const
const InputSizesMessage = UiSchema.TaggedEnum({
UpdatedInput: { size: Schema.Literals(sizes), value: Schema.String }
})
export const InputSizes = Html.component({
name: "InputSizes",
model: Schema.Struct({ sm: Schema.String, default: Schema.String, lg: Schema.String }),
message: InputSizesMessage
}).make({
init: () => ({ sm: "", default: "", lg: "" }),
update: {
UpdatedInput: ($, { size, value }) => $.evolve({ [size]: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
sizes.map((size) =>
Input(h, {
size,
props: { value: model[size] },
onUpdate: ({ value }) => InputSizesMessage.UpdatedInput({ size, value })
})
.attrs({
placeholder: size === "sm" ? "Small" : size === "lg" ? "Large" : "Default",
"aria-label": `${size} input`
})
)
)
})
Icons and addons
A group draws the border, so an icon, a shortcut or a unit sits inside it.
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Input, InputAddon, InputGroup, InputIcon } from "#ui/input"
const InputAddonsMessage = UiSchema.TaggedEnum({
UpdatedQuery: { value: Schema.String },
UpdatedSite: { value: Schema.String },
UpdatedAmount: { value: Schema.String }
})
export const InputAddons = Html.component({
name: "InputAddons",
model: Schema.Struct({ query: Schema.String, site: Schema.String, amount: Schema.String }),
message: InputAddonsMessage
}).make({
init: () => ({ query: "", site: "effect", amount: "49" }),
update: {
UpdatedQuery: ($, { value }) => $.evolve({ query: value }),
UpdatedSite: ($, { value }) => $.evolve({ site: value }),
UpdatedAmount: ($, { value }) => $.evolve({ amount: value })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
InputGroup(h).children(
InputIcon(h).children(
h.el("svg")
.attrs({ viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2" })
.children(
h.el("circle").attrs({ cx: "11", cy: "11", r: "7" }),
h.el("path").attrs({ d: "m20 20-3.5-3.5", "stroke-linecap": "round" })
)
),
Input(h, {
props: { value: model.query },
onUpdate: ({ value }) => InputAddonsMessage.UpdatedQuery({ value })
})
.attrs({ type: "search", placeholder: "Search components", "aria-label": "Search" }),
h.kbd.class("mr-2 shrink-0 rounded-xs border bg-muted px-1.5 py-px font-mono text-[11px] text-muted-foreground").children("⌘K")
),
InputGroup(h).children(
InputAddon(h).children("https://"),
Input(h, {
props: { value: model.site },
onUpdate: ({ value }) => InputAddonsMessage.UpdatedSite({ value })
})
.attr("aria-label", "Site name"),
InputAddon(h).children(".website")
),
InputGroup(h).children(
InputAddon(h).children("$"),
Input(h, {
props: { value: model.amount },
onUpdate: ({ value }) => InputAddonsMessage.UpdatedAmount({ value })
})
.attrs({ inputmode: "decimal", "aria-label": "Amount in US dollars" }),
InputAddon(h).children("USD")
)
)
})
Disabled, read-only and invalid
The disabled prop, the native readonly attribute and aria-invalid.
Enter an email address, such as ada@example.com
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { Input } from "#ui/input"
const InputStatesMessage = UiSchema.TaggedEnum({ UpdatedEmail: { value: Schema.String } })
const isEmail = (value: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value)
export const InputStates = Html.component({
name: "InputStates",
model: Schema.Struct({ email: Schema.String }),
message: InputStatesMessage
}).make({
init: () => ({ email: "ada@example" }),
update: {
UpdatedEmail: ($, { value }) => $.evolve({ email: value })
},
view: (h, model) => {
const invalid = !isEmail(model.email)
return h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
h.div.children(
h.label.class("mb-1.5 block text-[13px] font-medium").attr("for", "input-states-disabled").children("Disabled"),
Input(h, { props: { value: "ada@example.com", disabled: true } })
.attr("id", "input-states-disabled")
),
h.div.children(
h.label.class("mb-1.5 block text-[13px] font-medium").attr("for", "input-states-readonly").children("Read-only"),
Input(h, { props: { value: "org_2f9c41" } })
.attrs({ id: "input-states-readonly", readonly: "" })
),
h.div.children(
h.label.class("mb-1.5 block text-[13px] font-medium").attr("for", "input-states-invalid").children("Invalid"),
Input(h, {
props: { value: model.email },
onUpdate: ({ value }) => InputStatesMessage.UpdatedEmail({ value })
})
.attrs({
id: "input-states-invalid",
type: "email",
"aria-invalid": invalid ? "true" : undefined,
"aria-describedby": "input-states-hint"
}),
h.p.class("mt-1.5 text-xs text-muted-foreground data-[invalid]:text-destructive")
.attrs({ id: "input-states-hint", "data-invalid": invalid ? "" : undefined })
.children(invalid ? "Enter an email address, such as ada@example.com" : "Looks good")
)
)
}
})
Password
A redacted value with a button that reveals it.
13 characters
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Redacted, Schema } from "effect"
import { Input, InputAction, InputGroup } from "#ui/input"
const InputPasswordMessage = UiSchema.TaggedEnum({
UpdatedPassword: { value: Schema.Redacted(Schema.String) },
ClickedReveal: {}
})
export const InputPassword = Html.component({
name: "InputPassword",
model: Schema.Struct({ password: Schema.Redacted(Schema.String), revealed: Schema.Boolean }),
message: InputPasswordMessage
}).make({
init: () => ({ password: Redacted.make("correct horse"), revealed: false }),
update: {
UpdatedPassword: ($, { value }) => $.evolve({ password: value }),
ClickedReveal: ($) => $.evolve({ revealed: (revealed) => !revealed })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
h.div.children(
h.label.class("mb-1.5 block text-[13px] font-medium").attr("for", "input-password").children("Password"),
InputGroup(h).children(
// A secret is redacted in the Model and in the message, and unwrapped only for the input.
Input(h, {
props: { value: Redacted.value(model.password) },
onUpdate: ({ value }) => InputPasswordMessage.UpdatedPassword({ value: Redacted.make(value) })
})
.attrs({
id: "input-password",
type: model.revealed ? "text" : "password",
autocomplete: "new-password"
}),
InputAction(h)
.attrs({
"aria-label": model.revealed ? "Hide password" : "Show password",
"aria-pressed": model.revealed ? "true" : "false"
})
.on("click", InputPasswordMessage.ClickedReveal())
.children(
h.el("svg")
.attrs({
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
"stroke-width": "2",
"stroke-linecap": "round",
"stroke-linejoin": "round"
})
.children(
h.el("path").attr("d", "M2 12s3.6-7 10-7 10 7 10 7-3.6 7-10 7S2 12 2 12Z"),
h.el("circle").attrs({ cx: "12", cy: "12", r: "3" }),
model.revealed ? null : h.el("path").attr("d", "M4 4l16 16")
)
)
),
h.p.class("mt-1.5 text-xs text-muted-foreground data-[invalid]:text-destructive").children(`${Redacted.value(model.password).length} characters`)
)
)
})
File
A file input styled with the same class.
No file chosen
import { UiSchema } from "@effect/ui"
import { Html } from "@effect/ui/html"
import { Schema } from "effect"
import { inputVariants } from "#ui/input"
const InputFileMessage = UiSchema.TaggedEnum({
ChoseFiles: { names: Schema.Array(Schema.String) }
})
export const InputFile = Html.component({
name: "InputFile",
model: Schema.Struct({ names: Schema.Array(Schema.String) }),
message: InputFileMessage
}).make({
init: () => ({ names: [] }),
update: {
ChoseFiles: ($, { names }) => $.evolve({ names })
},
view: (h, model) =>
h.div.class("flex w-full max-w-90 flex-col gap-3.5").children(
h.div.children(
h.label.class("mb-1.5 block text-[13px] font-medium").attr("for", "input-file").children("Attachments"),
// A file input has no value to control, so it is a plain h.input with the same class.
h.input.class(inputVariants())
.attrs({ id: "input-file", type: "file", multiple: "" })
.on("change", ({ files }) => InputFileMessage.ChoseFiles({ names: files.map((file) => file.name) })),
h.p.class("mt-1.5 text-xs text-muted-foreground data-[invalid]:text-destructive").children(
model.names.length === 0 ? "No file chosen" : model.names.join(", ")
)
)
)
})
API reference
Props
| Prop | Type | Description |
|---|---|---|
props.value | string | What the input shows. Store what onUpdate reports and pass it back. |
props.disabled | boolean | Sets the native disabled attribute and data-disabled, and sends nothing. |
element | h.input | An input builder to share with another part, such as field.control(h.input). |
Events
| Event | Description |
|---|---|
onUpdate | Called on every edit with { value }, the text the user left in the field. |
Data attributes
| Attribute | Description |
|---|---|
data-disabled | Present while disabled is set. |