# Get started

A cairn is a pile of stones that walkers add to, marking the way for whoever comes next.

Build a Vite and React app of three files that draws a map with the SDK's interface around it and a search box at the top, and run it on your machine.

The code on this page is compiled in CI against the built packages. It uses [`MapView`](https://docs.mapcairn.com/api/map/MapView.md), [`MapChrome`](https://docs.mapcairn.com/api/map/MapChrome.md), [`SearchPill`](https://docs.mapcairn.com/api/map/SearchPill.md), [`ServicesProvider`](https://docs.mapcairn.com/api/map/ServicesProvider.md), [`createFakeServices`](https://docs.mapcairn.com/api/core/createFakeServices.md) and [`createMapLibreMapAdapter`](https://docs.mapcairn.com/api/adapter-maplibre/createMapLibreMapAdapter.md).

## Before you start

You need Node 24 or newer, npm 11 or newer and a browser. The SDK needs React 19, `maplibre-gl` 6.18 or later, an ESM bundler that handles CSS imports (tested with Vite) and a TypeScript `moduleResolution` of `bundler`, `node16` or `nodenext`.

## Install

Make a Vite and React project in a new, empty folder that is not inside another project, then add the SDK:

```bash
npm create vite@latest my-map -- --template react-ts
cd my-map
npm install @mapcairn/sdk react react-dom maplibre-gl
```

The template adds files this app does not use. Delete `src/App.tsx`, `src/App.css` and `src/index.css`.

`@mapcairn/sdk` is the one package most apps need. The interface comes from its root, and the MapLibre adapter that draws the map comes from `@mapcairn/sdk/adapter-maplibre`. An adapter connects the SDK to one outside thing, here the MapLibre map engine; the SDK describes each such thing (the map, search, storage) as a port, an interface, and an adapter implements it. An app that never imports the subpath carries no map engine. `maplibre-gl` is a required peer, so a strict package manager wants it listed.

## Write the three files

Replace the template's `index.html` and `src/main.tsx` with these, and add `src/page.css`.

`index.html`:

```html
<!doctype html>
<html lang="en-GB">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>My map</title>
  </head>
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
  </body>
</html>
```

The SDK ships no base stylesheet, so `src/page.css` gives the map its height and removes the body margin. Without it the map is zero pixels high and nothing shows:

`src/page.css`:

```css
html,
body,
#root {
  height: 100%;
  margin: 0;
}
```

The entry file builds the app's adapters once, in `services` (the composition root: the one place that decides which adapter fills each port), and `ServicesProvider` hands them to the components. The MapLibre adapter needs the address of the engine's worker file, which in a Vite app is the bundler's own worker URL import:

`src/main.tsx`:

```tsx check
/// <reference types="vite/client" />
import "@mapcairn/sdk/styles.css";
import "./page.css";
import {
  createFakeServices,
  defaultBasemapFor,
  FocusRegistryProvider,
  LiveRegionProvider,
  MapChrome,
  MapView,
  SearchPill,
  ServicesProvider,
  TooltipProvider,
  useBasemaps,
  useMap,
  useTheme,
} from "@mapcairn/sdk";
import { createMapLibreMapAdapter } from "@mapcairn/sdk/adapter-maplibre";
import { type ReactNode, StrictMode } from "react";
import { createRoot } from "react-dom/client";

const services = createFakeServices({
  createMapAdapter: () =>
    createMapLibreMapAdapter({
      loadWorkerUrl: async () => {
        const worker = await import(
          "maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url"
        );
        return worker.default;
      },
    }),
});

function App() {
  return (
    <ServicesProvider services={services}>
      <LiveRegionProvider>
        <TooltipProvider>
          <MapScreen />
        </TooltipProvider>
      </LiveRegionProvider>
    </ServicesProvider>
  );
}

function MapScreen() {
  const basemaps = useBasemaps();
  const { resolved } = useTheme();
  return (
    <MapView
      initialView={{
        center: { lat: -33.92, lng: 18.42 },
        zoom: 11,
        bearing: 0,
      }}
      basemap={defaultBasemapFor(basemaps, resolved)}
      chrome={(background) => <Chrome background={background} />}
    />
  );
}

function Chrome({ background }: { readonly background: ReactNode }) {
  const map = useMap();
  return (
    <FocusRegistryProvider focusMap={map.focusMap}>
      <MapChrome
        background={background}
        modal={false}
        topStart={
          <SearchPill
            onSelect={(result) =>
              map.adapter?.setView({ center: result.centre, zoom: 14 })
            }
          />
        }
      />
    </FocusRegistryProvider>
  );
}

const root = document.getElementById("root");
if (!root) {
  throw new Error("The page has no #root element.");
}
createRoot(root).render(
  <StrictMode>
    <App />
  </StrictMode>,
);
```

`MapChrome` lays out the interface in slots, named places over the map such as `topStart` and `bottomEnd`. `LiveRegionProvider` adds the live region, a hidden area that screen readers read announcements from, and `TooltipProvider` serves the tooltips. [Embed a map](https://docs.mapcairn.com/guides/embed-a-map.md) explains the rest of the file.

## Add the font (optional)

The SDK's design uses IBM Plex Sans Variable. Without it the interface uses your system's font through the fallback stack, and that is a fine place to start. The SDK's `tokens.css` already declares a fallback face matched to Plex's metrics, so loading the font later moves the layout very little. To load Plex, add the font package and import its stylesheet in `src/main.tsx`, above the SDK's:

```bash
npm install @fontsource-variable/ibm-plex-sans
```

```ts check
import "@fontsource-variable/ibm-plex-sans/wght.css";
import "@mapcairn/sdk/styles.css";
```

The design uses the Latin and Latin Extended subsets at weights 400 and 600. The package's `wght.css` declares more subsets and weights, and the browser fetches only the files a page's text needs. Vite copies the font files into your build with the rest of your assets and rewrites the stylesheet to point at them. If you serve the page with a strict Content Security Policy, the files must come from your own origin (`font-src 'self'`) or from an origin your `font-src` allows; [Embed a map](https://docs.mapcairn.com/guides/embed-a-map.md) has a policy to start from. [Theme the interface](https://docs.mapcairn.com/tasks/theme-the-interface.md) covers the rest of the theme.

## Run it

```bash
npm run dev
```

Open the address Vite prints. You should see a map of Cape Town, in the light or dark basemap that matches your system theme, with a "Search places" box at the top left. You can drag and zoom the map, and use the arrow keys when it has focus. The app adds no zoom buttons or credits of its own; those are slots of `MapChrome` that you fill, as the [example app](https://github.com/MapCairn/mapcairn/blob/main/examples/embed/README.md) does. Search shows sample results, because the services here are the SDK's in-memory fakes. The only requests the page makes are for the basemap, from `https://tiles.openfreemap.org`.

This app shows no credits for the map data. The basemap's providers ask for them, so before you publish, add [`Attribution`](https://docs.mapcairn.com/api/map/Attribution.md) to the `bottomEnd` slot of `MapChrome`, with the basemap you passed `MapView`.

If the map stays blank, check that `src/page.css` is imported and that no other style sets the height of `#root`. [Troubleshooting](https://docs.mapcairn.com/troubleshooting.md) lists the messages, including the one for a browser with no WebGL 2 context, and the fix for each.

## Where to go next

- Each task has a page that builds on this app: [choose a basemap](https://docs.mapcairn.com/tasks/choose-a-basemap.md), [add a data layer](https://docs.mapcairn.com/tasks/add-a-layer.md), [draw and edit shapes](https://docs.mapcairn.com/tasks/draw-shapes.md), [theme the interface](https://docs.mapcairn.com/tasks/theme-the-interface.md), [switch between light and dark](https://docs.mapcairn.com/tasks/follow-the-system-theme.md), [translate the interface](https://docs.mapcairn.com/tasks/translate-the-interface.md) and [host the map on a server-rendered page](https://docs.mapcairn.com/tasks/host-on-a-server-rendered-page.md).
- [Embed a map](https://docs.mapcairn.com/guides/embed-a-map.md) says what each part of `main.tsx` does, how another bundler serves the worker file and which Content Security Policy to start from.
- [Extending MapCairn](https://docs.mapcairn.com/guides/extending.md) lists what an app provides around the SDK and how to add a basemap, a data layer, a search or routing provider, or an adapter of your own.
- The [API reference](https://docs.mapcairn.com/api.md) lists every export of the six packages.
- [Troubleshooting](https://docs.mapcairn.com/troubleshooting.md) lists the error messages the SDK throws, with the cause and the fix of each, and what to check when the map is blank.

## Status

The SDK is pre-1.0. While the version starts with `0.`, a minor release can contain a breaking change to the public API, and a patch release is not meant to. A `^0.1.0` range takes patch releases but not the next minor release.
