Input

A single-line text field, with an optional clear button.

Put an input in a Field to give it a visible label and a description. The field wires the label and description to the input for you.

Preview
basic.tsx
import { Field, FieldDescription, FieldLabel } from "~/components/ui/field"
import { Input } from "~/components/ui/input"

export default function Example() {
  return (
    <Field className="w-full max-w-sm">
      <FieldLabel>Email</FieldLabel>
      <Input type="email" placeholder="ada@example.com" autoComplete="email" />
      <FieldDescription>We'll only use it to send receipts.</FieldDescription>
    </Field>
  )
}

Clearable

InputGroup holds an input with controls beside it. InputClear empties the input and only shows while it has a value.

Preview
clearable.tsx
import { Input, InputClear, InputGroup } from "~/components/ui/input"

export default function Example() {
  return (
    <InputGroup className="max-w-sm">
      <Input
        type="search"
        aria-label="Search"
        placeholder="Search projects"
        defaultValue="Roadmap"
      />
      <InputClear aria-label="Clear search" />
    </InputGroup>
  )
}

Dependencies

What it's built from and what's built from it in Mocha, read from the imports in its source.

UsesButtonprimitiveTextprimitive
Used bySelectform itemTextareaform item
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20lucide-react@1.73.0tailwind-variants@4.1.0

Installation

Set the project up for Mocha once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/mocha/input.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 lucide-react@1.73.0 tailwind-variants@4.1.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/input.tsx
import {
  Input as InputPrimitive,
  InputClear as InputClearPrimitive,
  InputGroup as InputGroupPrimitive,
} from "@ui-sdk/react/input"
import { X } from "lucide-react"
import type * as React from "react"
import { tv } from "tailwind-variants"

import { buttonVariants } from "~/components/ui/button"
import { textVariants } from "~/components/ui/text"
import { cnState } from "~/lib/utils"

/**
 * The outlined surface every text control shares: its border, fill, focus
 * ring, invalid and disabled states. Select's trigger builds on it, so the
 * system's fields look alike.
 */
const inputVariants = tv({
  base: [
    "rounded-md border border-input bg-background text-foreground",
    "placeholder:text-muted-foreground",
    "transition-[border-color,box-shadow] duration-150",
    "outline-none focus-visible:border-ring focus-visible:ring-4 focus-visible:ring-ring/25",
    "aria-invalid:border-destructive aria-invalid:focus-visible:ring-destructive/30",
    "data-disabled:cursor-not-allowed data-disabled:opacity-50",
  ],
})

const control = [
  inputVariants(),
  textVariants({ size: "sm" }),
  "h-9 w-full min-w-0 px-md",
  // No browser clear button on a search input: `InputClear` is the system's own.
  "[&::-webkit-search-cancel-button]:appearance-none",
  "file:border-0 file:bg-transparent",
]

/** A single-line text field. Put it in a `Field` to give it a label, a description and errors. */
function Input({ className, ...props }: React.ComponentProps<typeof InputPrimitive>) {
  return <InputPrimitive className={cnState(control.join(" "), className)} {...props} />
}

/** Holds an `Input` together with buttons or text beside it, like an `InputClear`. */
function InputGroup({ className, ...props }: React.ComponentProps<typeof InputGroupPrimitive>) {
  return (
    <InputGroupPrimitive
      className={cnState(
        "relative flex w-full items-center [&>[data-slot=input]]:pr-2xl",
        className,
      )}
      {...props}
    />
  )
}

/** Empties the input in its group. Shows only while the input has a value. */
function InputClear({
  className,
  children,
  ...props
}: React.ComponentProps<typeof InputClearPrimitive>) {
  return (
    <InputClearPrimitive
      className={cnState(
        [
          buttonVariants({ variant: "ghost", size: "icon-xs" }),
          "absolute right-1.5 text-muted-foreground",
        ],
        className,
      )}
      {...props}
    >
      {children ?? <X className="size-3.5" />}
    </InputClearPrimitive>
  )
}

export { Input, InputClear, InputGroup, inputVariants }

API reference

Generated from Mocha's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Input

A single-line text field. Put it in a Field to give it a label, a description and errors.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: InputState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: InputState) => string)

controller

Reads and writes the field from anywhere. Create one with createInputController().

Type { getValue: () => string; setValue: (value: string, options?: ChangeOptions<InputValueChangeReason>) => boolean; clear: (options?: ChangeOptions<...>) => boolean; focus: () => void; }

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultValue

The value it starts with. A form reset returns here.

Type string

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

onValueChange

Called when the user changes the value: typing ("input-change"), a Clear part ("clear-press"), a form reset ("reset") or a controller ("imperative"). details.cancel() keeps the value as it was.

Type (value: string, details: ChangeDetails<InputValueChangeReason>) => void

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<InputState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: InputState) => CSSProperties)

value

The value, for a controlled field.

Type string

InputClear

Empties the input in its group. Shows only while the input has a value.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<InputGroupState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<InputGroupState>) => string)

disabled

Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless focusableWhenDisabled is set.

Type boolean · Default false

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<InputGroupState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<InputGroupState>) => CSSProperties)

InputGroup

Holds an Input together with buttons or text beside it, like an InputClear.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: InputGroupState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: InputGroupState) => string)

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<InputGroupState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: InputGroupState) => CSSProperties)