# Input

A one-line text field.

A one-line text field: a name, an email, a URL, a search. It is the shared control (`lib/control.ts`), field kind: any focus shows the accent ring. Sizes xs 24 · sm 28 · md 32 · lg 36 (one scale with every control, buttons included) and looks outline · subtle · ghost. Inside a Field (field.json) it is wired to the field's label, helper and error through Ark UI's field context.

Kind: component · Category: Inputs · Status: stable · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add input
```

It writes the folder `input/` into the project's ui folder (`index.ts`, `input.tsx`, `Input.vue`, `Input.svelte`, `input.recipe.ts`). First time: [Installation](/installation.md).

## Usage

```tsx
import { Input } from "@/components/ui/input";
```

```vue
import { Input } from "@/components/ui/input";
```

```svelte
import { Input } from "$lib/components/ui/input";
```

## Parts

In the DOM the root carries `data-slot="input"` and each part `data-slot="input-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `input` | `input` |  | input or textarea No wrapper: the control is the root, so class and style land on this outermost element (what a caller sizes and places); every other attribute stays on the control. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`).  |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The shared control's sizes (`lib/control.ts`), dimensions only: xs 24px, sm 28px, md 32px, lg 36px; text 12 · 12 · 13 · 14px; padding 6 · 8 · 10 · 12px. 40px and 16px text on a coarse pointer. |
| `variant` | `"outline" \| "subtle" \| "ghost"` | `"outline"` | The shared control's looks (`lib/control.ts`), at every size. outline: the form field (field colour, hairline). subtle: the inspector (inset grey, no edge). ghost: nothing until touched. Focused, open and invalid look the same in every variant. |
| `mono` | `boolean` | `false` |  |
| `invalid` | `boolean` | `false` | aria-invalid and the red ring. Absent: a surrounding Field decides. |
| `disabled` | `boolean` | `false` |  |
| `multiline` | `boolean` | `false` | Render a textarea. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `input` | (value: string) |

## States

`hover`, `focus`, `invalid`, `disabled`

## Keyboard and accessibility

Keyboard:

- Standard text editing; Tab moves on

Notes:

