# Input

> Input lets users enter text, numbers, and other data. A form element with support for various types, validation states, and styling options.

Source: https://vaneui.com/docs/form-components/input

Import: `import { Input, Button, Col, Kbd, Label, Row, ThemeProvider } from "@vaneui/ui"`

Input takes a single-line value, such as a name, an email address or a search query, and renders a native `<input>`. With `invalid` set, it adds `aria-invalid="true"` and a trailing alert icon that screen readers skip. Text that runs over several lines needs a [Textarea](/docs/form-components/textarea), and a [Field](/docs/form-components/field) around an Input links its label, help text and error message.

## Basic usage

Input renders a styled text field for entering text, numbers, and other data. It defaults to `sm`, `primary`, `outline`, and `rounded`, with `insetRing` and `focusVisible`. Input is `wFull` by default, so it spans its container. Constrain the width with a sized parent or `className="w-64"`.

```tsx demo
<Input placeholder="Enter text..." />
```

## Sizes

Inputs come in different sizes: `xs`, `sm` (default), `md`, `lg`, `xl`. At the same size an Input is as tall as a `Button` and has the same corner radius. Both default to `sm`, so a field and a button with no size prop line up. When you size one, give the other the same prop.

```tsx demo
<Row>
  <Input placeholder="you@company.com" />
  <Button filled>Subscribe</Button>
</Row>
```

```tsx demo
<Col>
  <Input xs placeholder="Extra small" />
  <Input placeholder="Small (default)" />
  <Input md placeholder="Medium" />
  <Input lg placeholder="Large" />
  <Input xl placeholder="Extra large" />
</Col>
```

## Variants

Inputs are `outline` by default. Use `filled` for solid backgrounds.

```tsx demo
<Col>
  <Row flexWrap>
    <Input placeholder="Outline (default)" />
    <Input success placeholder="Outline success" />
    <Input danger placeholder="Outline danger" />
  </Row>
  <Row flexWrap>
    <Input filled placeholder="Filled" />
    <Input filled success placeholder="Filled success" />
    <Input filled danger placeholder="Filled danger" />
  </Row>
</Col>
```

## Shapes

Inputs support border radius styles: `rounded` (default), `pill`, and `sharp`.

```tsx demo
<Row flexWrap>
  <Input placeholder="Rounded (default)" />
  <Input pill placeholder="Pill shape" />
  <Input sharp placeholder="Sharp corners" />
</Row>
```

## Types

The `type` HTML attribute passes through to the underlying `<input>` element.

```tsx demo
<Col>
  <Input type="text" placeholder="Text input" />
  <Input type="email" placeholder="Email input" />
  <Input type="password" placeholder="Password input" />
  <Input type="number" placeholder="Number input" />
  <Input type="search" placeholder="Search input" />
</Col>
```

## With labels

Pair inputs with labels for accessibility and better UX. When the `Input` is nested inside a `Label`, it inherits the Label's size. Set it once on the Label.

```tsx demo
<Col>
  <Label>
    Full Name
    <Input type="text" placeholder="Enter your full name" />
  </Label>
  <Label lg>
    Email Address (large)
    <Input type="email" placeholder="Enter your email" />
  </Label>
</Col>
```

## States

Different input states: disabled, readonly, and validation feedback. Use appearance props (`success`, `danger`) for visual feedback, or the `invalid` validity prop for form validation state. `invalid` layers a danger border and ring over the current appearance.

```tsx
<Input placeholder="Normal input" />
<Input disabled placeholder="Disabled input" />
<Input success placeholder="Success state" />
<Input danger placeholder="Error state" />
<Input invalid placeholder="Validation error" />
```

```tsx demo
<Col>
  <Input placeholder="Normal input" />
  <Input disabled placeholder="Disabled input" />
  <Input success placeholder="Success state" />
  <Input danger placeholder="Error state" />
  <Input invalid placeholder="Validation error (validity)" />
</Col>
```

Input renders a wrapper `<span>` around the `<input>`. While the input is valid and has no section, the wrapper is `display: contents`, so it adds no box, and `ref`, `className` and `style` go to the `<input>`. The `<input>` stays the same node when `invalid` changes, so the field keeps focus and its text while it is validated as the user types. Before `@vaneui/ui` 1.6.0 the wrapper was rendered only while the input was invalid. A selector that expects the `<input>` as a direct child of its parent, such as `.form > input`, does not match.

## Icons, prefixes and units inside the field

`startSection` and `endSection` put content inside the field, at its start and end: a search icon, a prefix such as `https://`, a unit, a keyboard hint or a button. The field reserves the measured width of each section, so a long prefix pushes the text as far as it needs, and the section takes the field's size. Sections need `@vaneui/ui` 1.8.0 or later.

