# Context Bar (/docs/agent-ui/components/context-bar)

Use a context bar above a [composer](./composer.mdx)'s editor to show what the next prompt will carry, like files, folders, a selection or a URL, and let people remove what they don't want. It keeps to one row however many chips there are: the ones that don't fit go behind a "+N" button whose popover lists them.

<Example caption="Press Tab to focus the chips, move between them with the arrow keys, and press Delete to remove one. Press +N to see the chips that don't fit, and remove one from there.">
  <ContextBarExample />

  ```tsx tab="React"
  "use client";
  import { type ReactElement, useRef, useState } from "react";
  import { Composer, ComposerInput, ComposerSubmit } from "@/components/ui/composer";
  import { ContextBar } from "@/components/ui/context-bar";

  // Lucide "file", "folder", "square-dashed-text" and "link".
  const glyph = (...paths: string[]): ReactElement => (
    <svg
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="2"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
    >
      {paths.map((d) => (
        <path key={d} d={d} />
      ))}
    </svg>
  );

  const icons = {
    file: glyph(
      "M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z",
      "M14 2v5a1 1 0 0 0 1 1h5",
    ),
    folder: glyph(
      "M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z",
    ),
    selection: glyph(
      "M14 21h1",
      "M14 3h1",
      "M19 3a2 2 0 0 1 2 2",
      "M21 14v1",
      "M21 19a2 2 0 0 1-2 2",
      "M21 9v1",
      "M3 14v1",
      "M3 9v1",
      "M5 21a2 2 0 0 1-2-2",
      "M5 3a2 2 0 0 0-2 2",
      "M7 12h10",
      "M7 16h6",
      "M7 8h8",
      "M9 21h1",
      "M9 3h1",
    ),
    url: glyph(
      "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71",
      "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71",
    ),
  };

  const context = [
    { id: "layout", label: "src/app/layout.tsx", kind: "file", icon: icons.file },
    { id: "auth", label: "Lines 12–40 of auth.ts", kind: "selection", icon: icons.selection },
    { id: "components", label: "src/components", kind: "folder", icon: icons.folder },
    { id: "session", label: "src/lib/session.ts", kind: "file", icon: icons.file },
    { id: "docs", label: "nextjs.org/docs/app/routing", kind: "url", icon: icons.url },
    { id: "middleware", label: "middleware.ts", kind: "file", icon: icons.file },
    { id: "env", label: ".env.example", kind: "file", icon: icons.file },
  ];

  export default function ContextBarExample() {
    const [chips, setChips] = useState(context);
    const editor = useRef<HTMLDivElement>(null);

    return (
      <div className="flex w-full max-w-md flex-col gap-2">
        <ContextBar
          chips={chips}
          onRemove={(id) => setChips((current) => current.filter((chip) => chip.id !== id))}
          focusFallback={() => editor.current}
        />
        <Composer aria-label="Message">
          <ComposerInput ref={editor} aria-label="Prompt" placeholder="Ask about this context" />
          <ComposerSubmit />
        </Composer>
      </div>
    );
  }
  ```

  ```vue tab="Vue"
  <script setup lang="ts">
  import { h, shallowRef } from "vue";
  import { Composer, ComposerInput, ComposerSubmit } from "@/components/ui/vue/composer";
  import { ContextBar } from "@/components/ui/vue/context-bar";

  // Lucide "file", "folder", "square-dashed-text" and "link".
  const glyph = (...paths: string[]) =>
    h(
      "svg",
      {
        viewBox: "0 0 24 24",
        fill: "none",
        stroke: "currentColor",
        "stroke-width": "2",
        "stroke-linecap": "round",
        "stroke-linejoin": "round",
        "aria-hidden": "true",
      },
      paths.map((d) => h("path", { key: d, d })),
    );

  const icons = {
    file: glyph(
      "M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z",
      "M14 2v5a1 1 0 0 0 1 1h5",
    ),
    folder: glyph(
      "M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z",
    ),
    selection: glyph(
      "M14 21h1",
      "M14 3h1",
      "M19 3a2 2 0 0 1 2 2",
      "M21 14v1",
      "M21 19a2 2 0 0 1-2 2",
      "M21 9v1",
      "M3 14v1",
      "M3 9v1",
      "M5 21a2 2 0 0 1-2-2",
      "M5 3a2 2 0 0 0-2 2",
      "M7 12h10",
      "M7 16h6",
      "M7 8h8",
      "M9 21h1",
      "M9 3h1",
    ),
    url: glyph(
      "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71",
      "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71",
    ),
  };

  const context = [
    { id: "layout", label: "src/app/layout.tsx", kind: "file", icon: icons.file },
    { id: "auth", label: "Lines 12–40 of auth.ts", kind: "selection", icon: icons.selection },
    { id: "components", label: "src/components", kind: "folder", icon: icons.folder },
    { id: "session", label: "src/lib/session.ts", kind: "file", icon: icons.file },
    { id: "docs", label: "nextjs.org/docs/app/routing", kind: "url", icon: icons.url },
    { id: "middleware", label: "middleware.ts", kind: "file", icon: icons.file },
    { id: "env", label: ".env.example", kind: "file", icon: icons.file },
  ];

  const chips = shallowRef(context);
  const box = shallowRef<HTMLElement>();

  const remove = (id: string) => {
    chips.value = chips.value.filter((chip) => chip.id !== id);
  };
  // Focus goes to the prompt when the last chip is removed.
  const editor = () => box.value?.querySelector<HTMLElement>('[data-slot="token-input-editor"]') ?? null;
  </script>

  <template>
    <div ref="box" class="flex w-full max-w-md flex-col gap-2">
      <ContextBar :chips="chips" :on-remove="remove" :focus-fallback="editor" />
      <Composer aria-label="Message">
        <ComposerInput aria-label="Prompt" placeholder="Ask about this context" />
        <ComposerSubmit />
      </Composer>
    </div>
  </template>
  ```

  ```svelte tab="Svelte"
  <script lang="ts">
    import { Composer, ComposerInput, ComposerSubmit } from "@/components/ui/svelte/composer/index.js";
    import { ContextBar, type ContextBarChipData } from "@/components/ui/svelte/context-bar/index.js";

    const editor = $props.id();

    const context: ContextBarChipData[] = [
      { id: "layout", label: "src/app/layout.tsx", kind: "file", icon: file },
      { id: "auth", label: "Lines 12–40 of auth.ts", kind: "selection", icon: selection },
      { id: "components", label: "src/components", kind: "folder", icon: folder },
      { id: "session", label: "src/lib/session.ts", kind: "file", icon: file },
      { id: "docs", label: "nextjs.org/docs/app/routing", kind: "url", icon: url },
      { id: "middleware", label: "middleware.ts", kind: "file", icon: file },
      { id: "env", label: ".env.example", kind: "file", icon: file },
    ];
    let chips = $state.raw(context);
  </script>

  <!-- Lucide "file", "folder", "square-dashed-text" and "link". -->
  {#snippet glyph(paths: string[])}
    <svg
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
    >
      {#each paths as d (d)}<path {d} />{/each}
    </svg>
  {/snippet}
  {#snippet file()}
    {@render glyph([
      "M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z",
      "M14 2v5a1 1 0 0 0 1 1h5",
    ])}
  {/snippet}
  {#snippet folder()}
    {@render glyph([
      "M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z",
    ])}
  {/snippet}
  {#snippet selection()}
    {@render glyph([
      "M14 21h1",
      "M14 3h1",
      "M19 3a2 2 0 0 1 2 2",
      "M21 14v1",
      "M21 19a2 2 0 0 1-2 2",
      "M21 9v1",
      "M3 14v1",
      "M3 9v1",
      "M5 21a2 2 0 0 1-2-2",
      "M5 3a2 2 0 0 0-2 2",
      "M7 12h10",
      "M7 16h6",
      "M7 8h8",
      "M9 21h1",
      "M9 3h1",
    ])}
  {/snippet}
  {#snippet url()}
    {@render glyph([
      "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71",
      "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71",
    ])}
  {/snippet}

  <div class="flex w-full max-w-md flex-col gap-2">
    <ContextBar
      {chips}
      onRemove={(id: string) => (chips = chips.filter((chip) => chip.id !== id))}
      focusFallback={() => document.getElementById(editor)}
    />
    <Composer aria-label="Message">
      <ComposerInput id={editor} aria-label="Prompt" placeholder="Ask about this context" />
      <ComposerSubmit />
    </Composer>
  </div>
  ```

  ```tsx tab="Solid"
  import type { JSX } from "@solidjs/web";
  import { createSignal, For } from "solid-js";
  import { Composer, ComposerInput, ComposerSubmit } from "@/components/ui/solid/composer";
  import { ContextBar } from "@/components/ui/solid/context-bar";

  // Lucide "file", "folder", "square-dashed-text" and "link".
  const glyph =
    (...paths: string[]) =>
    (): JSX.Element => (
      <svg
        viewBox="0 0 24 24"
        fill="none"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
        aria-hidden="true"
      >
        <For each={paths}>{(d) => <path d={d} />}</For>
      </svg>
    );

  const icons = {
    file: glyph(
      "M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z",
      "M14 2v5a1 1 0 0 0 1 1h5",
    ),
    folder: glyph(
      "M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z",
    ),
    selection: glyph(
      "M14 21h1",
      "M14 3h1",
      "M19 3a2 2 0 0 1 2 2",
      "M21 14v1",
      "M21 19a2 2 0 0 1-2 2",
      "M21 9v1",
      "M3 14v1",
      "M3 9v1",
      "M5 21a2 2 0 0 1-2-2",
      "M5 3a2 2 0 0 0-2 2",
      "M7 12h10",
      "M7 16h6",
      "M7 8h8",
      "M9 21h1",
      "M9 3h1",
    ),
    url: glyph(
      "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71",
      "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71",
    ),
  };

  const context = [
    { id: "layout", label: "src/app/layout.tsx", kind: "file", icon: icons.file },
    { id: "auth", label: "Lines 12–40 of auth.ts", kind: "selection", icon: icons.selection },
    { id: "components", label: "src/components", kind: "folder", icon: icons.folder },
    { id: "session", label: "src/lib/session.ts", kind: "file", icon: icons.file },
    { id: "docs", label: "nextjs.org/docs/app/routing", kind: "url", icon: icons.url },
    { id: "middleware", label: "middleware.ts", kind: "file", icon: icons.file },
    { id: "env", label: ".env.example", kind: "file", icon: icons.file },
  ];

  export default function ContextBarExample() {
    const [chips, setChips] = createSignal(context);
    let editor: HTMLDivElement | undefined;

    return (
      <div class="flex w-full max-w-md flex-col gap-2">
        <ContextBar
          chips={chips()}
          onRemove={(id) => setChips((current) => current.filter((chip) => chip.id !== id))}
          focusFallback={() => editor ?? null}
        />
        <Composer aria-label="Message">
          <ComposerInput
            ref={(el) => {
              editor = el;
            }}
            aria-label="Prompt"
            placeholder="Ask about this context"
          />
          <ComposerSubmit />
        </Composer>
      </div>
    );
  }
  ```