- Every field needs a name: a Field label (wired by Ark's field context) or an aria-label.
- Inside a Field the control is described by the helper or the error (aria-describedby) and marked aria-invalid; the error should say how to fix the problem.
- Text is at least 16px on touch devices so iOS does not zoom the page on focus.
- Set type, inputmode, autocomplete and enterkeyhint for the data the field collects.

## Motion

- focus ring and invalid ring: box-shadow over duration.base, ease
- hover (pointer only): background over duration.fast, ease

## Tokens

`color.field`, `color.surface`, `color.hover`, `color.ink`, `color.ink-3`, `color.accent`, `color.accent-tint`, `color.red`, `color.red-tint`, `shadow.hairline`, `radius.control`, `text.xs`, `text.sm`, `font.mono`, `duration.fast`, `duration.base`

## Examples

### Project name

React · `project-name.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

/* an Input almost always sits in a Field: the label and the helper are wired to it */
export default function ProjectName() {
  return (
    <Field label="Project name" helper="Used in URLs and the CLI: lowercase and dashes." className="w-full max-w-80">
      <Input defaultValue="billing-api" autoComplete="off" />
    </Field>
  );
}
```

Vue · `project-name.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

const name = ref("billing-api");
</script>

<!-- an Input almost always sits in a Field: the label and the helper are wired to it -->
<template>
  <Field label="Project name" helper="Used in URLs and the CLI: lowercase and dashes." class="w-full max-w-80">
    <Input v-model:value="name" autocomplete="off" />
  </Field>
</template>
```

Svelte · `project-name.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";

  let name = $state("billing-api");
</script>

<!-- an Input almost always sits in a Field: the label and the helper are wired to it -->
<Field label="Project name" helper="Used in URLs and the CLI: lowercase and dashes." class="w-full max-w-80">
  <Input bind:value={name} autocomplete="off" />
</Field>
```

### Filter in a toolbar

React · `filter-toolbar.tsx`

```tsx
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { DownloadSimple } from "@phosphor-icons/react";

/* sm: 28px, the same height as the sm Button beside it */
export default function FilterToolbar() {
  return (
    <div className="flex w-full max-w-[360px] items-center gap-2">
      <Input size="sm" type="search" placeholder="Filter by branch or commit" aria-label="Filter deploys" className="min-w-0 flex-1" />
      <Button size="sm" icon={<DownloadSimple data-slot="icon" weight="bold" />}>Export</Button>
    </div>
  );
}
```

Vue · `filter-toolbar.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhDownloadSimple } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";

const query = ref("");
</script>

<!-- sm: 28px, the same height as the sm Button beside it -->
<template>
  <div class="flex w-full max-w-[360px] items-center gap-2">
    <Input v-model:value="query" size="sm" type="search" placeholder="Filter by branch or commit" aria-label="Filter deploys" class="min-w-0 flex-1" />
    <Button size="sm">
      <template #icon><PhDownloadSimple data-slot="icon" weight="bold" /></template>
      Export
    </Button>
  </div>
</template>
```

Svelte · `filter-toolbar.svelte`

```svelte
<script lang="ts">
  import DownloadSimple from "phosphor-svelte/lib/DownloadSimpleIcon";
  import { Button } from "$lib/components/ui/button";
  import { Input } from "$lib/components/ui/input";

  let query = $state("");
</script>

<!-- sm: 28px, the same height as the sm Button beside it -->
<div class="flex w-full max-w-[360px] items-center gap-2">
  <Input bind:value={query} size="sm" type="search" placeholder="Filter by branch or commit" aria-label="Filter deploys" class="min-w-0 flex-1" />
  <Button size="sm">
    {#snippet icon()}<DownloadSimple data-slot="icon" weight="bold" />{/snippet}
    Export
  </Button>
</div>
```

### Environment variable

React · `environment-variable.tsx`

```tsx
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Trash } from "@phosphor-icons/react";

/* mono for keys, values and anything copied from a terminal */
export default function EnvironmentVariable() {
  return (
    <div className="flex w-full max-w-[400px] items-center gap-2">
      <Input mono defaultValue="DATABASE_URL" aria-label="Key" className="w-36 min-w-0" />
      <Input mono defaultValue="postgres://web-app@db.northwind.example:5432/main" aria-label="Value" className="min-w-0 flex-1" />
      <Button variant="ghost" square muted aria-label="Remove DATABASE_URL" icon={<Trash data-slot="icon" weight="bold" />} />
    </div>
  );
}
```

Vue · `environment-variable.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhTrash } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";

const key = ref("DATABASE_URL");
const value = ref("postgres://web-app@db.northwind.example:5432/main");
</script>

<!-- mono for keys, values and anything copied from a terminal -->
<template>
  <div class="flex w-full max-w-[400px] items-center gap-2">
    <Input v-model:value="key" mono aria-label="Key" class="w-36 min-w-0" />
    <Input v-model:value="value" mono aria-label="Value" class="min-w-0 flex-1" />
    <Button variant="ghost" square muted aria-label="Remove DATABASE_URL">
      <template #icon><PhTrash data-slot="icon" weight="bold" /></template>
    </Button>
  </div>
</template>
```

Svelte · `environment-variable.svelte`

```svelte
<script lang="ts">
  import Trash from "phosphor-svelte/lib/TrashIcon";
  import { Button } from "$lib/components/ui/button";
  import { Input } from "$lib/components/ui/input";

  let key = $state("DATABASE_URL");
  let value = $state("postgres://web-app@db.northwind.example:5432/main");
</script>

<!-- mono for keys, values and anything copied from a terminal -->
<div class="flex w-full max-w-[400px] items-center gap-2">
  <Input bind:value={key} mono aria-label="Key" class="w-36 min-w-0" />
  <Input bind:value={value} mono aria-label="Value" class="min-w-0 flex-1" />
  <Button variant="ghost" square muted aria-label="Remove DATABASE_URL">
    {#snippet icon()}<Trash data-slot="icon" weight="bold" />{/snippet}
  </Button>
</div>
```

### Build settings in a side panel

React · `build-settings.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

/* subtle: the grey inset look for dense side panels, where an outline on every field is noise */
export default function BuildSettings() {
  return (
    <div className="flex w-full max-w-64 flex-col gap-3">
      <Field label="Build command">
        <Input size="sm" variant="subtle" mono defaultValue="pnpm build" />
      </Field>
      <Field label="Output directory">
        <Input size="sm" variant="subtle" mono defaultValue="apps/web/dist" />
      </Field>
    </div>
  );
}
```

Vue · `build-settings.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

const command = ref("pnpm build");
const output = ref("apps/web/dist");
</script>

<!-- subtle: the grey inset look for dense side panels, where an outline on every field is noise -->
<template>
  <div class="flex w-full max-w-64 flex-col gap-3">
    <Field label="Build command">
      <Input v-model:value="command" size="sm" variant="subtle" mono />
    </Field>
    <Field label="Output directory">
      <Input v-model:value="output" size="sm" variant="subtle" mono />
    </Field>
  </div>
</template>
```

Svelte · `build-settings.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";

  let command = $state("pnpm build");
  let output = $state("apps/web/dist");
</script>

<!-- subtle: the grey inset look for dense side panels, where an outline on every field is noise -->
<div class="flex w-full max-w-64 flex-col gap-3">
  <Field label="Build command">
    <Input bind:value={command} size="sm" variant="subtle" mono />
  </Field>
  <Field label="Output directory">
    <Input bind:value={output} size="sm" variant="subtle" mono />
  </Field>
</div>
```

### Locked for members

React · `locked-for-members.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

/* a disabled Field fades its label and control together; the helper says why */
export default function LockedForMembers() {
  return (
    <Field label="Workspace URL" helper="Only owners can change the workspace URL." disabled className="w-full max-w-80">
      <Input defaultValue="northwind" />
    </Field>
  );
}
```

Vue · `locked-for-members.vue`

```vue
<script setup lang="ts">
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
</script>

<!-- a disabled Field fades its label and control together; the helper says why -->
<template>
  <Field label="Workspace URL" helper="Only owners can change the workspace URL." disabled class="w-full max-w-80">
    <Input value="northwind" />
  </Field>
</template>
```

Svelte · `locked-for-members.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";
</script>

<!-- a disabled Field fades its label and control together; the helper says why -->
<Field label="Workspace URL" helper="Only owners can change the workspace URL." disabled class="w-full max-w-80">
  <Input value="northwind" />
</Field>
```

## Recipe

`input.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";
import { control, controlSize, controlVariant } from "../lib/control";

/* input — a text field. Its label, helper and error come from a Field around it (field.recipe.ts),
 * through Ark UI's field context.
 *
 * The box is the shared control recipe (lib/control.ts): the sizes
 * (xs 24 · sm 28 · md 32 · lg 36), the looks (outline, subtle, ghost) and every state (hover,
 * focus ring for any focus, invalid, disabled, read-only, 16px and 40px on coarse pointers) are
 * its. What is here: the native input's reset, the type, the direction of the text and the
 * textarea metrics. The part is found by data-slot (input); the root slot is `base`. */

/* the field kind (the ring for any focus); size and variant are passed below */
const c = control({ kind: "field" });

export const input = tv({
  slots: {
    base: [
      c.base(),
      "appearance-none block m-0 py-0 border-0",
      "[font-weight:inherit] [font-style:inherit] tracking-[inherit] leading-[1.4] text-ellipsis",
      /* dir="auto" (every adapter sets it): the value and the placeholder read in their own
       * direction; the alignment stays on the page's side (the parent's direction, not the text's) */
      "[:dir(ltr)>&[dir=auto]]:text-left [:dir(rtl)>&[dir=auto]]:text-right",
    ],
  },
  variants: {
    /* a textarea grows from 72px and resizes vertically; its block padding centres one line of
     * text in the size's height, so its first line sits where a single-line field's text does */
    multiline: {
      true: { base: "h-auto min-h-18 leading-[1.5] resize-y" },
    },
    size: {
      xs: { base: controlSize.xs },
      sm: { base: controlSize.sm },
      md: { base: controlSize.md },
      lg: { base: controlSize.lg },
    },
    variant: {
      outline: { base: controlVariant.outline },
      subtle: { base: controlVariant.subtle },
      ghost: { base: controlVariant.ghost },
    },
    mono: {
      true: { base: "font-mono" },
      false: { base: "[font-family:inherit]" },
    },
  },
  compoundVariants: [
    { multiline: true, size: "xs", class: { base: "py-[3px]" } },
    { multiline: true, size: "sm", class: { base: "py-[5px]" } },
    { multiline: true, size: "md", class: { base: "py-[7px]" } },
    { multiline: true, size: "lg", class: { base: "py-2" } },
  ],
  defaultVariants: { size: "md", variant: "outline", mono: false, multiline: false },
});

export type InputRecipeProps = VariantProps<typeof input>;
```
