# Search

> Canonical URL: https://docs.mountaya.com/en/sdks/typescript/search/
> Last updated: 2026-11-27
> Rules for agents: https://docs.mountaya.com/llms.txt#rules-for-agents

Search places, itineraries and zones from a Mountaya map, handle what the reader picks, or build a search box of your own.

`SearchControl` is one search box over places, points of interest, trails, itineraries, zones and collections. It searches through the map's client, and highlights what the reader picks.

## Search places, itineraries and zones

Put `SearchControl` in the stack's children:

```tsx
import { MapControls, MapView, SearchControl } from "@mountaya/elements-react";

<MapView client={client}>
  <MapControls position="top-left">
    <SearchControl />
  </MapControls>
</MapView>;
```

Search runs on the Data API, so the map's client needs a [session token](https://docs.mountaya.com/en/sdks/typescript/authentication.md) beside the key. With one, the box needs no other prop.

- `SearchControl` is not a flag on the stack: it is a panel-width bar, not a square chip. It positions nothing itself; composed into `<MapControls>` children (also as `MapControls.Search`) it takes the stack's corner. Outside a stack it takes a `className` for your layout.
- With a map in context and no other prop, each round goes through the map's own `search`, scoped to the visible area, and a round that finds nothing there widens to the whole world once. Without a map and without `onSearch` it is inert.
- It renders one list across every kind of result, in the order `itineraries`, `places`, `ways`, `pois`, `zones`, `collections`, with filter chips (`all`, `itineraries`, `places`, `zones`). A failed branch gets a line with a retry, and the rest of the list stays.
- Every Data API call needs a session token beside the publishable key; a client holding the key alone is refused before anything is sent, with `MountayaConfigurationError`. Under a token minted without `user_id`, the itineraries, zones and collections listed are what your organization publishes.
- `disabled` renders the box inert. The provider's `language` sets the language the Data API answers in, through `dataLanguageFor` (English where the API carries none); `language` on the control overrides it.

## Configure the search

Pick the kinds of result, their order, how many of each, and the language they answer in:

```tsx
<SearchControl language="FR" limit={5} targets={["places", "itineraries", "zones"]} />;
```

The list shows places first, then itineraries, then zones, five of each at most, named in French.

- `targets` takes any of `SEARCH_TARGETS` (`itineraries`, `places`, `ways`, `pois`, `zones`, `collections`), every one by default; the sections render in the order listed. `limit` is a whole number from 1 to 50 per target (`SEARCH_LIMIT_MAX`), the API's own by default.
- `language` is the Data API's spelling of a language, `FR` for `fr` (a `DataLanguage`). Left out, it is the provider's `language` (`fr`, the elements' and the map's spelling) through `dataLanguageFor`, English where the API carries no answers in it: `<MountayaElementsProvider language="fr">` sets it for every element at once.
- `units` (`UnitFormatOptions`, `{ ...defaultUnits, length: "metric" }` for instance) states every figure in the list, an itinerary's length as much as the distance to a hut. `wording` is a `SearchWording` (`@mountaya/elements/search`): `resolveLabel({ kind, value })` answers the word a category (`placeCategory`, `poiCategory`) or a way's tag (`highway`, `sacScale`, `surface`) reads as, or `null` for its English title.
- `query` and `viewportScope` are each controlled by their value: supplied, the application owns it and `onQueryChange` or `onViewportScopeChange` asks for the change; with the handler alone the control keeps its own state and the handler observes. Pass a value without its handler to freeze it. `viewportScope` starts `true`, restricted to the visible area.
- `onSearch` replaces the transport (`SearchFetch`, `(input, { signal }) => Promise<SearchResult>`), for an application that sends its reads through a mutation layer of its own. `onError` hears each branch whose request failed, always an `Error`.
- `strings` overrides the control's own words (`search` section); `style` and `className` theme it.

## Handle a pick

Left alone, a pick moves the camera onto the result and highlights it on the map. Pass `onSelect` to decide yourself:

```tsx
<SearchControl
  onSelect={(row) => {
    if (row.payload.kind === "itinerary") {
      void map.setItinerary(row.payload.itineraryId, { fit: true });
    }
  }}
/>;
```

Each row hands back a `payload` that says what it is: a place, a trail, an itinerary, a zone.