</Example>

## Installation [#installation]

```bash tab="React"
npx shadcn@latest add @kind-ui/context-bar
```

```bash tab="Web Components"
npx shadcn@latest add @kind-ui/context-bar-web
```

```bash tab="Vue"
npx shadcn@latest add @kind-ui/context-bar-vue
```

```bash tab="Svelte"
npx shadcn@latest add @kind-ui/context-bar-svelte
```

```bash tab="Solid"
npx shadcn@latest add @kind-ui/context-bar-solid
```

## Composition [#composition]

```text
ContextBar
├── ContextBarList
│   └── ContextBarChip
├── ContextBarOverflow
└── ContextBarContent
    ├── ContextBarTitle
    └── ContextBarOverflowList
        └── ContextBarItem
```

`ContextBar` is a group named by its `label`. `ContextBarList` is the row of chips, and each `ContextBarChip` shows a chip's icon, its label and a remove control. `ContextBarOverflow` is the "+N" button, present only while some chips don't fit, and named "N more". `ContextBarContent` is its popover: `ContextBarTitle` names it for assistive technology only, and `ContextBarOverflowList` holds a `ContextBarItem` for each chip that didn't fit. `ContextBar` renders the list, the button and the popover, and each of them its parts, when you pass no children.

## Usage [#usage]

