# GlassSearch

> A refractive glass search field that looks in several layers at once — apps, files, people — and opens into its ranked results, with its layers budding off the field as droplets.

```tsx
import {
  Calculator,
  CalendarDays,
  Camera,
  LayoutGrid,
  Map,
  NotebookPen,
  Users,
} from "lucide-react";
import { useState } from "react";

import { GlassSearch, type GlassSearchLayer } from "@vitrumui/react/search";

const layers: GlassSearchLayer[] = [
  {
    value: "apps",
    label: "Apps",
    icon: <LayoutGrid />,
    items: [
      { value: "calendar", label: "Calendar", icon: <CalendarDays /> },
      { value: "camera", label: "Camera", icon: <Camera /> },
      { value: "calculator", label: "Calculator", icon: <Calculator /> },
      {
        value: "maps",
        label: "Maps",
        icon: <Map />,
        keywords: ["directions", "route"],
      },
      { value: "notes", label: "Notes", icon: <NotebookPen /> },
    ],
  },
  {
    value: "people",
    label: "People",
    icon: <Users />,
    items: [
      { value: "ada", label: "Ada Moreno", description: "Product design" },
      { value: "cem", label: "Cem Yılmaz", description: "Engineering" },
      { value: "mira", label: "Mira Osei", description: "Support" },
    ],
  },
];

// Tiles of flat colour behind the field, for the glass to bend.
const TILES = [
  "bg-sky-500",
  "bg-amber-400",
  "bg-emerald-500",
  "bg-rose-500",
  "bg-violet-500",
  "bg-orange-500",
  "bg-teal-500",
  "bg-fuchsia-500",
];

// Rest on the empty field and its layers bud off as droplets. Type "ca" and the best match
// completes the text; Tab takes it, Enter opens it.
export default function Demo() {
  const [opened, setOpened] = useState<string | null>(null);
  return (
    <div className="relative isolate flex h-96 w-full max-w-xl flex-col items-center pt-16">
      <div
        aria-hidden
        className="absolute inset-x-6 top-10 -z-10 grid grid-cols-4 gap-3"
      >
        {TILES.map((tile, i) => (
          <span key={i} className={`h-20 rounded-2xl ${tile}`} />
        ))}
      </div>
      <GlassSearch
        label="Search apps and people"
        placeholder="Search apps and people"
        layers={layers}
        onAction={(item, layer) => setOpened(`${layer.label}: ${item.label}`)}
        className="w-full max-w-sm"
      />
      <p className="absolute bottom-4 text-sm text-neutral-500 dark:text-neutral-400">
        {opened ? `Opened ${opened}` : "Nothing opened yet"}
      </p>
    </div>
  );
}
```

**Use it for**

- A command or launcher field that finds things of several kinds at once: apps, documents, people, settings.
- Results that come from a server, alongside ones you already have, with the slow ones joining as they arrive.

**Not for**

- Picking one value for a form from a short, fixed list: use Select.
- A handful of actions on a button, with nothing to type: use Menu.

```bash
npm install @vitrumui/react
```

> The listbox, highlight, keyboard, announcements and ARIA come from UI SDK (`@ui-sdk/core` and `@ui-sdk/react`). Searching and ranking happen in the component.

## Usage

```tsx
import { LayoutGrid, Users } from 'lucide-react'
import { GlassSearch } from '@vitrumui/react/search'

<GlassSearch
  label="Search apps and people"
  placeholder="Search apps and people"
  layers={[
    {
      value: 'apps',
      label: 'Apps',
      icon: <LayoutGrid />,
      items: [
        { value: 'calendar', label: 'Calendar' },
        { value: 'maps', label: 'Maps', keywords: ['directions'] },
      ],
    },
    {
      value: 'people',
      label: 'People',
      icon: <Users />,
      items: [{ value: 'ada', label: 'Ada Moreno', description: 'Product design' }],
    },
  ]}
  onAction={(item, layer) => open(layer.value, item.value)}
/>
```

Each layer has a `value`, a `label` and an `icon`. Typing ranks every layer's `items` together; a result's own `icon` replaces its layer's. `onAction` runs on Enter or a press, and the field empties once it has run.

## Examples

### A static layer and an async one

