An input with icons, words or a button inside its frame, such as a search box or a price.
- 4.8 kB JS 4.8 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 16 tests
⌘K
Letters, numbers and dashes.
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { FormField, Input } from "./ui/uicontext/components/FormControls.ts"import { inAddons, InputAddons } from "./ui/uicontext/components/InputAddons.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ form: { display: "grid", gap: space.xl, inlineSize: "min(100%, 26rem)" } })const Message = UiSchema.TaggedEnum({ ChangedQuery: { value: Schema.String }, ClearedQuery: {}, ChangedSlug: { value: Schema.String }, ToggledKey: {},})const searchIcon = <M>(h: Html.H<M>) => h.svg.attrs({ width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", "stroke-width": "1.5", "stroke-linecap": "round" }).children(h.path.attrs({ d: "M7 12.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zM14.5 14.5l-3.6-3.6" }))const eye = <M>(h: Html.H<M>, open: boolean) => h.svg.attrs({ width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", stroke: "currentColor", "stroke-width": "1.5", "stroke-linecap": "round" }).children( h.path.attrs({ d: open ? "M1.5 8S4 3.5 8 3.5 14.5 8 14.5 8 12 12.5 8 12.5 1.5 8 1.5 8zM8 10a2 2 0 1 0 0-4 2 2 0 0 0 0 4z" : "M2 2l12 12M6.6 6.6a2 2 0 0 0 2.8 2.8M4.2 4.4C2.5 5.6 1.5 8 1.5 8S4 12.5 8 12.5c1.3 0 2.4-.4 3.4-1M7 3.6c.3 0 .7-.1 1-.1 4 0 6.5 4.5 6.5 4.5s-.5 1-1.5 2" }), )// a workspace's settings: search with a key hint, a URL with its fixed start, and an API key you can showexport const WorkspaceSettings = Html.component({ name: "WorkspaceSettings", model: Schema.Struct({ query: Schema.String, slug: Schema.String, shown: Schema.Boolean }), message: Message,}).make({ init: () => ({ query: "", slug: "northwind", shown: false }), update: { ChangedQuery: ($, { value }) => $.evolve({ query: value }), ClearedQuery: ($) => $.evolve({ query: "" }), ChangedSlug: ($, { value }) => $.evolve({ slug: value }), ToggledKey: ($) => $.evolve({ shown: (s) => !s }), }, view: (h, model) => h.div.attrs(sx(styles.form)).children( InputAddons( h, { start: [{ icon: searchIcon(h) }], end: model.query === "" ? [{ kbd: "mod+k" }] : [{ button: { label: "Clear", onPress: Message.ClearedQuery() } }] }, Input(h, { style: inAddons }) .attrs({ type: "search", placeholder: "Search settings", "aria-label": "Search settings" }) .value(model.query) .on("input", ({ value }) => Message.ChangedQuery({ value })), ), FormField(h, { id: "slug", label: "Workspace URL", hint: "Letters, numbers and dashes." }, (field) => InputAddons( h, { dir: "ltr", start: [{ text: "app.acme.com/" }] }, Input(h, { style: inAddons }).attrs({ ...field, autocomplete: "off", spellcheck: "false" }).value(model.slug).on("input", ({ value }) => Message.ChangedSlug({ value })), )), FormField(h, { id: "api-key", label: "API key" }, (field) => InputAddons( h, { dir: "ltr", end: [{ button: { label: model.shown ? "Hide key" : "Show key", icon: eye(h, !model.shown), iconOnly: true, onPress: Message.ToggledKey() } }] }, Input(h, { style: inAddons }).attrs({ ...field, type: model.shown ? "text" : "password", readonly: "", autocomplete: "off" }).value("sk_live_51Hq8vT2eZvKYlo2C0f9w"), )), ),})When to use
An input with icons, words or a button inside its frame, such as a search box or a price. Use Input instead when the input needs nothing inside its frame.
Usage
import { Input } from "./ui/uicontext/components/FormControls.ts"import { inAddons, InputAddons } from "./ui/uicontext/components/InputAddons.ts"export const Website = <M>(h: Html.H<M>) => InputAddons(h, { start: [{ text: "https://" }] }, Input(h, { style: inAddons }).attrs({ "aria-label": "Website", placeholder: "example.com" }))Examples
A price and a unit
content takes any control, such as a currency picker. Fixed words are hidden from screen readers, so the label says “in kg”.
import { UiSchema } from "effect/ui"import { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { Schema } from "effect"import { FormField, Input, Select } from "./ui/uicontext/components/FormControls.ts"import { inAddons, InputAddons } from "./ui/uicontext/components/InputAddons.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { space } from "./ui/uicontext/components/tokens.stylex.ts"const styles = stylex.create({ form: { display: "grid", gap: space.xl, inlineSize: "min(100%, 22rem)" }, unit: { blockSize: "100%", borderWidth: 0, borderRadius: 0, backgroundColor: "transparent", boxShadow: "none", paddingInlineStart: space.sm },})const Message = UiSchema.TaggedEnum({ ChangedPrice: { value: Schema.String }, ChangedCurrency: { value: Schema.String }, ChangedWeight: { value: Schema.String } })// a product's price and shipping weight: the currency is a picker inside the field, the unit is fixedexport const ProductPrice = Html.component({ name: "ProductPrice", model: Schema.Struct({ price: Schema.String, currency: Schema.String, weight: Schema.String }), message: Message,}).make({ init: () => ({ price: "48.00", currency: "USD", weight: "0.4" }), update: { ChangedPrice: ($, { value }) => $.evolve({ price: value }), ChangedCurrency: ($, { value }) => $.evolve({ currency: value }), ChangedWeight: ($, { value }) => $.evolve({ weight: value }), }, view: (h, model) => h.div.attrs(sx(styles.form)).children( FormField(h, { id: "price", label: "Price" }, (field) => InputAddons( h, { start: [{ text: model.currency === "EUR" ? "€" : model.currency === "GBP" ? "£" : "$" }], end: [{ content: Select(h, { style: styles.unit }) .attrs({ "aria-label": "Currency" }) .value(model.currency) .on("change", ({ value }) => Message.ChangedCurrency({ value })) .children(["USD", "EUR", "GBP"].map((c) => h.option.key(c).attrs({ value: c }).children(c))), }], }, Input(h, { style: inAddons }).attrs({ ...field, inputmode: "decimal" }).value(model.price).on("input", ({ value }) => Message.ChangedPrice({ value })), )), FormField(h, { id: "weight", label: "Shipping weight in kg" }, (field) => InputAddons(h, { end: [{ text: "kg" }] }, Input(h, { style: inAddons }).attrs({ ...field, inputmode: "decimal" }).value(model.weight).on("input", ({ value }) => Message.ChangedWeight({ value })))), ),})API reference
InputAddonsView function
Options
| Name | Type | Description |
|---|---|---|
start | InputAddon<Message>[] | — |
end | InputAddon<Message>[] | — |
dir | "ltr" | "ltr" for what always reads left to right, such as a URL, a key or code, in a right-to-left page too. |
style | Style | — |
InputView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
invalid | boolean | — | Marks the value as wrong: a red border, and aria-invalid for assistive technology. |
size | "sm" | "md" | md | sm for toolbars, table rows and side panels, a control's height; md for forms; md by default. |
style | Style | — | StyleX styles applied after the control's own. |
SelectView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
invalid | boolean | — | Marks the value as wrong: a red border, and aria-invalid for assistive technology. |
size | "sm" | "md" | md | sm for toolbars, table rows and side panels, a control's height; md for forms; md by default. |
style | Style | — | StyleX styles applied after the control's own. |
TextareaView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
invalid | boolean | — | Marks the value as wrong: a red border, and aria-invalid for assistive technology. |
size | "sm" | "md" | md | sm for toolbars, table rows and side panels, a control's height; md for forms; md by default. |
style | Style | — | StyleX styles applied after the control's own. |
InputGroupView function
Options
| Name | Type | Description |
|---|---|---|
start | string | — |
end | string | — |
style | Style | — |
FormFieldView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
idrequired | string | — | The control's id. The label, description and error take ids made from it. |
labelrequired | string | — | The label's text, which names the control. |
description | string | — | A hint under the control, read out with it. |
error | string | — | What's wrong with the value. Shown under the control, announced, and marks the control invalid. |
hint | string | — | A short note at the end of the label's line: a count such as "12/48", or "Max 5 MB". |
hintOver | boolean | — | The hint warns, as a count past its limit does. |
required | boolean | — | An empty value is an error: a mark after the label, and aria-required on the control. |
optional | boolean | — | May stay empty, said after the label: for a form where most fields are required. |
status | { readonly tone: "pending" | "success"; readonly text: string } | — | Where an async check stands, in the error's place while there's no error: "Checking…", "acme is available". |
layout | "stacked" | "inline" | "inline-end" | stacked | Where the label sits: above the control (text fields, selects, a radio group), or beside it, with the control first (a checkbox) or last (a switch in a settings row); stacked by default |
group | boolean | — | The control is a group of controls (a radio group, a segmented control): a group can't be the target of a <label for>, so the label names it through aria-labelledby instead |
labelStyle | Style | — | StyleX styles for the label: a row field's label that hides behind column headings. |
style | Style | — | StyleX styles for the field's wrapper. |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/input-group