### Chips [#chips]

Pass `chips`, in order, each with an `id` and a `label`. Add a `kind`, like `"file"` or `"folder"`, to style chips by what they hold: it's written as `data-kind`. In React, a chip's `icon` is an image URL or an element such as an inline `svg`. A chip added later fades up once; the ones there at first don't.

### Overflow [#overflow]

The bar measures its width and its chips' widths, and the chips that don't fit go behind "+N", which counts them and updates as chips come and go. A long label is cut short at `--kind-context-chip-max-width` (12rem by default), and the room kept for the button is `--kind-context-bar-more-width`; set either on the bar or an ancestor. The bar stays one row, so adding or removing a chip never moves what's below it.

### Removing [#removing]

Pass `onRemove` to let people remove chips. It gets the chip's `id`, and you take the chip out of `chips`. Delete or Backspace removes the focused chip, and each chip shows a remove control for the pointer, which is hidden from assistive technology, as [Attachments](./attachments.mdx)' is. The reason is `remove-press` from either, or `imperative` from the api, like an agent's `remove`. Call `details.cancel()` to keep the chip.

```tsx
<ContextBar
  chips={chips}
  onRemove={(id) => setChips((current) => current.filter((chip) => chip.id !== id))}
/>
```

A removed chip fades out, then the chips after it slide into its place. When it held focus, focus moves to a neighbor, else to "+N", else to `focusFallback`. Pass the composer's editor there, so focus isn't lost when the last chip goes.

