Skip to content
Facade UI
uiatom

Input

A text field built on Base UI's Field, which connects the label, help text and error message to the input. The border has a contrast of at least 3:1.

Open in v0 (opens in a new tab)

Preview

Open full width (opens in a new tab)
Preview width

Installation

Package manager
pnpm dlx shadcn@latest add @facade/input

This component needs @base-ui/react@^1.9.0, utils. The CLI installs them for you.

Usage

This is the code for the preview above. To install it as a file, run npx shadcn@latest add @facade/input-demo.

demos/input.tsx
import { Button } from "@registry/ui/button"
import { Input } from "@registry/ui/input"

export function Demo() {
  return (
    <div className="flex max-w-md flex-col gap-6">
      <Input
        label="Work email"
        type="email"
        autoComplete="email"
        description="We send the receipt here."
        required
      />
      <Input
        label="Email address"
        hideLabel
        type="email"
        placeholder="you@example.com"
        trailing={
          <Button size="sm" className="h-9">
            Subscribe
          </Button>
        }
      />
      <Input
        label="Company"
        defaultValue="Northwind"
        error="That company already has an account."
      />
    </div>
  )
}

Source

These are the files the CLI copies into your project.

components/ui/input.tsx
/**
 * Input — a text field built on Base UI's Field, so the label, description and
 * error are wired to the control for you.
 *
 * Base UI's `Field` generates the ids and the `aria-labelledby`,
 * `aria-describedby` and `aria-invalid` links between the parts. Doing that by
 * hand is where accessible forms usually go wrong — a visually associated label
 * that is not programmatically associated is invisible to assistive tech.
 *
 * a11y notes worth keeping:
 *
 *  - The border uses `--input`, which the token layer holds at 3:1 against the
 *    background (WCAG 1.4.11). It is noticeably darker than `--border`, because
 *    the edge is the only thing identifying the control as a field.
 *  - A required field is marked with the word "required" in the label, not with
 *    a bare asterisk whose meaning is a convention rather than information.
 *  - The error is rendered by `Field.Error`, which Base UI associates with the
 *    control, so it is announced rather than merely appearing underneath.
 *  - `Field.Root invalid` is set when `error` is passed in. Base UI otherwise
 *    derives validity from the control's own `ValidityState`, which knows
 *    nothing about an error that came back from a server — so without it, an
 *    external message is described but the field is neither marked
 *    `aria-invalid` nor given the red border.
 *  - 44px tall, matching every other control in the registry.
 *
 * `label` is required. Pass `hideLabel` for a search or subscribe field where
 * the design calls for a placeholder instead — the label stays in the
 * accessibility tree, which a placeholder alone never does.
 *
 * Dependencies: @base-ui/react, react, @/lib/utils.
 */

import { Field } from "@base-ui/react/field"
import { Input as BaseInput } from "@base-ui/react/input"
import type { ComponentPropsWithoutRef, ReactNode } from "react"

import { cn } from "@/lib/utils"

export interface InputProps extends Omit<ComponentPropsWithoutRef<"input">, "size"> {
  /** Required. Use `hideLabel` if it should not be visible. */
  label: ReactNode
  /** Hides the label visually but keeps it for screen readers. */
  hideLabel?: boolean
  /** Help text below the field. It is linked to the field for you. */
  description?: ReactNode
  /** Error message. Base UI links it to the field and announces it. */
  error?: ReactNode
  /** Shown inside the field at its end, such as a button or a unit. */
  trailing?: ReactNode
  /** Classes for the outer Field.Root. */
  className?: string
  /** Classes for the `<input>` itself. */
  inputClassName?: string
}

export function Input({
  label,
  hideLabel = false,
  description,
  error,
  trailing,
  className,
  inputClassName,
  name,
  required,
  ...props
}: InputProps) {
  return (
    <Field.Root
      name={name}
      invalid={Boolean(error)}
      className={cn("flex w-full flex-col gap-1.5", className)}
    >
      <Field.Label
        className={cn("text-foreground text-sm font-medium", hideLabel && "sr-only")}
      >
        {label}
        {/* Spelled out, because an asterisk is a convention, not information. */}
        {required ? <span className="text-muted-foreground"> (required)</span> : null}
      </Field.Label>

      <div className="relative flex w-full items-center">
        <BaseInput
          name={name}
          required={required}
          className={cn(
            "border-input bg-background text-foreground placeholder:text-muted-foreground",
            "h-11 w-full rounded-md border px-3.5 text-base",
            "duration-facade-fast ease-facade-out transition-[border-color,box-shadow]",
            "focus-visible:ring-ring focus-visible:border-ring outline-none focus-visible:ring-2",
            "disabled:cursor-not-allowed disabled:opacity-60",
            "data-[invalid]:border-destructive data-[invalid]:focus-visible:ring-destructive",
            trailing && "pr-28",
            inputClassName,
          )}
          {...props}
        />
        {trailing ? (
          <span className="absolute inset-y-1 right-1 flex items-center">{trailing}</span>
        ) : null}
      </div>

      {description ? (
        <Field.Description className="text-muted-foreground text-pretty text-sm">
          {description}
        </Field.Description>
      ) : null}

      {/* `match` shows an error from outside, which Base UI's own validity
          state knows nothing about. Without one, no children are passed, so
          Base UI shows the browser's own message for a failed check. */}
      <Field.Error
        className="text-destructive text-pretty text-sm"
        match={Boolean(error) || undefined}
        {...(error ? { children: error } : {})}
      />
    </Field.Root>
  )
}

Props

InputProps

Extends Omit<ComponentPropsWithoutRef<"input">, "size">.

Props for InputProps
PropTypeDefault
label*RequiredRequired. Use `hideLabel` if it should not be visible.ReactNode—
hideLabelHides the label visually but keeps it for screen readers.booleanfalse
descriptionHelp text below the field. It is linked to the field for you.ReactNode—
errorError message. Base UI links it to the field and announces it.ReactNode—
trailingShown inside the field at its end, such as a button or a unit.ReactNode—
classNameClasses for the outer Field.Root.string—
inputClassNameClasses for the `<input>` itself.string—