# Input Group

A text field with addons at its start or end: text, an icon or a button.

An Input with addons joined to it: a prefix or suffix text (https://, .com, USD), an icon, a keyboard hint or a Button at its start or end. The group is the field (the Input's colour, hairline, radius, heights, hover, rings and motion) and the Input inside it draws nothing of its own. A press on an addon's text puts the caret in the field. Inside a Field it is wired to the label, helper and error.

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

## Add

```bash
npx fndn add input-group
```

It writes the folder `input-group/` into the project's ui folder (`index.ts`, `input-group.tsx`, `InputGroup.vue`, `InputGroup.svelte`, `input-group.recipe.ts`), and what it is built from: [Input](/components/input.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { InputGroup } from "@/components/ui/input-group";
```

```vue
import { InputGroup } from "@/components/ui/input-group";
```

```svelte
import { InputGroup } from "$lib/components/ui/input-group";
```

## Parts

In the DOM the root carries `data-slot="input-group"` and each part `data-slot="input-group-<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` | `div` | `input-group` |  | The field: the shared control (`lib/control.ts`), field kind: its sizes and looks, the hover fill, the accent ring while focus is inside, the red ring when it is invalid, half opacity when disabled. class and style land here; every other attribute on the Input. |
| `start` | `span` | `input-group-start` | optional | Addon before the Input: text ink-2, nowrap, icons ink-3 and 14px; as far from the edge as the Input's text (the size's padding), 4px around a Button (2px up to 28px). |
| `input` | `input` | `input-group-input` |  | The Input component (data-input-group="input"), transparent, without its own ring; 8px from an addon. |
| `end` | `span` | `input-group-end` | optional | Addon after the Input, as the start addon. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `start` | `node (children / slot)` |  | Addon before the Input: text, an icon, a Kbd, or a Button (ghost, xs, square, muted, with a tooltip). |
| `end` | `node (children / slot)` |  | Addon after the Input, as start. |
| `value` | `string` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). The Input's value. Controlled: value with onChange. Uncontrolled: defaultValue. |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The shared control's sizes (`lib/control.ts`): xs 24px, sm 28px, md 32px, lg 36px, with their text and padding. |
| `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 on the Input; the group shows the red ring. |
| `disabled` | `boolean` | `false` |  |
| `placeholder` | `string` |  |  |

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

## States

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

## Keyboard and accessibility

Keyboard:

- The Input's: standard text editing; Tab moves to a Button in an addon, then on

Notes:

- Addon text is not part of the Input's name: name the Input with a Field label or an aria-label, and say the unit there when it matters.
- A Button in an addon is a real Button with its own name (a tooltip names an icon-only one).
- Text is 16px on touch devices (the Input's).

## Motion

- the Input's: the ring over duration.base and the fill over duration.fast, on the group
- nothing else moves

## Tokens

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

## Dependencies

- [Input](/components/input.md)

## Examples

### Search projects

React · `search-projects.tsx`

```tsx
import { InputGroup } from "@/components/ui/input-group";
import { MagnifyingGlass } from "@phosphor-icons/react";

/* an icon addon is decoration: hidden from assistive technology, the input keeps its name */
export default function SearchProjects() {
  return (
    <InputGroup
      start={<MagnifyingGlass weight="bold" aria-hidden />}
      type="search"
      placeholder="Search projects"
      aria-label="Search projects"
      className="w-full max-w-80"
    />
  );
}
```

Vue · `search-projects.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhMagnifyingGlass } from "@phosphor-icons/vue";
import { InputGroup } from "@/components/ui/input-group";

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

<!-- an icon addon is decoration: hidden from assistive technology, the input keeps its name -->
<template>
  <InputGroup v-model:value="query" type="search" placeholder="Search projects" aria-label="Search projects" class="w-full max-w-80">
    <template #start><PhMagnifyingGlass weight="bold" aria-hidden="true" /></template>
  </InputGroup>
</template>
```

Svelte · `search-projects.svelte`

```svelte
<script lang="ts">
  import MagnifyingGlass from "phosphor-svelte/lib/MagnifyingGlassIcon";
  import { InputGroup } from "$lib/components/ui/input-group";

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