### Opening [#opening]

"+N" opens the popover with focus on its first chip, and each chip there is removable the same way. Escape closes it with focus back on "+N", and removing the last chip that didn't fit closes it. Pass `open` and `onOpenChange` to control whether it's open, or `defaultOpen` to start open. `onOpenChange` gets UI SDK Popover's reason, like `trigger-press` or `escape-key`.

### Keyboard [#keyboard]

The row of chips is one Tab stop, and the arrow keys move between them. Tab moves on to "+N" when it's present. The popover's list is one Tab stop too, with the arrow keys between its chips.

### Presentation and Motion [#presentation-and-motion]

With `presentation="auto"`, the default, the popover is a bottom sheet on small touch screens; pass `"popover"` or `"sheet"` to choose one. `motion` sets how chips enter, leave and slide: `"auto"`, the default, follows `prefers-reduced-motion`; `"reduced"` only fades, and `"none"` moves nothing.

## API Reference [#api-reference]

### ContextBarOptions

| Prop             | Type                                                                                      | Description                                                                                                                                     |
| ---------------- | ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `chips?`         | `readonly ContextChip<unknown>[] \| undefined`                                            | The chips, in order, keyed by `id`. Default: `[]`                                                                                               |
| `label?`         | `string \| undefined`                                                                     | Names the bar and the list of the chips that don't fit. Default: `"Context"`                                                                    |
| `onRemove?`      | `((id: string, details: ChangeDetails<ContextBarRemoveReason>) => void) \| undefined`     | Called with a chip's `id` when someone removes it. While it's set, chips can be removed.                                                        |
| `focusFallback?` | `(() => HTMLElement \| null \| undefined) \| undefined`                                   | Where focus goes when a focused chip leaves and no chip is left to take it, like the composer's editor. Without it, focus is lost.              |
| `open?`          | `boolean \| undefined`                                                                    | Whether the list of the chips that don't fit is open. Passing it controls it: change it in `onOpenChange`.                                      |
| `defaultOpen?`   | `boolean \| undefined`                                                                    | Whether the list of the chips that don't fit is open at first, when `open` isn't passed. Default: `false`                                       |
| `onOpenChange?`  | `((open: boolean, details: ChangeDetails<PopoverOpenChangeReason>) => void) \| undefined` | Called when the list of the chips that don't fit opens or closes, with what caused it.                                                          |
| `presentation?`  | `"auto" \| "popover" \| "sheet"`                                                          | How the list of the chips that don't fit presents. `"auto"` is a bottom sheet on small touch screens and a popover elsewhere. Default: `"auto"` |
| `motion?`        | `"auto" \| "none" \| "reduced"`                                                           | `"auto"` follows `prefers-reduced-motion`. Default: `"auto"`                                                                                    |

Vue, Svelte and Solid take the same props. Controlled props and callbacks are spelled as each framework expects:

| React                  | Vue                            | Svelte                      | Solid                  |
| ---------------------- | ------------------------------ | --------------------------- | ---------------------- |
| `open`, `onOpenChange` | `v-model:open`, `@open-change` | `bind:open`, `onOpenChange` | `open`, `onOpenChange` |
| `onRemove`             | `@remove`                      | `onRemove`                  | `onRemove`             |

### Agent [#agent]

An agent connected with `createAgent` finds the context bar by its `label`, "Context" by default, and reads `open`. It lists every chip by `id` and label. `remove` takes a chip's `id` and refuses one that isn't there; it's offered while `onRemove` is set. `open` is offered while some chips don't fit and the popover is closed, and `close` while it's open. None needs a person to confirm.

### ContextBarAgentActions

| Prop     | Type                                     | Description                                                                                                                                      |
| -------- | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| `remove` | `((id: string) => boolean) \| undefined` | Removes a chip by its `id`, through `onRemove`. Returns `false` for an unknown chip or a canceled removal. Offered only while `onRemove` is set. |
| `open`   | `(() => boolean) \| undefined`           | Opens the list of the chips that don't fit. Offered only while some don't and it's closed.                                                       |
| `close`  | `(() => boolean) \| undefined`           | Closes the list of the chips that don't fit. Offered only while it's open.                                                                       |

<Caption>
  Runs in React, Vue, Svelte, Solid and Web Components. On `<kind-context-bar>`, set `chips` as a property and `label` and `presentation` as attributes, and listen for `openchange`. Add the `can-remove` attribute to listen for `contextremove`, with the chip's `id` as `event.detail.value`. The element fills in its parts when it has no children.
</Caption>