```tsx
import {
  FileText,
  FolderOpen,
  LayoutGrid,
  Music,
  Settings,
} from "lucide-react";

import {
  GlassSearch,
  type GlassSearchItem,
  type GlassSearchLayer,
} from "@vitrumui/react/search";

const FILES = [
  "Budget 2027.xlsx",
  "Brand guidelines.pdf",
  "Launch plan.docx",
  "Meeting notes – March.md",
  "Roadmap.key",
  "Team photo.png",
];

// A layer that answers later, like a server would: it waits, and stops as soon as the text
// changes (the signal aborts).
function findFiles(query: string, signal: AbortSignal) {
  return new Promise<GlassSearchItem[]>((resolve, reject) => {
    const timer = setTimeout(() => {
      const q = query.toLowerCase();
      resolve(
        FILES.filter((name) => name.toLowerCase().includes(q)).map((name) => ({
          value: name,
          label: name,
          description: "Shared drive",
        })),
      );
    }, 450);
    signal.addEventListener("abort", () => {
      clearTimeout(timer);
      reject(signal.reason);
    });
  });
}

const layers: GlassSearchLayer[] = [
  {
    value: "apps",
    label: "Apps",
    icon: <LayoutGrid />,
    items: [
      { value: "music", label: "Music", icon: <Music /> },
      { value: "settings", label: "Settings", icon: <Settings /> },
      { value: "files", label: "Files", icon: <FolderOpen /> },
    ],
  },
  {
    value: "files",
    label: "Files",
    icon: <FileText />,
    search: findFiles,
  },
];

// Apps are known up front and rank as you type; Files answer after a moment and join the list.
// Press the Files chip to search that layer alone.
export default function Demo() {
  return (
    <div className="relative isolate flex h-96 w-full max-w-xl flex-col items-center pt-16">
      <div
        aria-hidden
        className="absolute inset-0 -z-10 flex flex-col gap-4 py-8"
      >
        <span className="h-10 w-4/5 rounded-r-full bg-indigo-500" />
        <span className="h-10 w-3/5 self-end rounded-l-full bg-lime-400" />
        <span className="h-10 w-2/3 rounded-r-full bg-red-500" />
        <span className="h-10 w-1/2 self-end rounded-l-full bg-cyan-400" />
      </div>
      <GlassSearch
        label="Search apps and files"
        placeholder="Try “plan” or “music”"
        layers={layers}
        className="w-full max-w-sm"
      />
    </div>
  );
}
```

Give a layer `search(query, signal)` instead of `items` and it answers when its promise settles. The signal aborts as soon as the text changes, so stop the request there. Its results are ranked with the rest but not filtered again.

Return a promise from `onAction` and the chosen result stays pending until it settles. If it rejects, the text stays so the visitor can try again.

## API reference

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `layers` | `GlassSearchLayer[]` | — | Where to look: `{ value, label, icon, items?, search? }`. The first four become droplets when they fit; every layer that answers shows as a chip. |
| `label / placeholder` | `string / string` | — | The field's accessible name, and the hint shown while it is empty. A hint too long for the field scrolls through it. |
| `onAction` | `(item, layer) => unknown` | — | Runs when a result is chosen. Return a promise to keep the result pending until it settles. |
| `onQueryChange` | `(query: string) => void` | — | Called with the text on every change. |
| `defaultLayer / onLayerChange` | `string \| null / (layer: string \| null) => void` | `null` | The layer to search alone at first, and a callback when the visitor picks or releases one. |
| `layersAfter` | `number` | `2000` | How long the empty field waits under focus or the pointer before its layers bud off, in ms. |
| `arrive` | `boolean` | `false` | Play an arrival when the field mounts: it comes in a little large, out of a blur. For a field opened by a shortcut or in a dialog. |
| `empty / limit` | `ReactNode / number` | `'No results' / 50` | What shows when nothing matches, and the most results listed at once. |
| `material / tint / blur` | `'clear' \| 'tinted' / number / number` | `'clear' / 0.74 / 3.5` | The glass: clear glass frosts and bends what is behind it. Inside a VitrumProvider, `tint` and `blur` follow the theme unless you set them. |
| `className / ref / …props` | `string / Ref<HTMLDivElement> / div attributes` | — | Size the field with `className` (`w-full max-w-xl`); `ref` and other attributes go to its root. |

## Keyboard and accessibility

- The field is a combobox that controls a listbox. `aria-expanded` is true only while results are showing.
- Up and Down move the highlight; Enter runs the highlighted result. Tab, or Right Arrow at the end of the text, takes the suggested completion.
- Escape empties the field; pressed again, it releases the chosen layer. Backspace in an empty field releases it too.
- Droplets and chips are buttons named by their layer. While the droplets are melted into the field they leave the tab order.
- Matching ignores case and accents, so “sukru” finds “Şükrü”. The number of results is announced once typing pauses.
- With reduced motion everything appears at once. Without WebGL2 it is a frosted field with round buttons at its end.

Source: /docs/search