- Unset, a pick does what `pickTarget(row.payload)` of `@mountaya/elements/search` answers: the camera frames the result's extent, or flies to its point at a fixed zoom where it has none, and `map.setSearchHighlight(mark)` draws it above everything else, in `--mtya-accent`. A collection has no place of its own and moves nothing. One result is highlighted at a time; `setSearchHighlight(null)` takes it down.
- `onSelect` suppresses both the camera move and the highlight; the handler decides. Call `pickTarget(row.payload)` yourself to keep either for the kinds you do not handle.
- A `SearchRow` carries `id`, `title`, `matched` (the title's ranges that matched, found without regard to case or accents), `secondary` (its second line) and `payload`. Payload kinds: `place` (`point`, `bounds`), `poi` (`point`, `category`), `way` (`anchor`, `coordinates`, `bounds`), `itinerary` (`itineraryId`, `start`, `bounds`), `zone` (`zoneId`, `point`, `bounds`) and `collection` (`collectionId`, `name`, `description`, `isPrivate`).
- The control drops no marker: it takes no `resultMarker` prop, and `@mountaya/elements/search` exports no `SEARCH_RESULT_MARKER_ID`. The pick is drawn in the map's search highlight slot, apart from `setHighlight`, so lighting a feature leaves the picked result where it is.

## Build your own search box

`useSearch` runs the same search and hands you the rows, for markup of your own:

```tsx
import { useState } from "react";
import { pickTarget, searchListState } from "@mountaya/elements/search";
import { useMountayaMap, useSearch } from "@mountaya/elements-react";

export const PlaceSearch = () => {
  const map = useMountayaMap();
  const [query, setQuery] = useState("");
  const { snapshot } = useSearch({
    fetch: map === null ? undefined : (input, options) => map.search(input, options),
    language: "FR",
    limit: 5,
    open: query !== "",
    query,
    targets: ["places", "itineraries", "zones"],
    viewportScope: false,
  });
  const { rows } = searchListState(snapshot, "all");

  return (
    <div>
      <input onChange={(event) => setQuery(event.target.value)} value={query} />
      <ul>
        {rows.map((row) => (
          <li key={row.id}>
            <button onClick={() => map?.setSearchHighlight(pickTarget(row.payload).mark)} type="button">
              {row.title}
            </button>
          </li>
        ))}
      </ul>
    </div>
  );
};
```

The pause between keystrokes, the cancelled rounds and the fallback to the whole world are handled for you.

- `useSearch({ fetch, query, open, viewportScope, targets, limit, language, viewport, wording, onError })` answers `{ snapshot, retry }`. With no `fetch` it is inert and every section stays empty. `open` gates the live area refresh alone: a scoped round re-runs as the map pans only while the results are looked at.
- It is a binding over `SearchEngine` of `@mountaya/elements/search`, which holds the debounce (`SEARCH_DEBOUNCE_MS`), the abort of a round a newer keystroke replaced, the live area refresh and the widen-once fallback, and names no framework: `getSnapshot` and `subscribe` are `useSyncExternalStore`'s two arguments, so Vue or Svelte wrap the same pair. Subscribing starts the work and the last unsubscribe stops it.
- `searchListState(snapshot, filter)` answers what a list shows under a filter: `rows`, `failed` (a line per failed branch), `nothingFound`, `fallbackNote` (the round widened from the visible area) and `status` (what a status line announces). Gate "nothing found" on `nothingFound`, never on empty sections: a scoped round that finds nothing widens and asks again.
- A snapshot names the query it answers (`snapshot.query`, trimmed); through a typing pause it answers the query it settled on, not the text in the box. `useSearch` draws a skeleton until the two agree.
- `splitByRanges(row.title, row.matched ?? [])` slices the title into matched and unmatched parts for emphasis. The row builders (`placeRow`, `poiRow`, `wayRow`, `itineraryRow`, `zoneRow`, `collectionRow`) answer a `SearchRow` or `null` for a result with nowhere to go. All of `@mountaya/elements/search` is renderer-free.
- Outside React, `map.search(input, { signal })` is the Data API's search passed straight through; it draws nothing. A map client builds a `viewport` with `createSearchViewport(client)`.

## Next steps

- [Controls and panels](https://docs.mountaya.com/en/sdks/typescript/controls.md): The stack the search box joins.
- [Show a map](https://docs.mountaya.com/en/sdks/typescript/map.md): Draw what a reader picked.
- [Data API search](https://docs.mountaya.com/en/apis/data/places.md): The query behind the box.
