Skip to the content

Controls and surfaces

input

A single-line text input in three sizes, with an invalid state.

Controls

When to use it

Short free text: a name, an email, a number. Wrap it in field for a label and an error; use textarea for several lines, select for a fixed list, input-group to add an icon or a button inside the edge.

See it working

The demonstration the browser checks drive, with the keyboard as well as the pointer. More of it is on the Controls page.

Button

Field, input, textarea

Used for sign-in only.

Select, slider, choices

Add it

npx shadcn@latest add @rivier-ui/input

The registry is named once in components.json: see Installation.

Example

Import it from @/components/ui/input.

<Input type="email" placeholder="you@example.com" />
<Input size="sm" defaultValue="Rivier" />
<Input aria-invalid="true" aria-label="Email" defaultValue="not-an-email" />

What it needs

Plain markup: it can stay a server component. Packages: class-variance-authority.

It imports

It is used by

Source

The whole file. Once copied it is yours: the registry item holds the same text.

src/components/ui/input.tsx
/**
 * @name input
 * @category ui
 * @description A single-line text input in three sizes, with an invalid state.
 * @when Short free text: a name, an email, a number. Wrap it in field for a label and an error; use textarea for several lines, select for a fixed list, input-group to add an icon or a button inside the edge.
 * @example
 * <Input type="email" placeholder="you@example.com" />
 * <Input size="sm" defaultValue="Rivier" />
 * <Input aria-invalid="true" aria-label="Email" defaultValue="not-an-email" />
 */
import { cva, type VariantProps } from "class-variance-authority";
import type { ComponentProps } from "react";
import { cn } from "@/lib/cn";

export const inputVariants = cva(
  "w-full min-w-0 rounded-control border border-input bg-background text-sm text-foreground transition-[border-color,opacity] duration-instant ease-out placeholder:text-muted-foreground disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive motion-reduce:transition-none",
  {
    variants: {
      size: {
        sm: "h-control-sm px-2.5",
        md: "h-control px-3",
        lg: "h-control-lg px-4 text-base",
      },
    },
    defaultVariants: { size: "md" },
  },
);

/** `size` is the control's height (sm, md, lg), not the native character-width attribute. */
export type InputProps = Omit<ComponentProps<"input">, "size"> & VariantProps<typeof inputVariants>;

export function Input({ className, size, type = "text", ...props }: InputProps) {
  return (
    <input
      data-slot="input"
      type={type}
      className={cn(
        inputVariants({ size }),
        "file:me-3 file:h-full file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground",
        className,
      )}
      {...props}
    />
  );
}