```tsx demo
<Col className="max-w-sm">
  <Input aria-label="Search" placeholder="Search docs" startSection={
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} aria-hidden="true">
      <circle cx="11" cy="11" r="7"/>
      <path d="M20 20l-3.5-3.5"/>
    </svg>
  }/>
  <Input aria-label="Website" startSection="https://" placeholder="vaneui.com"/>
  <Input aria-label="Weight" type="number" defaultValue="72" endSection="kg"/>
  <Input lg aria-label="Command" placeholder="Run a command" endSection={<Kbd>⌘K</Kbd>}/>
</Col>
```

A section ignores the pointer, so a click on an icon or a unit focuses the field. A button or a link inside a section stays clickable. Give an icon-only button an `aria-label`, and `type="button"` inside a form:

```tsx demo
const [query, setQuery] = useState('');
const inputRef = useRef<HTMLInputElement>(null);

function clear() {
  setQuery('');
  inputRef.current?.focus();
}

return (
  <Input
    ref={inputRef}
    aria-label="Search"
    placeholder="Type to search"
    value={query}
    onChange={event => setQuery(event.target.value)}
    endSection={query ? (
      <Button type="button" xs ghost secondary onClick={clear}>Clear</Button>
    ) : null}
  />
);
```

The `<input>` stays the same node when a section appears or goes away, so the field keeps focus and its text. A button that removes itself, like Clear here, takes the focus with it, so its click handler moves focus back to the field. With a section, the wrapper becomes the box the sections sit in: `className`, `wFit` and the other width props, and the sizing part of `style` (width, margin, flex) go to the wrapper, and `ref` and the other attributes stay on the `<input>`. An invalid field shows the alert icon just inside an end section. [Field](/docs/form-components/field) passes `startSection` and `endSection` to the Input it renders.

## Customizing

Set app-wide Input defaults with `ThemeProvider`'s `themeDefaults` and add a focus-ring colour with `extraClasses`:

```tsx demo
<ThemeProvider
  themeDefaults={{ input: { lg: true, filled: true } }}
  extraClasses={{ input: { primary: 'focus:ring-2 focus:ring-blue-500/40' } }}
>
  <Input placeholder="Email" />
</ThemeProvider>
```

## Frequently asked questions

### Should I use `danger` or `invalid` to mark a wrong value?

Use `invalid`, or an `error` on the [Field](/docs/form-components/field) around the Input. `invalid` adds the danger border and ring over the current appearance, sets `aria-invalid="true"` and shows the trailing alert icon. `danger` only changes the colors, so screen readers hear nothing and no icon appears. Keep `danger` for a field that is not in error, such as the box where a user types a project name to confirm deleting it.

### Why don't my Input theme defaults reach Textarea or Select?

Each control has its own theme node. `themeDefaults={{ input: { md: true } }}` sizes every Input, while Textarea reads the `textarea` node and Select reads `select`. To keep a form consistent, set the same defaults on all three: `{ input: { md: true }, textarea: { md: true }, select: { md: true } }`. All three are flat nodes, with no `main` level like the one Button has.

### How do I put an icon or a button inside an Input?

