Selection and input · Material 3: Text fields, Search
Text field
Single-line, multi-line and search input in a Field with label, help and error. A sunken pill that lifts to the page surface when you type.
Examples and variants
With label and help
The placeholder slides aside on the first letter.
Shown on the project switcher.
function TextFieldBasic() {
return (
<Field className="w-full max-w-sm">
<FieldLabel htmlFor="tf-name">Project name</FieldLabel>
<Input id="tf-name" placeholder="Northwind" aria-describedby="tf-name-help" />
<FieldDescription id="tf-name-help">Shown on the project switcher.</FieldDescription>
</Field>
)
}Error
Type one or two letters. The error rises in; fixing it leaves a brief check.
function TextFieldError() {
const [value, setValue] = useState("")
const invalid = value.length > 0 && value.length < 3
return (
<Field className="w-full max-w-sm" data-invalid={invalid || undefined}>
<FieldLabel htmlFor="tf-handle">Handle</FieldLabel>
<Input
id="tf-handle"
value={value}
onChange={(event) => setValue(event.target.value)}
aria-invalid={invalid || undefined}
placeholder="At least 3 letters"
/>
<div className="min-h-5">
<FieldError>{invalid ? "Use at least 3 letters." : null}</FieldError>
</div>
</Field>
)
}Multi-line
Textarea grows with its content.
function TextFieldMultiline() {
return (
<Field className="w-full max-w-sm">
<FieldLabel htmlFor="tf-notes">Notes</FieldLabel>
<Textarea id="tf-notes" placeholder="It grows as you type" />
</Field>
)
}Search
Input group with a search icon that turns into a spinner, then a clear button.
function TextFieldSearch() {
return (
<div className="w-full max-w-sm">
<InputGroupSearch placeholder="Search components" aria-label="Search components" />
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/field.json
npx shadcn@latest add https://isotopi.com/r/input.json
npx shadcn@latest add https://isotopi.com/r/textarea.json
npx shadcn@latest add https://isotopi.com/r/input-group.json
npx shadcn@latest add https://isotopi.com/r/label.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install @base-ui/react class-variance-authority cn lucide-reactCopy these 6 files to the same paths in your project. Imports use the
@/alias for your project root.components/ui/label.tsxlabel
components/ui/label.tsx"use client" import * as React from "react" import { cn } from "cn" function Label({ className, ...props }: React.ComponentProps<"label">) { return ( <label data-slot="label" className={cn( "flex items-center gap-2 text-sm leading-none font-medium select-none group-data-[disabled=true]:pointer-events-none group-data-[disabled=true]:text-(--field-label-disabled) group-data-[invalid=true]/field:text-(--color-danger-fg) peer-disabled:cursor-not-allowed peer-disabled:text-(--field-label-disabled)", className )} {...props} /> ) } export { Label }components/ui/separator.tsxseparator
components/ui/separator.tsx"use client" import { Separator as SeparatorPrimitive } from "@base-ui/react/separator" import { cn } from "cn" /* Divider. Minimal draws no lines: - variant="space" (default): an empty gap of --divider-space. - variant="tint": a faint --divider-thickness line in --divider-color, centred in the same gap, for dense lists that need a visible break. */ function Separator({ className, orientation = "horizontal", variant = "space", ...props }: SeparatorPrimitive.Props & { variant?: "space" | "tint" }) { return ( <SeparatorPrimitive data-slot="separator" data-variant={variant} orientation={orientation} className={cn( "shrink-0 bg-clip-content data-horizontal:h-(--divider-space) data-horizontal:w-full data-vertical:w-(--divider-space) data-vertical:self-stretch", variant === "tint" ? "bg-(--divider-color) data-horizontal:py-[calc((var(--divider-space)-var(--divider-thickness))/2)] data-vertical:px-[calc((var(--divider-space)-var(--divider-thickness))/2)]" : "bg-transparent", className )} {...props} /> ) } export { Separator }components/ui/field.tsxfield
components/ui/field.tsx"use client" import { useEffect, useMemo, useRef, useState } from "react" import { cva, type VariantProps } from "class-variance-authority" import { cn } from "cn" import { Label } from "@/components/ui/label" import { Separator } from "@/components/ui/separator" function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) { return ( <fieldset data-slot="field-set" className={cn( "flex flex-col gap-4 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3", className )} {...props} /> ) } function FieldLegend({ className, variant = "legend", ...props }: React.ComponentProps<"legend"> & { variant?: "legend" | "label" }) { return ( <legend data-slot="field-legend" data-variant={variant} className={cn( "mb-1.5 font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base", className )} {...props} /> ) } function FieldGroup({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="field-group" className={cn( "group/field-group @container/field-group flex w-full flex-col gap-5 data-[slot=checkbox-group]:gap-3 *:data-[slot=field-group]:gap-4", className )} {...props} /> ) } const fieldVariants = cva( "group/field flex w-full gap-2 data-[invalid=true]:text-(--color-danger-fg)", { variants: { orientation: { vertical: "flex-col *:w-full [&>.sr-only]:w-auto", horizontal: "flex-row items-center has-[>[data-slot=field-content]]:items-start *:data-[slot=field-label]:flex-auto has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px", responsive: "flex-col *:w-full @md/field-group:flex-row @md/field-group:items-center @md/field-group:*:w-auto @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:*:data-[slot=field-label]:flex-auto [&>.sr-only]:w-auto @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px", }, }, defaultVariants: { orientation: "vertical", }, } ) function Field({ className, orientation = "vertical", ...props }: React.ComponentProps<"div"> & VariantProps<typeof fieldVariants>) { return ( <div role="group" data-slot="field" data-orientation={orientation} className={cn(fieldVariants({ orientation }), className)} {...props} /> ) } function FieldContent({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="field-content" className={cn( "group/field-content flex flex-1 flex-col gap-0.5 leading-snug", className )} {...props} /> ) } function FieldLabel({ className, ...props }: React.ComponentProps<typeof Label>) { return ( <Label data-slot="field-label" className={cn( "group/field-label peer/field-label flex w-fit gap-2 leading-snug group-data-[disabled=true]/field:text-(--field-label-disabled) has-data-checked:border-primary/30 has-data-checked:bg-primary/5 has-[>[data-slot=field]]:rounded-lg has-[>[data-slot=field]]:border has-[>[data-slot=field]]:not-has-[:disabled,[data-disabled]]:hover:bg-(--field-choice-container-hover) has-[>[data-slot=field]]:has-[:focus-visible]:border-ring has-[>[data-slot=field]]:has-[:focus-visible]:ring-3 has-[>[data-slot=field]]:has-[:focus-visible]:ring-ring/50 *:data-[slot=field]:p-2.5 dark:has-data-checked:border-primary/20 dark:has-data-checked:bg-primary/10", "has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col", className )} {...props} /> ) } function FieldTitle({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="field-label" className={cn( "flex w-fit items-center gap-2 text-sm font-medium group-data-[disabled=true]/field:text-(--field-label-disabled)", className )} {...props} /> ) } function FieldDescription({ className, ...props }: React.ComponentProps<"p">) { return ( <p data-slot="field-description" className={cn( "text-left text-sm leading-normal font-normal text-muted-foreground group-has-data-horizontal/field:text-balance [[data-variant=legend]+&]:-mt-1.5", "last:mt-0 nth-last-2:-mt-1", "[&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary", className )} {...props} /> ) } function FieldSeparator({ children, className, ...props }: React.ComponentProps<"div"> & { children?: React.ReactNode }) { return ( <div data-slot="field-separator" data-content={!!children} className={cn( "relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2", className )} {...props} > <Separator className="absolute inset-0 top-1/2" /> {children && ( <span className="relative mx-auto block w-fit bg-background px-2 text-muted-foreground" data-slot="field-separator-content" > {children} </span> )} </div> ) } function FieldError({ className, children, errors, ...props }: React.ComponentProps<"div"> & { errors?: Array<{ message?: string } | undefined> }) { const hadContent = useRef(false) const motion = useRef<string | undefined>(undefined) const [message, setMessage] = useState<React.ReactNode>(null) const [phase, setPhase] = useState<"show" | "leave" | "reward">("show") const content = useMemo(() => { if (children) { return children } if (!errors?.length) { return null } const uniqueErrors = [ ...new Map(errors.map((error) => [error?.message, error])).values(), ] if (uniqueErrors?.length == 1) { return uniqueErrors[0]?.message } return ( <ul className="ml-4 flex list-disc flex-col gap-1"> {uniqueErrors.map( (error, index) => error?.message && <li key={index}>{error.message}</li> )} </ul> ) }, [children, errors]) if (content && content !== message) setMessage(content) function remember(node: HTMLDivElement | null) { if (!node) return motion.current = node.closest<HTMLElement>("[data-motion]")?.dataset.motion } useEffect(() => { if (content) { hadContent.current = true queueMicrotask(() => setPhase("show")) return } if (!hadContent.current) return hadContent.current = false const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches if (reduced || motion.current === "still") return queueMicrotask(() => setPhase("leave")) }, [content]) if (!content && phase === "reward") { return ( <div data-slot="field-fixed" className="field-fixed inline-flex items-center gap-1 text-sm text-muted-foreground" onAnimationEnd={(event) => { if (event.animationName !== "isotope-reward-fade") return setPhase("show") }} > <span aria-hidden className="size-icon-sm rounded-full bg-primary" /> Fixed </div> ) } if (!content && phase === "leave") { return ( <div role="alert" data-slot="field-error" ref={remember} className={cn("field-leave text-sm font-normal text-(--color-danger-fg)", className)} onAnimationEnd={() => setPhase(motion.current === "calm" ? "show" : "reward")} {...props} > {message} </div> ) } if (!content) return null return ( <div role="alert" data-slot="field-error" ref={remember} className={cn( "isotope-rise text-sm font-normal text-(--color-danger-fg)", className )} {...props} > {content} </div> ) } export { Field, FieldLabel, FieldDescription, FieldError, FieldGroup, FieldLegend, FieldSeparator, FieldSet, FieldContent, FieldTitle, }components/ui/input.tsxinput
components/ui/input.tsx"use client" import * as React from "react" import { Input as InputPrimitive } from "@base-ui/react/input" import { cn } from "cn" function Input({ className, type, placeholder, onChange, ...props }: React.ComponentProps<"input">) { const inset = className?.includes("px-0") const [filled, setFilled] = React.useState(Boolean(props.value ?? props.defaultValue)) const [leaving, setLeaving] = React.useState(false) return ( <span className={cn("relative flex min-w-0 flex-1", inset && "contents")}> <InputPrimitive type={type} data-slot="input" placeholder={placeholder} className={cn( !inset && "placeholder:text-transparent", "h-(--field-height-md) w-full min-w-0 rounded-(--field-corner) border-solid border-(length:--field-stroke-width) border-(--field-stroke-color) bg-(--field-container) hover:bg-(--field-container-hover) px-(--field-padding-x-text) py-1 text-base text-(--field-label) shadow-(--field-elevation) outline-none", "transition-[box-shadow,background-color] duration-(--duration-medium) ease-(--ease-bounce-soft)", "file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-ink-subtle", "focus-visible:bg-(--field-container-focus) focus-visible:shadow-(--field-focus-shadow)", "disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-(--field-container-disabled) disabled:text-(--field-label-disabled) disabled:shadow-none", "aria-invalid:animate-[isotope-nudge_var(--duration-fast)_var(--ease-out)] aria-invalid:shadow-(--focus-ring-invalid) md:text-sm", className )} {...props} onChange={(event) => { const next = event.target.value.length > 0 const level = event.target.closest<HTMLElement>("[data-motion]")?.dataset.motion const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches const quiet = reduced || level === "calm" || level === "still" if (!filled && next && !quiet) setLeaving(true) if (!next || quiet) setLeaving(false) setFilled(next) onChange?.(event) }} /> {placeholder && !inset && (!filled || leaving) ? ( <span aria-hidden className={cn( "pointer-events-none absolute inset-y-0 left-0 flex items-center px-4 text-sm text-ink-subtle", leaving && "input-placeholder-out" )} onAnimationEnd={() => setLeaving(false)} > {placeholder} </span> ) : null} </span> ) } export { Input }components/ui/textarea.tsxtextarea
components/ui/textarea.tsx"use client" import * as React from "react" import { cn } from "cn" function Textarea({ className, maxRows = 8, onInput, ...props }: React.ComponentProps<"textarea"> & { maxRows?: number }) { function resize(element: HTMLTextAreaElement) { const motion = element.closest<HTMLElement>("[data-motion]")?.dataset.motion const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches const instant = reduced || motion === "still" const calm = motion === "calm" element.style.height = "auto" const style = getComputedStyle(element) const line = Number.parseFloat(style.lineHeight) || 20 const padding = Number.parseFloat(style.paddingTop) + Number.parseFloat(style.paddingBottom) const next = Math.min(element.scrollHeight, line * maxRows + padding) if (instant) element.style.transition = "none" element.style.height = `${calm || instant ? next : next + 1}px` if (!calm && !instant) { requestAnimationFrame(() => { element.style.height = `${next}px` }) } } return ( <textarea data-slot="textarea" className={cn( "min-h-[calc(var(--size-control-md)*2)] w-full resize-none overflow-y-auto rounded-(--field-corner-multiline) border-solid border-(length:--field-stroke-width) border-(--field-stroke-color) bg-(--field-container) hover:bg-(--field-container-hover) px-(--field-padding-x) py-(--field-padding-y) text-sm text-(--field-label) shadow-(--field-elevation) outline-none transition-[height,background-color,box-shadow] duration-(--duration-fast) ease-(--ease-sheet) placeholder:text-(--field-placeholder) focus-visible:bg-(--field-container-focus) focus-visible:shadow-(--field-focus-shadow) disabled:cursor-not-allowed disabled:bg-(--field-container-disabled) disabled:text-(--field-label-disabled) disabled:shadow-none aria-invalid:animate-[isotope-nudge_var(--duration-fast)_var(--ease-out)] aria-invalid:shadow-(--focus-ring-invalid)", className )} {...props} onInput={(event) => { resize(event.currentTarget) onInput?.(event) }} /> ) } export { Textarea }components/ui/input-group.tsxinput-group
components/ui/input-group.tsx"use client" import * as React from "react" import { LoaderCircleIcon, SearchIcon, XIcon } from "lucide-react" import { cn } from "cn" import { tokenMs } from "@/lib/motion/token-ms" import { Input } from "@/components/ui/input" function InputGroup({ className, children, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="input-group" className={cn( "flex h-(--field-height-md) items-center gap-inline-sm rounded-(--field-corner) bg-(--field-container) pr-2 ps-(--field-padding-x) shadow-(--field-elevation) transition-[background-color,box-shadow] duration-(--duration-fast) ease-(--ease-out) focus-within:bg-(--field-container-focus) focus-within:shadow-(--field-focus-shadow)", className )} {...props} > {children} </div> ) } function InputGroupInput({ className, ...props }: React.ComponentProps<typeof Input>) { return ( <Input className={cn( "h-auto flex-1 bg-transparent px-0 shadow-none focus-visible:bg-transparent focus-visible:shadow-none", className )} {...props} /> ) } function InputGroupSearch({ className, defaultValue, loading = false, onChange, ...props }: React.ComponentProps<typeof Input> & { loading?: boolean }) { const inputRef = React.useRef<HTMLInputElement>(null) const pendingTimer = React.useRef(0) const [value, setValue] = React.useState(String(defaultValue ?? "")) const [pending, setPending] = React.useState(false) const busy = loading || pending const mode = busy ? "loading" : value.length > 0 ? "clear" : "search" return ( <InputGroup className={className}> <InputGroupInput ref={inputRef} {...props} value={value} onChange={(event) => { const next = event.target.value setValue(next) onChange?.(event) if (loading || next.length === 0) { window.clearTimeout(pendingTimer.current) setPending(false) return } setPending(true) window.clearTimeout(pendingTimer.current) pendingTimer.current = window.setTimeout(() => setPending(false), tokenMs("--duration-medium")) }} /> <span className="relative size-icon-md shrink-0"> <SearchIcon aria-hidden className={cn( "icon-morph pointer-events-none absolute inset-0 m-auto size-icon-sm text-muted-foreground", mode === "search" ? "scale-100 opacity-100" : "scale-50 opacity-0" )} /> <LoaderCircleIcon aria-hidden className={cn( "icon-morph icon-spin pointer-events-none absolute inset-0 m-auto size-icon-sm text-muted-foreground", mode === "loading" ? "scale-100 opacity-100" : "scale-50 opacity-0" )} /> <button type="button" aria-label="Clear" tabIndex={mode === "clear" ? 0 : -1} className={cn( "hit-target icon-morph absolute inset-0 inline-flex items-center justify-center rounded-full text-muted-foreground outline-none focus-visible:shadow-(--focus-ring)", mode === "clear" ? "scale-100 opacity-100" : "pointer-events-none scale-50 opacity-0" )} onClick={() => { setValue("") setPending(false) inputRef.current?.focus() }} > <XIcon className="size-icon-sm" /> </button> </span> </InputGroup> ) } export { InputGroup, InputGroupInput, InputGroupSearch }
Imports
What the examples on this page import:
import { Field, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
import { InputGroupSearch } from "@/components/ui/input-group"
import { Textarea } from "@/components/ui/textarea"Anatomy
- 1Label · FieldLabel, linked with htmlFor.
- 2Container · --field-container (sunken), --field-corner, --field-height-*; lifts to --field-container-focus.
- 3Input text and placeholder · --field-label and --field-placeholder.
- 4Leading / trailing slots · Icons or buttons inside an Input group.
- 5Help text · FieldDescription.
- 6Error · FieldError rises in under the control.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
Field orientation | "vertical" | "horizontal" | "vertical" | Label above or beside the control. |
Input aria-invalid | boolean | — | Shows the error halo and links FieldError. |
InputGroupSearch loading | boolean | — | Shows the spinner in the trailing slot. |
Textarea | textarea props | — | Auto-grows between 2 lines and its max height. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--field-height-sm | var(--size-control-sm) | 32px | same |
--field-height-md | var(--size-control-md) | 40px | same |
--field-height-lg | var(--size-control-lg) | 48px | same |
--field-padding-x | var(--space-inset-control-x) | 12px | same |
--field-padding-x-text | var(--space-inset-control-x-lg) | 16px | same |
--field-padding-y | var(--space-inset-control-y) | 8px | same |
--field-corner | var(--shape-full) | 999px | same |
--field-corner-multiline | var(--shape-md) | 14px | same |
--field-container | var(--surface-container) | oklch(0.965 0.005 95) | oklch(0.12 0 0) |
--field-container-focus | var(--surface-container-lowest) | oklch(1 0 0) | oklch(0.14 0 0) |
--field-label | var(--on-surface) | oklch(0.22 0.01 260) | oklch(1 0 0) |
--field-container-hover | color-mix(in oklch, var(--field-container), var(--field-label) var(--state-hover-mix)) | color-mix(in oklch, var(--field-container), var(--field-label) var(--state-hover-mix)) | same |
--field-container-disabled | var(--color-bg-disabled) | color-mix(in oklch, var(--color-fg-default) var(--state-disabled-container-mix), transparent) | same |
--field-label-disabled | var(--color-fg-disabled) | oklch(0.62 0.01 95) | oklch(0.53 0 0) |
--field-placeholder | var(--on-surface-variant) | oklch(0.45 0.01 95) | oklch(0.68 0 0) |
--field-stroke-width | var(--stroke-width-control) | 0px | same |
--field-stroke-color | var(--stroke-color-default) | oklch(0.22 0.01 260 / 18%) | oklch(1 0 0 / 12%) |
--field-elevation | var(--elevation-1) | 0 1px 1px -0.5px oklch(0.22 0.01 260 / 7%), 0 2px 3px -1px oklch(0.22 0.01 260 / 10%), 0 4px 8px -4px oklch(0.22 0.01 260 / 6%) | 0 1px 2px oklch(0 0 0 / 35%), 0 2px 10px oklch(0 0 0 / 25%) |
--field-focus-halo-width | var(--state-field-stroke) | 3px | same |
--field-focus-shadow | var(--focus-field) | 0 0 0 var(--state-field-stroke) color-mix(in oklch, var(--color-fg-default) var(--state-field-stroke-mix), transparent), var(--elevation-3) | same |
--field-error-shadow | var(--focus-ring-invalid) | 0 0 0 var(--state-focus-ring-offset) var(--focus-ring-gap, var(--color-bg-canvas)), 0 0 0 calc(var(--state-focus-ring-offset) + var(--state-focus-ring-width)) var(--color-danger) | same |
--field-label-gap | var(--space-1) | 4px | same |
--field-action-label | var(--field-placeholder) | oklch(0.45 0.01 95) | oklch(0.68 0 0) |
--field-action-label-hover | var(--field-label) | oklch(0.22 0.01 260) | oklch(1 0 0) |
--field-action-container-hover | var(--color-bg-hover) | oklch(0.965 0.005 95) | oklch(0.22 0 0) |
--field-action-container-pressed | var(--color-bg-pressed) | oklch(0.94 0.007 95) | oklch(0.27 0 0) |
--field-choice-container-hover | color-mix(in oklch, var(--field-container), var(--field-label) var(--state-hover-mix)) | color-mix(in oklch, var(--field-container), var(--field-label) var(--state-hover-mix)) | same |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Focus | Container lifts to the page surface; a soft halo and shadow bloom in. | --duration-fast, --field-focus-shadow |
| First letter | Placeholder slides aside. | --duration-medium |
| Invalid | The field nudges once; the error rises in. | --duration-fast |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Pair every control with a visible Label (htmlFor) or aria-label.
- Errors use aria-invalid and FieldError; help text is linked with aria-describedby.
- Focus shows the field halo (--field-focus-shadow); keyboard users also get the focus ring on buttons inside.
Usage
Do
- Keep labels short and visible.
- Explain errors in plain words, next to the field.
Don’t
- Don't use the placeholder as the label.
Code
"use client"
import * as React from "react"
import { Input as InputPrimitive } from "@base-ui/react/input"
import { cn } from "cn"
function Input({
className,
type,
placeholder,
onChange,
...props
}: React.ComponentProps<"input">) {
const inset = className?.includes("px-0")
const [filled, setFilled] = React.useState(Boolean(props.value ?? props.defaultValue))
const [leaving, setLeaving] = React.useState(false)
return (
<span className={cn("relative flex min-w-0 flex-1", inset && "contents")}>
<InputPrimitive
type={type}
data-slot="input"
placeholder={placeholder}
className={cn(
!inset && "placeholder:text-transparent",
"h-(--field-height-md) w-full min-w-0 rounded-(--field-corner) border-solid border-(length:--field-stroke-width) border-(--field-stroke-color) bg-(--field-container) hover:bg-(--field-container-hover) px-(--field-padding-x-text) py-1 text-base text-(--field-label) shadow-(--field-elevation) outline-none",
"transition-[box-shadow,background-color] duration-(--duration-medium) ease-(--ease-bounce-soft)",
"file:inline-flex file:h-6 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-ink-subtle",
"focus-visible:bg-(--field-container-focus) focus-visible:shadow-(--field-focus-shadow)",
"disabled:pointer-events-none disabled:cursor-not-allowed disabled:bg-(--field-container-disabled) disabled:text-(--field-label-disabled) disabled:shadow-none",
"aria-invalid:animate-[isotope-nudge_var(--duration-fast)_var(--ease-out)] aria-invalid:shadow-(--focus-ring-invalid) md:text-sm",
className
)}
{...props}
onChange={(event) => {
const next = event.target.value.length > 0
const level = event.target.closest<HTMLElement>("[data-motion]")?.dataset.motion
const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches
const quiet = reduced || level === "calm" || level === "still"
if (!filled && next && !quiet) setLeaving(true)
if (!next || quiet) setLeaving(false)
setFilled(next)
onChange?.(event)
}}
/>
{placeholder && !inset && (!filled || leaving) ? (
<span
aria-hidden
className={cn(
"pointer-events-none absolute inset-y-0 left-0 flex items-center px-4 text-sm text-ink-subtle",
leaving && "input-placeholder-out"
)}
onAnimationEnd={() => setLeaving(false)}
>
{placeholder}
</span>
) : null}
</span>
)
}
export { Input }
"use client"
import * as React from "react"
import { cn } from "cn"
function Textarea({
className,
maxRows = 8,
onInput,
...props
}: React.ComponentProps<"textarea"> & { maxRows?: number }) {
function resize(element: HTMLTextAreaElement) {
const motion = element.closest<HTMLElement>("[data-motion]")?.dataset.motion
const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches
const instant = reduced || motion === "still"
const calm = motion === "calm"
element.style.height = "auto"
const style = getComputedStyle(element)
const line = Number.parseFloat(style.lineHeight) || 20
const padding = Number.parseFloat(style.paddingTop) + Number.parseFloat(style.paddingBottom)
const next = Math.min(element.scrollHeight, line * maxRows + padding)
if (instant) element.style.transition = "none"
element.style.height = `${calm || instant ? next : next + 1}px`
if (!calm && !instant) {
requestAnimationFrame(() => {
element.style.height = `${next}px`
})
}
}
return (
<textarea
data-slot="textarea"
className={cn(
"min-h-[calc(var(--size-control-md)*2)] w-full resize-none overflow-y-auto rounded-(--field-corner-multiline) border-solid border-(length:--field-stroke-width) border-(--field-stroke-color) bg-(--field-container) hover:bg-(--field-container-hover) px-(--field-padding-x) py-(--field-padding-y) text-sm text-(--field-label) shadow-(--field-elevation) outline-none transition-[height,background-color,box-shadow] duration-(--duration-fast) ease-(--ease-sheet) placeholder:text-(--field-placeholder) focus-visible:bg-(--field-container-focus) focus-visible:shadow-(--field-focus-shadow) disabled:cursor-not-allowed disabled:bg-(--field-container-disabled) disabled:text-(--field-label-disabled) disabled:shadow-none aria-invalid:animate-[isotope-nudge_var(--duration-fast)_var(--ease-out)] aria-invalid:shadow-(--focus-ring-invalid)",
className
)}
{...props}
onInput={(event) => {
resize(event.currentTarget)
onInput?.(event)
}}
/>
)
}
export { Textarea }
"use client"
import { useEffect, useMemo, useRef, useState } from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
import { Label } from "@/components/ui/label"
import { Separator } from "@/components/ui/separator"
function FieldSet({ className, ...props }: React.ComponentProps<"fieldset">) {
return (
<fieldset
data-slot="field-set"
className={cn(
"flex flex-col gap-4 has-[>[data-slot=checkbox-group]]:gap-3 has-[>[data-slot=radio-group]]:gap-3",
className
)}
{...props}
/>
)
}
function FieldLegend({
className,
variant = "legend",
...props
}: React.ComponentProps<"legend"> & { variant?: "legend" | "label" }) {
return (
<legend
data-slot="field-legend"
data-variant={variant}
className={cn(
"mb-1.5 font-medium data-[variant=label]:text-sm data-[variant=legend]:text-base",
className
)}
{...props}
/>
)
}
function FieldGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="field-group"
className={cn(
"group/field-group @container/field-group flex w-full flex-col gap-5 data-[slot=checkbox-group]:gap-3 *:data-[slot=field-group]:gap-4",
className
)}
{...props}
/>
)
}
const fieldVariants = cva(
"group/field flex w-full gap-2 data-[invalid=true]:text-(--color-danger-fg)",
{
variants: {
orientation: {
vertical: "flex-col *:w-full [&>.sr-only]:w-auto",
horizontal:
"flex-row items-center has-[>[data-slot=field-content]]:items-start *:data-[slot=field-label]:flex-auto has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
responsive:
"flex-col *:w-full @md/field-group:flex-row @md/field-group:items-center @md/field-group:*:w-auto @md/field-group:has-[>[data-slot=field-content]]:items-start @md/field-group:*:data-[slot=field-label]:flex-auto [&>.sr-only]:w-auto @md/field-group:has-[>[data-slot=field-content]]:[&>[role=checkbox],[role=radio]]:mt-px",
},
},
defaultVariants: {
orientation: "vertical",
},
}
)
function Field({
className,
orientation = "vertical",
...props
}: React.ComponentProps<"div"> & VariantProps<typeof fieldVariants>) {
return (
<div
role="group"
data-slot="field"
data-orientation={orientation}
className={cn(fieldVariants({ orientation }), className)}
{...props}
/>
)
}
function FieldContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="field-content"
className={cn(
"group/field-content flex flex-1 flex-col gap-0.5 leading-snug",
className
)}
{...props}
/>
)
}
function FieldLabel({
className,
...props
}: React.ComponentProps<typeof Label>) {
return (
<Label
data-slot="field-label"
className={cn(
"group/field-label peer/field-label flex w-fit gap-2 leading-snug group-data-[disabled=true]/field:text-(--field-label-disabled) has-data-checked:border-primary/30 has-data-checked:bg-primary/5 has-[>[data-slot=field]]:rounded-lg has-[>[data-slot=field]]:border has-[>[data-slot=field]]:not-has-[:disabled,[data-disabled]]:hover:bg-(--field-choice-container-hover) has-[>[data-slot=field]]:has-[:focus-visible]:border-ring has-[>[data-slot=field]]:has-[:focus-visible]:ring-3 has-[>[data-slot=field]]:has-[:focus-visible]:ring-ring/50 *:data-[slot=field]:p-2.5 dark:has-data-checked:border-primary/20 dark:has-data-checked:bg-primary/10",
"has-[>[data-slot=field]]:w-full has-[>[data-slot=field]]:flex-col",
className
)}
{...props}
/>
)
}
function FieldTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="field-label"
className={cn(
"flex w-fit items-center gap-2 text-sm font-medium group-data-[disabled=true]/field:text-(--field-label-disabled)",
className
)}
{...props}
/>
)
}
function FieldDescription({ className, ...props }: React.ComponentProps<"p">) {
return (
<p
data-slot="field-description"
className={cn(
"text-left text-sm leading-normal font-normal text-muted-foreground group-has-data-horizontal/field:text-balance [[data-variant=legend]+&]:-mt-1.5",
"last:mt-0 nth-last-2:-mt-1",
"[&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
className
)}
{...props}
/>
)
}
function FieldSeparator({
children,
className,
...props
}: React.ComponentProps<"div"> & {
children?: React.ReactNode
}) {
return (
<div
data-slot="field-separator"
data-content={!!children}
className={cn(
"relative -my-2 h-5 text-sm group-data-[variant=outline]/field-group:-mb-2",
className
)}
{...props}
>
<Separator className="absolute inset-0 top-1/2" />
{children && (
<span
className="relative mx-auto block w-fit bg-background px-2 text-muted-foreground"
data-slot="field-separator-content"
>
{children}
</span>
)}
</div>
)
}
function FieldError({
className,
children,
errors,
...props
}: React.ComponentProps<"div"> & {
errors?: Array<{ message?: string } | undefined>
}) {
const hadContent = useRef(false)
const motion = useRef<string | undefined>(undefined)
const [message, setMessage] = useState<React.ReactNode>(null)
const [phase, setPhase] = useState<"show" | "leave" | "reward">("show")
const content = useMemo(() => {
if (children) {
return children
}
if (!errors?.length) {
return null
}
const uniqueErrors = [
...new Map(errors.map((error) => [error?.message, error])).values(),
]
if (uniqueErrors?.length == 1) {
return uniqueErrors[0]?.message
}
return (
<ul className="ml-4 flex list-disc flex-col gap-1">
{uniqueErrors.map(
(error, index) =>
error?.message && <li key={index}>{error.message}</li>
)}
</ul>
)
}, [children, errors])
if (content && content !== message) setMessage(content)
function remember(node: HTMLDivElement | null) {
if (!node) return
motion.current = node.closest<HTMLElement>("[data-motion]")?.dataset.motion
}
useEffect(() => {
if (content) {
hadContent.current = true
queueMicrotask(() => setPhase("show"))
return
}
if (!hadContent.current) return
hadContent.current = false
const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches
if (reduced || motion.current === "still") return
queueMicrotask(() => setPhase("leave"))
}, [content])
if (!content && phase === "reward") {
return (
<div
data-slot="field-fixed"
className="field-fixed inline-flex items-center gap-1 text-sm text-muted-foreground"
onAnimationEnd={(event) => {
if (event.animationName !== "isotope-reward-fade") return
setPhase("show")
}}
>
<span aria-hidden className="size-icon-sm rounded-full bg-primary" />
Fixed
</div>
)
}
if (!content && phase === "leave") {
return (
<div
role="alert"
data-slot="field-error"
ref={remember}
className={cn("field-leave text-sm font-normal text-(--color-danger-fg)", className)}
onAnimationEnd={() => setPhase(motion.current === "calm" ? "show" : "reward")}
{...props}
>
{message}
</div>
)
}
if (!content) return null
return (
<div
role="alert"
data-slot="field-error"
ref={remember}
className={cn(
"isotope-rise text-sm font-normal text-(--color-danger-fg)",
className
)}
{...props}
>
{content}
</div>
)
}
export {
Field,
FieldLabel,
FieldDescription,
FieldError,
FieldGroup,
FieldLegend,
FieldSeparator,
FieldSet,
FieldContent,
FieldTitle,
}
"use client"
import * as React from "react"
import { LoaderCircleIcon, SearchIcon, XIcon } from "lucide-react"
import { cn } from "cn"
import { tokenMs } from "@/lib/motion/token-ms"
import { Input } from "@/components/ui/input"
function InputGroup({ className, children, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="input-group"
className={cn(
"flex h-(--field-height-md) items-center gap-inline-sm rounded-(--field-corner) bg-(--field-container) pr-2 ps-(--field-padding-x) shadow-(--field-elevation) transition-[background-color,box-shadow] duration-(--duration-fast) ease-(--ease-out) focus-within:bg-(--field-container-focus) focus-within:shadow-(--field-focus-shadow)",
className
)}
{...props}
>
{children}
</div>
)
}
function InputGroupInput({ className, ...props }: React.ComponentProps<typeof Input>) {
return (
<Input
className={cn(
"h-auto flex-1 bg-transparent px-0 shadow-none focus-visible:bg-transparent focus-visible:shadow-none",
className
)}
{...props}
/>
)
}
function InputGroupSearch({
className,
defaultValue,
loading = false,
onChange,
...props
}: React.ComponentProps<typeof Input> & { loading?: boolean }) {
const inputRef = React.useRef<HTMLInputElement>(null)
const pendingTimer = React.useRef(0)
const [value, setValue] = React.useState(String(defaultValue ?? ""))
const [pending, setPending] = React.useState(false)
const busy = loading || pending
const mode = busy ? "loading" : value.length > 0 ? "clear" : "search"
return (
<InputGroup className={className}>
<InputGroupInput
ref={inputRef}
{...props}
value={value}
onChange={(event) => {
const next = event.target.value
setValue(next)
onChange?.(event)
if (loading || next.length === 0) {
window.clearTimeout(pendingTimer.current)
setPending(false)
return
}
setPending(true)
window.clearTimeout(pendingTimer.current)
pendingTimer.current = window.setTimeout(() => setPending(false), tokenMs("--duration-medium"))
}}
/>
<span className="relative size-icon-md shrink-0">
<SearchIcon
aria-hidden
className={cn(
"icon-morph pointer-events-none absolute inset-0 m-auto size-icon-sm text-muted-foreground",
mode === "search" ? "scale-100 opacity-100" : "scale-50 opacity-0"
)}
/>
<LoaderCircleIcon
aria-hidden
className={cn(
"icon-morph icon-spin pointer-events-none absolute inset-0 m-auto size-icon-sm text-muted-foreground",
mode === "loading" ? "scale-100 opacity-100" : "scale-50 opacity-0"
)}
/>
<button
type="button"
aria-label="Clear"
tabIndex={mode === "clear" ? 0 : -1}
className={cn(
"hit-target icon-morph absolute inset-0 inline-flex items-center justify-center rounded-full text-muted-foreground outline-none focus-visible:shadow-(--focus-ring)",
mode === "clear" ? "scale-100 opacity-100" : "pointer-events-none scale-50 opacity-0"
)}
onClick={() => {
setValue("")
setPending(false)
inputRef.current?.focus()
}}
>
<XIcon className="size-icon-sm" />
</button>
</span>
</InputGroup>
)
}
export { InputGroup, InputGroupInput, InputGroupSearch }