<!-- an icon addon is decoration: hidden from assistive technology, the input keeps its name -->
{#snippet start()}<MagnifyingGlass weight="bold" aria-hidden="true" />{/snippet}
<InputGroup bind:value={query} {start} type="search" placeholder="Search projects" aria-label="Search projects" class="w-full max-w-80" />
```

### Spending limit in dollars

React · `spending-limit.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { InputGroup } from "@/components/ui/input-group";

/* text addons give the unit; inside a Field the label and helper are wired as for an Input */
export default function SpendingLimit() {
  return (
    <Field label="Spending limit" helper="Deploys pause when the month reaches it." className="w-full max-w-80">
      <InputGroup start="$" end="USD" defaultValue="1,240.00" inputMode="decimal" />
    </Field>
  );
}
```

Vue · `spending-limit.vue`

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

const limit = ref("1,240.00");
</script>

<!-- text addons give the unit; inside a Field the label and helper are wired as for an Input -->
<template>
  <Field label="Spending limit" helper="Deploys pause when the month reaches it." class="w-full max-w-80">
    <InputGroup v-model:value="limit" start="$" end="USD" inputmode="decimal" />
  </Field>
</template>
```

Svelte · `spending-limit.svelte`

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

  let limit = $state("1,240.00");
</script>

<!-- text addons give the unit; inside a Field the label and helper are wired as for an Input -->
<Field label="Spending limit" helper="Deploys pause when the month reaches it." class="w-full max-w-80">
  <InputGroup bind:value={limit} start="$" end="USD" inputmode="decimal" />
</Field>
```

### API key with a copy button

React · `api-key.tsx`

```tsx
import { useState } from "react";
import { Button } from "@/components/ui/button";
import { InputGroup } from "@/components/ui/input-group";
import { Tooltip } from "@/components/ui/tooltip";
import { Copy } from "@phosphor-icons/react";

/* a Button in an addon: ghost, xs, square, muted, named by its tooltip */
export default function ApiKey() {
  const [copied, setCopied] = useState(false);
  const key = "nw_live_4f9a2c71e0b8d3";
  return (
    <InputGroup
      readOnly
      mono
      value={key}
      aria-label="API key"
      className="w-full max-w-80"
      end={
        <Tooltip content={copied ? "Copied" : "Copy"}>
          <Button
            variant="ghost"
            size="xs"
            square
            muted
            aria-label="Copy API key"
            icon={<Copy data-slot="icon" weight="bold" />}
            onClick={() => { void navigator.clipboard?.writeText(key); setCopied(true); }}
          />
        </Tooltip>
      }
    />
  );
}
```

Vue · `api-key.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { PhCopy } from "@phosphor-icons/vue";
import { Button } from "@/components/ui/button";
import { InputGroup } from "@/components/ui/input-group";
import { Tooltip } from "@/components/ui/tooltip";

const copied = ref(false);
const key = "nw_live_4f9a2c71e0b8d3";
function copy() {
  void navigator.clipboard?.writeText(key);
  copied.value = true;
}
</script>

<!-- a Button in an addon: ghost, xs, square, muted, named by its tooltip -->
<template>
  <InputGroup readonly mono :value="key" aria-label="API key" class="w-full max-w-80">
    <template #end>
      <Tooltip :content="copied ? 'Copied' : 'Copy'">
        <Button variant="ghost" size="xs" square muted aria-label="Copy API key" @click="copy">
          <template #icon><PhCopy data-slot="icon" weight="bold" /></template>
        </Button>
      </Tooltip>
    </template>
  </InputGroup>
</template>
```

Svelte · `api-key.svelte`

```svelte
<script lang="ts">
  import Copy from "phosphor-svelte/lib/CopyIcon";
  import { Button } from "$lib/components/ui/button";
  import { InputGroup } from "$lib/components/ui/input-group";
  import { Tooltip } from "$lib/components/ui/tooltip";

  let copied = $state(false);
  const key = "nw_live_4f9a2c71e0b8d3";
</script>

<!-- a Button in an addon: ghost, xs, square, muted, named by its tooltip -->
{#snippet end()}
  <Tooltip content={copied ? "Copied" : "Copy"}>
    {#snippet trigger(props)}
      <Button
        {...props()}
        variant="ghost"
        size="xs"
        square
        muted
        aria-label="Copy API key"
        onclick={() => { void navigator.clipboard?.writeText(key); copied = true; }}
      >
        {#snippet icon()}<Copy data-slot="icon" weight="bold" />{/snippet}
      </Button>
    {/snippet}
  </Tooltip>
{/snippet}
<InputGroup readonly mono value={key} aria-label="API key" class="w-full max-w-80" {end} />
```

### Top bar search with a shortcut

React · `command-search.tsx`

```tsx
import { InputGroup } from "@/components/ui/input-group";
import { Kbd } from "@/components/ui/kbd";
import { MagnifyingGlass } from "@phosphor-icons/react";

/* the top bar's search: the Kbd addon tells the shortcut that focuses it from anywhere */
export default function CommandSearch() {
  return (
    <InputGroup
      start={<MagnifyingGlass weight="bold" aria-hidden />}
      end={<Kbd keys={["⌘", "K"]} />}
      placeholder="Search or jump to…"
      aria-label="Search Northwind"
      aria-keyshortcuts="Meta+K"
      className="w-full max-w-72"
    />
  );
}
```

Vue · `command-search.vue`

```vue
<script setup lang="ts">
import { PhMagnifyingGlass } from "@phosphor-icons/vue";
import { InputGroup } from "@/components/ui/input-group";
import { Kbd } from "@/components/ui/kbd";
</script>

<!-- the top bar's search: the Kbd addon tells the shortcut that focuses it from anywhere -->
<template>
  <InputGroup placeholder="Search or jump to…" aria-label="Search Northwind" aria-keyshortcuts="Meta+K" class="w-full max-w-72">
    <template #start><PhMagnifyingGlass weight="bold" aria-hidden="true" /></template>
    <template #end><Kbd :keys="['⌘', 'K']" /></template>
  </InputGroup>
</template>
```

Svelte · `command-search.svelte`

```svelte
<script lang="ts">
  import MagnifyingGlass from "phosphor-svelte/lib/MagnifyingGlassIcon";
  import { InputGroup } from "$lib/components/ui/input-group";
  import { Kbd } from "$lib/components/ui/kbd";
</script>

<!-- the top bar's search: the Kbd addon tells the shortcut that focuses it from anywhere -->
{#snippet start()}<MagnifyingGlass weight="bold" aria-hidden="true" />{/snippet}
{#snippet end()}<Kbd keys={["⌘", "K"]} />{/snippet}
<InputGroup {start} {end} placeholder="Search or jump to…" aria-label="Search Northwind" aria-keyshortcuts="Meta+K" class="w-full max-w-72" />
```

### Preview URL in a side panel

React · `preview-url.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { InputGroup } from "@/components/ui/input-group";

/* sm and subtle, as in a dense side panel; the addons keep the fixed parts of the URL out of the value */
export default function PreviewUrl() {
  return (
    <Field label="Preview URL" className="w-full max-w-72">
      <InputGroup size="sm" variant="subtle" mono start="https://" end=".example.dev" defaultValue="feat-usage-alerts" />
    </Field>
  );
}
```

Vue · `preview-url.vue`

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

const subdomain = ref("feat-usage-alerts");
</script>

<!-- sm and subtle, as in a dense side panel; the addons keep the fixed parts of the URL out of the value -->
<template>
  <Field label="Preview URL" class="w-full max-w-72">
    <InputGroup v-model:value="subdomain" size="sm" variant="subtle" mono start="https://" end=".example.dev" />
  </Field>
</template>
```

Svelte · `preview-url.svelte`

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

  let subdomain = $state("feat-usage-alerts");
</script>

<!-- sm and subtle, as in a dense side panel; the addons keep the fixed parts of the URL out of the value -->
<Field label="Preview URL" class="w-full max-w-72">
  <InputGroup bind:value={subdomain} size="sm" variant="subtle" mono start="https://" end=".example.dev" />
</Field>
```

## Recipe

`input-group.recipe.ts`:

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

const ctl = control({ kind: "field" });

/* input-group — an Input with addons joined to it: text (https://, USD), an icon, a Kbd or a
 * Button at its start or end. The group is the field: it is the shared control recipe (lib/control.ts:
 * sizes xs 24 · sm 28 · md 32 · lg 36, looks outline · subtle · ghost, the hover fill, the accent
 * ring on focus, the red ring when invalid, half opacity when disabled, the same motion), and the
 * Input inside it goes transparent (its own fill, hairline and rings are switched off by the `input` slot, merged over
 * the input recipe). The states are read from the Input itself (:focus, aria-invalid, :disabled),
 * so a Field around the group drives them too.
 *
 * Addons: text in ink-2 (a unit or a prefix says something), an icon in ink-3, as far from the
 * edge as the Input's own padding; the Input's padding on that side becomes the 8px gap. An addon that holds a Button keeps 4px around it (xs, 24px: concentric
 * with the 32px field). A click on an addon's text focuses the Input (the adapter). */
export const inputGroup = tv({
  slots: {
    /* the shared control recipe (lib/control.ts), field kind: focus anywhere in the group rings it.
     * Invalid and disabled are read from the Input (Field context or the prop), so a Field around
     * the group drives them too. The group draws the box; the Input inside draws nothing. */
    base: [
      ctl.base(), "flex items-center px-0",
      "has-[[data-slot=input][aria-invalid=true]]:[box-shadow:0_0_0_1px_var(--fndn-red)]",
      "has-[[data-slot=input][aria-invalid=true]:focus]:[box-shadow:0_0_0_1px_var(--fndn-red),0_0_0_4px_var(--fndn-red-tint)]",
      "has-[[data-slot=input]:disabled]:opacity-50 has-[[data-slot=input]:disabled]:pointer-events-none",
    ],
    /* text: one line, cut at its end past 45% of the field (the adapter adds a title while cut);
     * an icon, a Kbd or a Button: a centred row that never shrinks */
    start: [
      "min-w-0 max-w-[45%] shrink-0 overflow-hidden text-ellipsis whitespace-nowrap text-ink-2 cursor-text [unicode-bidi:plaintext]",
      "has-[>*]:flex has-[>*]:items-center has-[>*]:gap-1.5 has-[>*]:overflow-visible has-[>*]:max-w-none [&>svg]:size-[14px] [&>svg]:shrink-0 [&>svg]:text-ink-3",
    ],
    end: [
      "min-w-0 max-w-[45%] shrink-0 overflow-hidden text-ellipsis whitespace-nowrap text-ink-2 cursor-text [unicode-bidi:plaintext]",
      "has-[>*]:flex has-[>*]:items-center has-[>*]:gap-1.5 has-[>*]:overflow-visible has-[>*]:max-w-none [&>svg]:size-[14px] [&>svg]:shrink-0 [&>svg]:text-ink-3",
    ],
    /* merged over the input recipe (twMerge): the Input draws nothing of its own (no fill, edge,
     * ring or coarse minimum: the group has them); it keeps the size's padding on a side with no
     * addon, and 8px beside one */
    input: [
      "flex-1 h-full min-h-0 pointer-coarse:min-h-0 bg-transparent shadow-none",
      "[@media(hover:hover)_and_(pointer:fine)]:[&:where(:hover:not(:focus-visible,:has(:focus-visible),[aria-expanded=true],[readonly],[data-readonly],[aria-readonly=true]))]:bg-transparent",
      "focus-within:bg-transparent focus-within:[box-shadow:none]",
      "[&:is([aria-invalid=true],[data-invalid])]:[box-shadow:none] [&:is([aria-invalid=true],[data-invalid]):focus-within]:[box-shadow:none]",
      "disabled:opacity-100",
      "[[data-slot=input-group-start]~&]:ps-2 [&:has(~[data-slot=input-group-end])]:pe-2",
    ],
  },
  variants: {
    /* the shared control's sizes (an addon is as far from the edge as the Input's text would be;
     * one that holds a Button keeps the Button's inset: xs and sm 2px, md and lg 4px) and looks */
    size: {
      xs: { base: controlSize.xs, start: "ps-(--control-px) has-[[data-slot=button]]:ps-0", end: "pe-(--control-px) has-[[data-slot=button]]:pe-0" },
      sm: { base: controlSize.sm, start: "ps-(--control-px) has-[[data-slot=button]]:ps-0.5", end: "pe-(--control-px) has-[[data-slot=button]]:pe-0.5" },
      md: { base: controlSize.md, start: "ps-(--control-px) has-[[data-slot=button]]:ps-1", end: "pe-(--control-px) has-[[data-slot=button]]:pe-1" },
      lg: { base: controlSize.lg, start: "ps-(--control-px) has-[[data-slot=button]]:ps-1.5", end: "pe-(--control-px) has-[[data-slot=button]]:pe-1.5" },
    },
    variant: {
      outline: { base: controlVariant.outline },
      subtle: { base: controlVariant.subtle },
      ghost: { base: controlVariant.ghost },
    },
  },
  defaultVariants: { size: "md", variant: "outline" },
});

export type InputGroupRecipeProps = VariantProps<typeof inputGroup>;
```