Pass it in `startSection` or `endSection`, as [Icons, prefixes and units inside the field](#icons-prefixes-and-units-inside-the-field) shows. The field reserves room for it, and an invalid field puts the alert icon next to an end section. Give a button there `type="button"` so it does not submit the form, and an `aria-label` when it has no text. Before `@vaneui/ui` 1.8.0, place the icon or an [IconButton](/docs/basic-components/icon-button) next to the Input in a `Row` instead.

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `primary` | Appearance | yes | Primary color appearance (gray) |
| `accent` | Appearance |  | Accent color appearance (rose) |
| `secondary` | Appearance |  | Secondary color appearance (gray) |
| `tertiary` | Appearance |  | Tertiary color appearance |
| `success` | Appearance |  | Success color appearance (green) |
| `danger` | Appearance |  | Danger color appearance (red) |
| `warning` | Appearance |  | Warning color appearance (amber) |
| `info` | Appearance |  | Info color appearance (cyan) |
| `inheritAppearance` | Appearance |  | Inherit appearance from parent — suppresses own data-appearance/data-variant, uses parent's CSS variables |
| `disabled` | Disabled |  | Disable the component — reduces opacity, changes cursor to not-allowed, and prevents interaction |
| `flex1` | Flex |  | Take up remaining space (= `flex-1`, i.e. `flex: 1 1 0%`) |
| `flexAuto` | Flex |  | Grow but respect intrinsic size (= `flex-auto`, i.e. `flex: 1 1 auto`) |
| `flexNone` | Flex |  | Don't grow and don't shrink (= `flex-none`, i.e. `flex: none`) |
| `fontSans` | Font Family | yes | Sans-serif font family — emits font-sans |
| `fontSerif` | Font Family |  | Serif font family — emits font-serif |
| `fontMono` | Font Family |  | Monospace font family — emits font-mono |
| `fontHeading` | Font Family |  | Heading font family (defaults to sans, customizable via --font-heading) — emits font-heading |
| `italic` | Font Style |  | Italic font style |
| `notItalic` | Font Style |  | Not italic (normal) font style |
| `fontThin` | Font Weight |  | Thin font weight (100) — emits font-thin |
| `fontExtralight` | Font Weight |  | Extra light font weight (200) — emits font-extralight |
| `fontLight` | Font Weight |  | Light font weight (300) — emits font-light |
| `fontNormal` | Font Weight | yes | Normal font weight (400) — emits font-normal |
| `fontMedium` | Font Weight |  | Medium font weight (500) — emits font-medium |
| `fontSemibold` | Font Weight |  | Semibold font weight (600) — emits font-semibold |
| `fontBold` | Font Weight |  | Bold font weight (700) — emits font-bold |
| `fontExtrabold` | Font Weight |  | Extra bold font weight (800) — emits font-extrabold |
| `fontBlack` | Font Weight |  | Black font weight (900) — emits font-black |
| `padding` | Padding | yes | Enable internal padding |
| `paddingX` | Padding |  | Enable only horizontal padding |
| `paddingY` | Padding |  | Enable only vertical padding |
| `noPadding` | Padding |  | Disable internal padding |
| `readOnly` | Read Only |  | Render the field read-only — mutes it (lighter than disabled) and sets a default cursor; the value stays selectable and submittable |
| `pill` | Shape |  | Fully rounded corners (circular) |
| `sharp` | Shape |  | No rounded corners (square) |
| `rounded` | Shape | yes | Medium rounded corners |
| `noShrink` | Shrink |  | Prevent the flex item from shrinking below its content size (= `shrink-0`) |
| `xs` | Size |  | Extra small size |
| `sm` | Size | yes | Small size |
| `md` | Size |  | Medium size |
| `lg` | Size |  | Large size |
| `xl` | Size |  | Extra large size |
| `skeleton` | Skeleton |  | Show the component as a loading placeholder in its own size and shape. On a container, every component inside it becomes a placeholder too |
| `textLeft` | Text Align |  | Align text to left (physical side, does not flip under RTL) |
| `textCenter` | Text Align |  | Align text to center |
| `textRight` | Text Align |  | Align text to right (physical side, does not flip under RTL) |
| `textJustify` | Text Align |  | Justify text |
| `textStart` | Text Align |  | Align text to the reading-direction start (left in LTR, right in RTL) |
| `textEnd` | Text Align |  | Align text to the reading-direction end (right in LTR, left in RTL) |
| `underline` | Text Decoration |  | Add underline decoration below text |
| `lineThrough` | Text Decoration |  | Add strikethrough/line-through decoration across text |
| `noUnderline` | Text Decoration |  | Remove ALL text-decoration — underline, line-through, and overline (text-decoration: none) |
| `overline` | Text Decoration |  | Add overline decoration above text |
| `uppercase` | Text Transform |  | Transform text to uppercase |
| `lowercase` | Text Transform |  | Transform text to lowercase |
| `capitalize` | Text Transform |  | Capitalize first letter of each word |
| `normalCase` | Text Transform |  | Normal text case (no transformation) |
| `truncate` | Truncate |  | Single line truncation with ellipsis |
| `lineClamp2` | Truncate |  | Truncate at 2 lines with ellipsis |
| `lineClamp3` | Truncate |  | Truncate at 3 lines with ellipsis |
| `lineClamp4` | Truncate |  | Truncate at 4 lines with ellipsis |
| `lineClamp5` | Truncate |  | Truncate at 5 lines with ellipsis |
| `noTruncate` | Truncate |  | Remove truncation |
| `invalid` | Validity |  | Mark the field invalid (red border/ring), layered over any appearance |
| `filled` | Variant |  | Filled variant - solid background with contrasting text color |
| `outline` | Variant | yes | Outline variant - transparent background with border and colored text |
| `ghost` | Variant |  | Ghost variant - transparent background, no border, appearance-colored text, tinted hover background |

Layout and utility props (Border, Cursor, Display, Flex Direction, Focus Visible, Gap, Height, Hide, Items, Justify, Overflow, Position, Responsive Sizing, Responsive Width, Ring, Shadow, Show, Transition, Transparent, Whitespace, Width, Wrap) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
