> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> To set up Hatchmark for a user, follow https://hatchmark.dev/docs/get-started/agent-setup.md. Inside a Hatchmark project, prefer `hatchmark docs`: it prints the docs of the installed version.

# Embedding

> The <hatchmark-chart> and <hatchmark-scope> elements, the @hatchmark/react components and hooks, and drawing a chart on any JavaScript server.

Charts for your own pages, drawn from [serve's API](/docs/reference/api.md) by the same renderer as the dashboard
pages. [Embed charts](/docs/guides/embed.md) walks through a first chart.

| Package | Use it for |
| - | - |
| The served script | Any page: `<script type="module" src="<serve>/_hatchmark/static/elements.js">` |
| `@hatchmark/elements` | The same elements from npm, bundled with your app |
| `@hatchmark/react` | React components and hooks, built on `@hatchmark/elements` |

The packages are released with `hatchmark` at the same version; install the version your serve runs,
so the payloads and the renderer match. The served script always matches its serve. Palettes other
than the default, map and cohort drawing, and map boundaries load on first use.

## Elements

### `<hatchmark-scope>`

Charts inside share its endpoint and theme, filter each other with picks and show one build.

| Attribute or property | Value |
| - | - |
| `endpoint` | serve's API, such as `https://bi.example.com/api/v1` |
| `theme` | `auto` (default), `light` or `dark` |
| `picks` | The picks on screen by dashboard, then pick id: `{ sales: { channel: "Social" } }`, as JSON in the attribute |

While a pick holds, the scope shows it as a chip with a button that clears it, above its charts or
floating at the bottom of a phone's screen, as a dashboard's header does; style it with
`hatchmark-scope::part(picks)`.

Setting `picks` makes the scope controlled: a click or the chip's clear button then only fires
`hatchmark-pick`, and the app sets `picks` to apply it. A page drawn on the server with picks sets
the `picks` attribute, so the charts take over the drawn payloads without fetching them again.

### `<hatchmark-chart>`

| Attribute or property | Value |
| - | - |
| `block` | `<dashboard>.<block id>` |
| `period` | For a period filter with the id `period`: a preset key, or `{ start, end }` dates as a property; set another id in `filters` |
| `filters` (property) | Values by filter id, as the page URL takes them ([Serve API](/docs/reference/api.md#filters)) |
| `payload` (property) | A payload fetched elsewhere: the chart draws it and doesn't fetch while its block, period, filters and its scope's picks match |
| `endpoint` | serve's API, for a chart outside a scope |
| `theme` | `light` or `dark`; without it the chart follows its scope or the page |

A chart draws at the width its container gives it and redraws when the width changes. It takes the
dashboard's phone layout when the screen is a phone's, as a dashboard does, whatever the chart's own
width. Its card fills the element's height, so charts in a row your layout stretches line up.

### Events

Both bubble out of shadow roots.

| Event | On | `detail` |
| - | - | - |
| `hatchmark-pick` | scope | `dashboard`, every pick on screen in `picks`, the `source.block`, and `cleared` when the filters left a pick with no rows |
| `hatchmark-state` | chart | `status` (`loading`, `ready` or `error`), `error` with its `code` and `message`, and `build` |

## React

| Export | Does |
| - | - |
| `HatchmarkProvider` | A `<hatchmark-scope>`: `endpoint`, `theme`, `picks`, `onPick` |
| `HatchmarkChart` | A `<hatchmark-chart>`: `block`, `period`, `filters`, `payload`, `endpoint`, `theme`, `onState` |
| `useHatchmarkBlock(block, options)` | A block's `payload`, `status` and `error` for your own UI |
| `fetchBlock(block, options)` (`/server`) | A payload fetched on the server, for `payload` |

`fetchBlock` takes `period`, `filters`, `picks`, `endpoint` (default `HATCHMARK_URL`), `headers` and
`signal`, and throws `HatchmarkError` with the API's `code` when the API answers an error. A
`HatchmarkChart` given a `payload` is drawn on the server, except maps with boundaries, which draw
once their boundaries load.

## Types

`/api/v1/types.d.ts` holds the build's blocks as TypeScript, except those of a dashboard on which
no block reads data, which have no payload. Saved anywhere your app compiles, it
types `block`, `period` and `filters` on `HatchmarkChart`, `useHatchmarkBlock` and `fetchBlock`, and
`block` on `<hatchmark-chart>`, so a name the build doesn't have fails to compile:

```ts theme={null}
declare global {
  interface HatchmarkRegister {
    blocks: {
      "sales.revenue": {
        kind: "line";
        height: "default";
        period: "last_90_days" | "year_to_date" | { start: string; end: string };
        filters: {
          grain?: "week:sunday" | "month" | undefined;
          channel?: string | readonly string[] | undefined;
        };
      };
    };
  }
}
```

* `period` lists the dashboard's presets, plus `{ start, end }` and `"<first>..<last>"` when it
  offers custom ranges. It is `never` on a dashboard without a period filter. A period filter with
  another id than `period` is set through `filters` under its id instead.
* `filters` takes grain keys as written in the build, a segment filter as a comma-separated string,
  and select values as strings, since the data decides them. A filter set to `undefined` is left
  out of the request.
* A block whose id comes from its position is marked `@deprecated`: the id changes when blocks
  move, so give it an `id:` in its dashboard file.

Without the file, every block name, period and filter type-checks. A serve build also writes it to
`_hatchmark/api/types.d.ts` in its output, so an app in the same repository can include it from
there.

## Drawing on any JavaScript server

`@hatchmark/elements/server` draws a chart to HTML with no DOM, for servers that aren't React:

```ts theme={null}
import { renderChartHtml } from "@hatchmark/elements/server";

const html = renderChartHtml(payload, { theme: "dark" });
```

It returns a `<hatchmark-chart>` with its drawing in a declarative shadow root. A map with boundaries
needs them in `geographies`, from `await geographiesFor(payload)`.

`payloadShadowHtml(payload)` returns a shadow root with only the payload, for a chart the browser
should draw without fetching it again.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.