---
title: "Tools & defaults — Svelte"
description: "Change what each tool draws, add your own tools, and choose the cursor, the preview and what happens after drawing."
framework: "Svelte"
source: "https://www.embedpdf.com/docs/headless/svelte/annotations/tools"
---

# Tools & defaults

A tool says what a click or a drag on the page creates: the kind of annotation, its default
colors and sizes, the cursor, and whether a preview follows the pointer. Change the built-in
tools, or add your own, with the `tools` setting of `annotationPlugin()`:

```ts
annotationPlugin({
  tools: [
    { id: 'ink', defaults: { color: '#1e90ff', strokeWidth: 3 } },
    {
      id: 'arrow',
      extends: 'line',
      defaults: { lineEndings: { start: 'none', end: 'closed-arrow' } },
    },
  ],
});
```

The first entry changes the built-in `ink` tool. The second adds an `arrow` tool: a line with an
arrowhead. Activate a tool with `interaction.activateTool('arrow')`, as on the
[overview](../annotations#let-people-draw).

## Change the default color, width and font

A tool's `defaults` are the fields every annotation it draws starts with. They're the same fields
the annotation has, so the [engine's page for each kind](https://www.embedpdf.com/docs/engine/annotations) lists what you
can set:

```ts
annotationPlugin({
  tools: [
    { id: 'square', defaults: { color: '#dc143c', strokeWidth: 2, interiorColor: '#ffe4e1' } },
    { id: 'highlight', defaults: { color: '#90ee90', opacity: 0.6 } },
    { id: 'free-text', defaults: { fontFamily: 'helvetica', fontSize: 16, fontColor: '#1e1e1e' } },
    { id: 'note', defaults: { icon: 'help', color: '#ffd700' } },
  ],
});
```

Defaults can be any field except where the annotation goes (`box`, points, `rect`), since that
comes from the drawing. Flags are fields too: `{ id: 'note', defaults: { locked: true } }` makes
every new note locked.

A field the tool's kind doesn't have (a `fontSize` on a square) stops the app at startup with an
error that names the tool and the field.

## Make your own tool

`extends` starts from a tool and changes what you name. The new tool keeps everything else: its
kind, cursor, preview and behavior:

```ts
annotationPlugin({
  tools: [
    { id: 'red-pen', extends: 'ink', defaults: { color: '#ff0000', strokeWidth: 2 } },
    { id: 'marker', extends: 'ink-highlight', defaults: { color: '#ffa500' } },
    { id: 'todo', extends: 'note', defaults: { icon: 'key', contents: 'TODO' } },
    {
      id: 'docs-link',
      extends: 'link',
      defaults: { target: { kind: 'uri', uri: 'https://example.com/docs' } },
    },
  ],
});
```

Give a tool a label and an icon for your toolbar in `meta`. The plugin keeps it and never reads
it:

```ts
annotationPlugin({
  tools: [{ id: 'red-pen', extends: 'ink', meta: { label: 'Red pen', icon: 'pen' } }],
});
```

`annotation.tools.list()` returns every tool, so a toolbar can build itself from it.

**`App.svelte`**

```svelte
<script lang="ts" module>
  import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
  import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
  import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
  import { interactionPlugin } from '@embedpdf/svelte/interaction';
  import { AnnotationLayer, annotationPlugin } from '@embedpdf/svelte/annotation';
  import { localEngine } from '@embedpdf/engine';
  import Toolbar from './Toolbar.svelte';

  import '../custom-tools.css';

  const engine = localEngine();
  // A blue pen, an arrow, and three tools of your own. `meta` is yours: the toolbar reads its label.
  const plugins = [
    stagePlugin(),
    renderPlugin(),
    interactionPlugin(),
    annotationPlugin({
      tools: [
        { id: 'ink', defaults: { color: '#1e90ff', strokeWidth: 3 }, meta: { label: 'Blue pen' } },
        {
          id: 'arrow',
          extends: 'line',
          defaults: { lineEndings: { start: 'none', end: 'closed-arrow' } },
          meta: { label: 'Arrow' },
        },
        {
          id: 'red-pen',
          extends: 'ink',
          defaults: { color: '#ff0000', strokeWidth: 2 },
          meta: { label: 'Red pen' },
        },
        {
          id: 'marker',
          extends: 'ink-highlight',
          defaults: { color: '#ffa500' },
          meta: { label: 'Marker' },
        },
        {
          id: 'todo',
          extends: 'note',
          defaults: { icon: 'key', contents: 'TODO' },
          meta: { label: 'To do' },
        },
      ],
    }),
  ];

  const ebook = async (): Promise<OpenInput> => {
    const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
    return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
  };
</script>

<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
  <DocumentGate>
    {#snippet fallback()}
      <p class="loading">Loading…</p>
    {/snippet}
    <Toolbar />
    <Stage class="stage">
      <RenderLayer />
      <AnnotationLayer />
    </Stage>
  </DocumentGate>
</Viewer>
```

**`custom-tools.css`**

```css
/* Colors */
:root {
  --ink: #1a2748;
  --text: #3b4b7a;
  --muted: #5a6b92;
  --line: #e6eaf2;
  --line-strong: #dce8fc;
  --surface: #ffffff;
  --tint: #f3f7fe;
  --canvas: #f7f9fc;
  --accent: #054fb3;
  --focus: #7db6ff;
  --shadow: rgb(7 32 76 / 0.04);
}

/* The controls, above the pages */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

/* One choice of a few: the pressed button is the current one */
.segmented {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px;
  background: var(--tint);
  border: 1px solid var(--line);
  border-radius: 11px;
}

.segmented button {
  font: inherit;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}

.segmented button:hover {
  color: var(--ink);
}

.segmented button[aria-pressed='true'] {
  color: var(--accent);
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(7 32 76 / 0.1);
}

.segmented button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* The pages */
.stage {
  height: 420px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--canvas);
  box-shadow:
    0 1px 2px var(--shadow),
    0 18px 40px -28px rgb(7 32 76 / 0.28);
}

.loading {
  margin: 0;
  padding: 24px 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
```

**`Toolbar.svelte`**

```svelte
<!-- A toolbar that builds itself from the tools: every tool with a label gets a button. -->
<script lang="ts">
  import { usePageList } from '@embedpdf/svelte/runtime';
  import { useInteraction, useInteractionState } from '@embedpdf/svelte/interaction';
  import { useAnnotation, useAnnotationState } from '@embedpdf/svelte/annotation';

  const annotation = useAnnotation();
  const interaction = useInteraction();
  const tools = useInteractionState();
  const ready = useAnnotationState((state) => state.status === 'ready');
  const pages = usePageList();
  const labelled = $derived(
    annotation.tools.list().filter((tool) => typeof tool.meta?.label === 'string'),
  );
  let added = false;

  // On load: an arrow drawn with the arrow tool's defaults, and the arrow tool active.
  $effect(() => {
    const cover = pages.current[0]?.ref;
    if (!ready.current || !cover || added) return;
    added = true;
    void annotation.create(
      cover,
      { subtype: 'line', linePoints: { start: { x: 520, y: 120 }, end: { x: 470, y: 230 } } },
      undefined,
      { tool: 'arrow' },
    );
    interaction.activateTool('arrow');
  });
</script>

<div class="toolbar">
  <div class="segmented" role="group" aria-label="Tool">
    <button
      type="button"
      aria-pressed={tools.activeToolId === 'pointer'}
      onclick={() => interaction.activateTool('pointer')}
    >
      Select
    </button>
    {#each labelled as tool (tool.id)}
      <button
        type="button"
        aria-pressed={tools.activeToolId === tool.id}
        onclick={() => interaction.activateTool(tool.id)}
      >
        {String(tool.meta?.label)}
      </button>
    {/each}
  </div>
</div>
```

> **Try it:** every button but **Select** comes from a tool's `meta`. Draw with **Red pen**, then
> place a **To do** note.

Tools can also be added while the app runs. `register()` returns a function that removes the tool
again:

```ts
const remove = annotation.tools.register({
  id: 'green-pen',
  extends: 'ink',
  defaults: { color: '#00a000' },
});
```

## Let people change the defaults

A color picker or a width slider in your toolbar changes a tool's defaults for the drawings that
follow:

**`ink-color.svelte`**

```svelte
<script lang="ts">
  import { useAnnotation, useAnnotationDefaults } from '@embedpdf/svelte/annotation';

  const annotation = useAnnotation();
  const defaults = useAnnotationDefaults('ink');
</script>

<input
  type="color"
  value={defaults.current.color}
  oninput={(event) => annotation.tools.updateDefaults('ink', { color: event.currentTarget.value })}
/>
```

`useAnnotationDefaults()` updates when the defaults change, so the picker always
shows the current color. To build a whole style panel for any tool, ask for its fields; they come in
the same shape as a [style panel for the selection](selecting#a-style-panel):

```ts
const { properties, values } = annotation.tools.getProperties('ink'); // color, opacity, strokeWidth, …
```

Defaults belong to the person drawing: they're kept in this viewer and never shared with others on
the same document.

### Remember them

To keep a person's choices for next time, save them when they change:

**`remember.svelte`**

```svelte
<script lang="ts">
  import { useAnnotationEvent } from '@embedpdf/svelte/annotation';

  useAnnotationEvent(
    (annotation) => annotation.tools.onDefaultsChanged,
    ({ toolId, defaults }) => {
      localStorage.setItem(`tool:${toolId}`, JSON.stringify(defaults));
    },
  );
</script>
```

Then pass them back at startup:

```ts
annotationPlugin({
  tools: ['ink', 'highlight'].map((id) => ({
    id,
    defaults: JSON.parse(localStorage.getItem(`tool:${id}`) ?? '{}'),
  })),
});
```

**`App.svelte`**

```svelte
<script lang="ts" module>
  import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
  import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
  import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
  import { interactionPlugin } from '@embedpdf/svelte/interaction';
  import { AnnotationLayer, annotationPlugin } from '@embedpdf/svelte/annotation';
  import { localEngine } from '@embedpdf/engine';
  import PenStyle from './PenStyle.svelte';
  import RememberDefaults from './RememberDefaults.svelte';
  import StartWithThePen from './StartWithThePen.svelte';

  import '../tool-defaults.css';

  const engine = localEngine();
  // The pen starts with what the reader picked last time: see RememberDefaults.svelte.
  const plugins = [
    stagePlugin(),
    renderPlugin(),
    interactionPlugin(),
    annotationPlugin({
      tools: [{ id: 'ink', defaults: JSON.parse(localStorage.getItem('tool:ink') ?? '{}') }],
    }),
  ];

  const ebook = async (): Promise<OpenInput> => {
    const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
    return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
  };
</script>

<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
  <DocumentGate>
    {#snippet fallback()}
      <p class="loading">Loading…</p>
    {/snippet}
    <StartWithThePen />
    <RememberDefaults />
    <PenStyle />
    <Stage class="stage">
      <RenderLayer />
      <AnnotationLayer />
    </Stage>
  </DocumentGate>
</Viewer>
```

**`tool-defaults.css`**

```css
/* Colors */
:root {
  --ink: #1a2748;
  --text: #3b4b7a;
  --muted: #5a6b92;
  --line: #e6eaf2;
  --line-strong: #dce8fc;
  --surface: #ffffff;
  --tint: #f3f7fe;
  --canvas: #f7f9fc;
  --accent: #054fb3;
  --focus: #7db6ff;
  --shadow: rgb(7 32 76 / 0.04);
}

/* The controls, above the pages */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

/* Colors to pick from: the pressed one is the current one */
.swatches {
  display: inline-flex;
  gap: 6px;
}

.swatch {
  width: 26px;
  height: 26px;
  padding: 0;
  border: 2px solid var(--surface);
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--line);
  cursor: pointer;
}

.swatch[aria-pressed='true'] {
  box-shadow: 0 0 0 2px var(--ink);
}

.swatch:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* Any color */
.color {
  width: 34px;
  height: 30px;
  padding: 2px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}

/* A slider, with its label and its value */
.range {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 550;
  color: var(--ink);
}

.range input {
  width: 110px;
  accent-color: var(--accent);
}

.readout {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

/* The pages */
.stage {
  height: 420px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--canvas);
  box-shadow:
    0 1px 2px var(--shadow),
    0 18px 40px -28px rgb(7 32 76 / 0.28);
}

.loading {
  margin: 0;
  padding: 24px 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
```

**`PenStyle.svelte`**

```svelte
<!-- The pen's color and width, for the strokes that follow. -->
<script lang="ts">
  import { useAnnotation, useAnnotationDefaults } from '@embedpdf/svelte/annotation';

  const COLORS = ['#e5484d', '#1e90ff', '#30a46c', '#1a2748'];

  const annotation = useAnnotation();
  const defaults = useAnnotationDefaults('ink');
</script>

<div class="toolbar">
  <div class="swatches" role="group" aria-label="Pen color">
    {#each COLORS as color (color)}
      <button
        type="button"
        class="swatch"
        aria-label={color}
        aria-pressed={defaults.current.color === color}
        style:background={color}
        onclick={() => annotation.tools.updateDefaults('ink', { color })}
      ></button>
    {/each}
  </div>
  <input
    type="color"
    class="color"
    aria-label="Any color"
    value={defaults.current.color ?? '#000000'}
    oninput={(event) =>
      annotation.tools.updateDefaults('ink', { color: event.currentTarget.value })}
  />
  <label class="range">
    Width
    <input
      type="range"
      min={1}
      max={12}
      value={defaults.current.strokeWidth ?? 1}
      oninput={(event) =>
        annotation.tools.updateDefaults('ink', { strokeWidth: Number(event.currentTarget.value) })}
    />
    <output class="readout">{defaults.current.strokeWidth} pt</output>
  </label>
</div>
```

**`RememberDefaults.svelte`**

```svelte
<!-- Every change is kept for next time. -->
<script lang="ts">
  import { useAnnotationEvent } from '@embedpdf/svelte/annotation';

  useAnnotationEvent(
    (annotation) => annotation.tools.onDefaultsChanged,
    ({ toolId, defaults }) => {
      localStorage.setItem(`tool:${toolId}`, JSON.stringify(defaults));
    },
  );
</script>
```

**`StartWithThePen.svelte`**

```svelte
<!-- On load: the pen is active, with a stroke drawn in its current style. -->
<script lang="ts">
  import { usePageList } from '@embedpdf/svelte/runtime';
  import { useInteraction } from '@embedpdf/svelte/interaction';
  import { useAnnotation, useAnnotationState } from '@embedpdf/svelte/annotation';

  const annotation = useAnnotation();
  const interaction = useInteraction();
  const ready = useAnnotationState((state) => state.status === 'ready');
  const pages = usePageList();
  let added = false;

  $effect(() => {
    const cover = pages.current[0]?.ref;
    if (!ready.current || !cover || added) return;
    added = true;
    const wave = Array.from({ length: 24 }, (_, i) => ({
      x: 300 + i * 10,
      y: 540 + Math.sin(i / 2) * 14,
    }));
    void annotation.create(cover, { subtype: 'ink', inkList: [wave] }, undefined, { tool: 'ink' });
    interaction.activateTool('ink');
  });
</script>
```

> **Try it:** pick a color and a width, and draw. Reload the page: the pen still has them.

## What happens after drawing

By default the new annotation is selected, so its handles and your menu appear at once, and the
tool stays active, so people can draw another. Change it for every tool, or for one:

```ts
annotationPlugin({
  afterCreate: { select: false, tool: 'stay' }, // every tool: keep drawing, don't select
  tools: [
    { id: 'stamp', afterCreate: { select: true, tool: 'default' } }, // one stamp, then back
  ],
});
```

| Setting    | What it does                                                                                  | Default  |
| :--------- | :-------------------------------------------------------------------------------------------- | :------- |
| `select`   | Select the new annotation.                                                                    | `true`   |
| `tool`     | `'stay'` keeps the tool active. `'default'` goes back to the default tool, usually `pointer`. | `'stay'` |
| `editText` | For text boxes: start typing in the new box right away.                                       | `true`   |

A tool's own `afterCreate` wins over the one for every tool.

The setting can change while the app runs. A "keep tool" button in your toolbar is one call:

```ts
annotation.updateSettings({ afterCreate: { tool: keepTool ? 'stay' : 'default' } });
```

**`App.svelte`**

```svelte
<script lang="ts" module>
  import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
  import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
  import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
  import { interactionPlugin } from '@embedpdf/svelte/interaction';
  import { AnnotationLayer, annotationPlugin } from '@embedpdf/svelte/annotation';
  import { localEngine } from '@embedpdf/engine';
  import AfterDrawing from './AfterDrawing.svelte';

  import '../after-create.css';

  const engine = localEngine();
  const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin()];

  const ebook = async (): Promise<OpenInput> => {
    const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
    return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
  };
</script>

<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
  <DocumentGate>
    {#snippet fallback()}
      <p class="loading">Loading…</p>
    {/snippet}
    <AfterDrawing />
    <Stage class="stage">
      <RenderLayer />
      <AnnotationLayer />
    </Stage>
  </DocumentGate>
</Viewer>
```

**`after-create.css`**

```css
/* Colors */
:root {
  --ink: #1a2748;
  --text: #3b4b7a;
  --muted: #5a6b92;
  --line: #e6eaf2;
  --line-strong: #dce8fc;
  --surface: #ffffff;
  --tint: #f3f7fe;
  --canvas: #f7f9fc;
  --accent: #054fb3;
  --focus: #7db6ff;
  --shadow: rgb(7 32 76 / 0.04);
}

/* The controls, above the pages */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  line-height: 1.1;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 12px;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 1px 2px var(--shadow);
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease;
}

.button:hover {
  background: var(--tint);
  border-color: var(--line-strong);
}

.button:active {
  transform: translateY(1px);
}

.button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.button[aria-pressed='true'] {
  color: var(--accent);
  background: var(--tint);
  border-color: var(--line-strong);
}

.button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

/* A switch: a checkbox with its label */
.check {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 550;
  color: var(--ink);
  cursor: pointer;
}

.check input {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--accent);
}

/* Pushes what follows to the right */
.spacer {
  flex: 1 1 auto;
}

.readout {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

/* The pages */
.stage {
  height: 420px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--canvas);
  box-shadow:
    0 1px 2px var(--shadow),
    0 18px 40px -28px rgb(7 32 76 / 0.28);
}

.loading {
  margin: 0;
  padding: 24px 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
```

**`AfterDrawing.svelte`**

```svelte
<script lang="ts">
  import { onMount } from 'svelte';
  import { useInteraction, useInteractionState } from '@embedpdf/svelte/interaction';
  import { useAnnotation, useAnnotationSettings } from '@embedpdf/svelte/annotation';

  const annotation = useAnnotation();
  const interaction = useInteraction();
  const tools = useInteractionState();
  const afterCreate = useAnnotationSettings((settings) => settings.afterCreate);

  // The rectangle tool is active on load.
  onMount(() => {
    interaction.activateTool('square');
  });
</script>

<div class="toolbar">
  <button
    type="button"
    class="button"
    aria-pressed={tools.activeToolId === 'square'}
    onclick={() => interaction.activateTool('square')}
  >
    Rectangle
  </button>
  <label class="check">
    <input
      type="checkbox"
      checked={afterCreate.current.select}
      onchange={(event) =>
        annotation.updateSettings({ afterCreate: { select: event.currentTarget.checked } })}
    />
    Select it
  </label>
  <label class="check">
    <input
      type="checkbox"
      checked={afterCreate.current.tool === 'stay'}
      onchange={(event) =>
        annotation.updateSettings({
          afterCreate: { tool: event.currentTarget.checked ? 'stay' : 'default' },
        })}
    />
    Keep the tool
  </label>
  <button type="button" class="button" onclick={() => annotation.resetSettings()}>Reset</button>
  <span class="spacer"></span>
  <output class="readout">{tools.activeToolId === 'square' ? 'Drawing' : 'Selecting'}</output>
</div>
```

> **Try it:** draw a rectangle, then turn off **Keep the tool** and draw another: the tool goes back
> to selecting.

Annotations your code creates are never selected unless you ask for it; see
[Create an annotation](with-code#create-an-annotation).

## Click or drag

Most drawing tools also work with a single click: it places an annotation of a default size where
you click. `clickCreate` sets that size, or turns it off:

```ts
annotationPlugin({
  tools: [
    { id: 'square', clickCreate: { width: 120, height: 80 } },
    { id: 'line', clickCreate: { length: 100 } },
    { id: 'circle', clickCreate: false }, // drag only
  ],
});
```

| Tool                 | A click makes                                         |
| :------------------- | :---------------------------------------------------- |
| `square`, `circle`   | 80 × 60 points, centered on the click                 |
| `line`               | 80 points long, pointing right, centered on the click |
| `free-text`          | 180 × 40 points, its top-left at the click            |
| `note`, `attachment` | A 20 × 20 icon                                        |
| `stamp`              | The stamp, 150 points wide, centered on the click     |

`anchor` says which part of it lands on the click. A box takes `'center'` (the default) or
`'top-left'`; a line takes `'center'` (the default), `'start'` or `'end'`. A line's `rotation` turns it
clockwise from pointing right, in degrees. An arrow whose tip lands where you click, pointing down:

```ts
annotationPlugin({
  tools: [{ id: 'arrow', clickCreate: { length: 80, rotation: 90, anchor: 'end' } }],
});
```

**`App.svelte`**

```svelte
<script lang="ts" module>
  import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
  import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
  import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
  import { interactionPlugin } from '@embedpdf/svelte/interaction';
  import { AnnotationLayer, annotationPlugin } from '@embedpdf/svelte/annotation';
  import { localEngine } from '@embedpdf/engine';
  import Toolbar from './Toolbar.svelte';

  import '../click-create.css';

  const engine = localEngine();
  // What one click makes: a 120 × 80 rectangle, an arrow pointing down at the click, and no circle.
  const plugins = [
    stagePlugin(),
    renderPlugin(),
    interactionPlugin(),
    annotationPlugin({
      tools: [
        { id: 'square', clickCreate: { width: 120, height: 80 } },
        {
          id: 'arrow',
          extends: 'line',
          defaults: { lineEndings: { start: 'none', end: 'closed-arrow' } },
          clickCreate: { length: 80, rotation: 90, anchor: 'end' },
        },
        { id: 'circle', clickCreate: false }, // drag only
      ],
    }),
  ];

  const ebook = async (): Promise<OpenInput> => {
    const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
    return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
  };
</script>

<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
  <DocumentGate>
    {#snippet fallback()}
      <p class="loading">Loading…</p>
    {/snippet}
    <Toolbar />
    <Stage class="stage">
      <RenderLayer />
      <AnnotationLayer />
    </Stage>
  </DocumentGate>
</Viewer>
```

**`click-create.css`**

```css
/* Colors */
:root {
  --ink: #1a2748;
  --text: #3b4b7a;
  --muted: #5a6b92;
  --line: #e6eaf2;
  --line-strong: #dce8fc;
  --surface: #ffffff;
  --tint: #f3f7fe;
  --canvas: #f7f9fc;
  --accent: #054fb3;
  --focus: #7db6ff;
  --shadow: rgb(7 32 76 / 0.04);
}

/* The controls, above the pages */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

/* One choice of a few: the pressed button is the current one */
.segmented {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px;
  background: var(--tint);
  border: 1px solid var(--line);
  border-radius: 11px;
}

.segmented button {
  font: inherit;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}

.segmented button:hover {
  color: var(--ink);
}

.segmented button[aria-pressed='true'] {
  color: var(--accent);
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(7 32 76 / 0.1);
}

.segmented button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.hint {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

/* The pages */
.stage {
  height: 420px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--canvas);
  box-shadow:
    0 1px 2px var(--shadow),
    0 18px 40px -28px rgb(7 32 76 / 0.28);
}

.loading {
  margin: 0;
  padding: 24px 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
```

**`Toolbar.svelte`**

```svelte
<script lang="ts">
  import { onMount } from 'svelte';
  import { useInteraction, useInteractionState } from '@embedpdf/svelte/interaction';

  const TOOLS = [
    { id: 'square', label: 'Rectangle', hint: 'A click makes 120 × 80 points' },
    { id: 'arrow', label: 'Arrow', hint: 'A click points an arrow down at it' },
    { id: 'circle', label: 'Circle', hint: 'Drag only: a click does nothing' },
  ];

  const interaction = useInteraction();
  const tools = useInteractionState();
  const active = $derived(TOOLS.find((tool) => tool.id === tools.activeToolId));

  // The rectangle tool is active on load: click the page.
  onMount(() => {
    interaction.activateTool('square');
  });
</script>

<div class="toolbar">
  <div class="segmented" role="group" aria-label="Tool">
    {#each TOOLS as tool (tool.id)}
      <button
        type="button"
        aria-pressed={tools.activeToolId === tool.id}
        onclick={() => interaction.activateTool(tool.id)}
      >
        {tool.label}
      </button>
    {/each}
  </div>
  <p class="hint">{active?.hint ?? 'Pick a tool, then click the page'}</p>
</div>
```

> **Try it:** click the page with each tool. The arrow's tip lands where you click.

What a click makes always stays on the page: near an edge, it's moved inside.

## The cursor

While a tool is active, the pointer shows the tool's cursor. Over an annotation the viewer switches
to a move or resize cursor by itself, and over text to a text cursor, so people always see what a
click will do. You choose the cursor for the rest of the page. There are three steps, from simple
to fully yours.

**A standard cursor.** Any CSS cursor name:

```ts
annotationPlugin({ tools: [{ id: 'square', cursor: 'cell' }] });
```

**An image.** `svgCursor()`, from `@embedpdf/svelte/interaction`, turns an SVG into a cursor. The
SVG needs a `width` and a `height`, and `hotspot` is the point in it that clicks:

```ts
annotationPlugin({
  tools: [{ id: 'ink', cursor: svgCursor({ svg: penIcon, hotspot: { x: 2, y: 22 } }) }],
});
```

**An image that follows the defaults.** `useToolCursor()` sets a tool's cursor from a component, so
it can change with the tool's color:

**`ink-cursor.svelte`**

```svelte
<script lang="ts">
  import { useAnnotationDefaults } from '@embedpdf/svelte/annotation';
  import { useToolCursor } from '@embedpdf/svelte/interaction';

  import { penIcon } from './icons';

  const defaults = useAnnotationDefaults('ink');

  // A function, so the cursor follows the color
  useToolCursor(() => ({
    toolId: 'ink',
    cursors: { crosshair: { svg: penIcon(defaults.current.color), hotspot: { x: 2, y: 22 } } },
  }));
</script>
```

`cursors` replaces cursors by name: `crosshair` is the ink tool's own cursor. Name `text` too to
replace the text cursor a markup tool shows over text. The cursor goes back to the tool's own when
the component is removed.

**`App.svelte`**

```svelte
<script lang="ts" module>
  import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
  import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
  import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
  import { interactionPlugin } from '@embedpdf/svelte/interaction';
  import { AnnotationLayer, annotationPlugin } from '@embedpdf/svelte/annotation';
  import { localEngine } from '@embedpdf/engine';
  import InkCursor from './InkCursor.svelte';
  import Toolbar from './Toolbar.svelte';

  import '../cursor.css';

  const engine = localEngine();
  // A standard cursor for the rectangle tool: any CSS cursor name.
  const plugins = [
    stagePlugin(),
    renderPlugin(),
    interactionPlugin(),
    annotationPlugin({ tools: [{ id: 'square', cursor: 'cell' }] }),
  ];

  const ebook = async (): Promise<OpenInput> => {
    const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
    return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
  };
</script>

<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
  <DocumentGate>
    {#snippet fallback()}
      <p class="loading">Loading…</p>
    {/snippet}
    <InkCursor />
    <Toolbar />
    <Stage class="stage">
      <RenderLayer />
      <AnnotationLayer />
    </Stage>
  </DocumentGate>
</Viewer>
```

**`cursor.css`**

```css
/* Colors */
:root {
  --ink: #1a2748;
  --text: #3b4b7a;
  --muted: #5a6b92;
  --line: #e6eaf2;
  --line-strong: #dce8fc;
  --surface: #ffffff;
  --tint: #f3f7fe;
  --canvas: #f7f9fc;
  --accent: #054fb3;
  --focus: #7db6ff;
  --shadow: rgb(7 32 76 / 0.04);
}

/* The controls, above the pages */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  line-height: 1.1;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 12px;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 1px 2px var(--shadow);
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease;
}

.button:hover {
  background: var(--tint);
  border-color: var(--line-strong);
}

.button:active {
  transform: translateY(1px);
}

.button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.button[aria-pressed='true'] {
  color: var(--accent);
  background: var(--tint);
  border-color: var(--line-strong);
}

.button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

/* Colors to pick from: the pressed one is the current one */
.swatches {
  display: inline-flex;
  gap: 6px;
}

.swatch {
  width: 26px;
  height: 26px;
  padding: 0;
  border: 2px solid var(--surface);
  border-radius: 50%;
  box-shadow: 0 0 0 1px var(--line);
  cursor: pointer;
}

.swatch[aria-pressed='true'] {
  box-shadow: 0 0 0 2px var(--ink);
}

.swatch:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* The pages */
.stage {
  height: 420px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--canvas);
  box-shadow:
    0 1px 2px var(--shadow),
    0 18px 40px -28px rgb(7 32 76 / 0.28);
}

.loading {
  margin: 0;
  padding: 24px 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
```

**`InkCursor.svelte`**

```svelte
<!-- The pen cursor follows the pen's color. -->
<script lang="ts">
  import { useToolCursor } from '@embedpdf/svelte/interaction';
  import { useAnnotationDefaults } from '@embedpdf/svelte/annotation';

  // A pen, 24 × 24, in a color: its tip is the bottom-left corner.
  const penIcon = (color: string) =>
    `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">` +
    `<path d="M3 21l1.5-5.5L16 4l4 4L8.5 19.5z" fill="${color}" stroke="#ffffff" stroke-width="1.5" stroke-linejoin="round"/>` +
    `</svg>`;

  const defaults = useAnnotationDefaults('ink');

  // A function, so the cursor follows the color.
  useToolCursor(() => ({
    toolId: 'ink',
    cursors: {
      crosshair: { svg: penIcon(defaults.current.color ?? '#000000'), hotspot: { x: 2, y: 22 } },
    },
  }));
</script>
```

**`Toolbar.svelte`**

```svelte
<script lang="ts">
  import { onMount } from 'svelte';
  import { useInteraction, useInteractionState } from '@embedpdf/svelte/interaction';
  import { useAnnotation, useAnnotationDefaults } from '@embedpdf/svelte/annotation';

  const COLORS = ['#e5484d', '#1e90ff', '#30a46c', '#1a2748'];

  const annotation = useAnnotation();
  const interaction = useInteraction();
  const tools = useInteractionState();
  const defaults = useAnnotationDefaults('ink');

  // The pen is active on load: move the pointer over the page.
  onMount(() => {
    interaction.activateTool('ink');
  });

  function pick(swatch: string) {
    annotation.tools.updateDefaults('ink', { color: swatch });
    interaction.activateTool('ink');
  }
</script>

<div class="toolbar">
  <button
    type="button"
    class="button"
    aria-pressed={tools.activeToolId === 'ink'}
    onclick={() => interaction.activateTool('ink')}
  >
    Pen
  </button>
  <div class="swatches" role="group" aria-label="Pen color">
    {#each COLORS as swatch (swatch)}
      <button
        type="button"
        class="swatch"
        aria-label={swatch}
        aria-pressed={defaults.current.color === swatch}
        style:background={swatch}
        onclick={() => pick(swatch)}
      ></button>
    {/each}
  </div>
  <button
    type="button"
    class="button"
    aria-pressed={tools.activeToolId === 'square'}
    onclick={() => interaction.activateTool('square')}
  >
    Rectangle
  </button>
</div>
```

> **Try it:** move the pointer over the page, then pick another color. **Rectangle** shows the
> standard `cell` cursor.

## Show a preview before placing

A **preview** (a ghost) is a see-through copy of what a click would place, following the pointer. It
shows exactly where the annotation will land and how big it will be:

```ts
annotationPlugin({
  tools: [
    { id: 'square', ghost: true },
    { id: 'note', ghost: { opacity: 0.3 } },
    { id: 'todo', extends: 'note', ghost: false },
  ],
});
```

The preview is the annotation the click will make, drawn with the tool's current defaults: its
color, its width, an arrow's head, a form field's border and background. Change a default and the
preview changes with it. It's half see-through unless you set `opacity`; `--epdf-ghost-opacity`
sets it from CSS for every tool (see [Colors & theming](../ui/theming)).

**`App.svelte`**

```svelte
<script lang="ts" module>
  import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
  import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
  import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
  import { interactionPlugin } from '@embedpdf/svelte/interaction';
  import { AnnotationLayer, annotationPlugin } from '@embedpdf/svelte/annotation';
  import { localEngine } from '@embedpdf/engine';
  import Toolbar from './Toolbar.svelte';

  import '../ghost.css';

  const engine = localEngine();
  // A preview for the rectangle (off by default for tools you drag), and a fainter one for notes.
  const plugins = [
    stagePlugin(),
    renderPlugin(),
    interactionPlugin(),
    annotationPlugin({
      tools: [
        { id: 'square', ghost: true },
        { id: 'note', ghost: { opacity: 0.3 } },
      ],
    }),
  ];

  const ebook = async (): Promise<OpenInput> => {
    const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
    return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
  };
</script>

<script lang="ts">
  // `--epdf-ghost-opacity` from CSS wins over each tool's own opacity, for every tool.
  let opacity = $state<number | null>(null);
</script>

<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
  <DocumentGate>
    {#snippet fallback()}
      <p class="loading">Loading…</p>
    {/snippet}
    <Toolbar bind:opacity />
    <Stage class="stage" style={opacity === null ? undefined : `--epdf-ghost-opacity: ${opacity}`}>
      <RenderLayer />
      <AnnotationLayer />
    </Stage>
  </DocumentGate>
</Viewer>
```

**`ghost.css`**

```css
/* Colors */
:root {
  --ink: #1a2748;
  --text: #3b4b7a;
  --muted: #5a6b92;
  --line: #e6eaf2;
  --line-strong: #dce8fc;
  --surface: #ffffff;
  --tint: #f3f7fe;
  --canvas: #f7f9fc;
  --accent: #054fb3;
  --focus: #7db6ff;
  --shadow: rgb(7 32 76 / 0.04);
}

/* The controls, above the pages */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

/* One choice of a few: the pressed button is the current one */
.segmented {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px;
  background: var(--tint);
  border: 1px solid var(--line);
  border-radius: 11px;
}

.segmented button {
  font: inherit;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}

.segmented button:hover {
  color: var(--ink);
}

.segmented button[aria-pressed='true'] {
  color: var(--accent);
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(7 32 76 / 0.1);
}

.segmented button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

/* A slider, with its label and its value */
.range {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 550;
  color: var(--ink);
}

.range input {
  width: 110px;
  accent-color: var(--accent);
}

.readout {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  line-height: 1.1;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 8px 12px;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 1px 2px var(--shadow);
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease;
}

.button:hover {
  background: var(--tint);
  border-color: var(--line-strong);
}

.button:active {
  transform: translateY(1px);
}

.button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.button[aria-pressed='true'] {
  color: var(--accent);
  background: var(--tint);
  border-color: var(--line-strong);
}

.button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

/* The pages */
.stage {
  height: 420px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--canvas);
  box-shadow:
    0 1px 2px var(--shadow),
    0 18px 40px -28px rgb(7 32 76 / 0.28);
}

.loading {
  margin: 0;
  padding: 24px 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
```

**`Toolbar.svelte`**

```svelte
<script lang="ts">
  import { onMount } from 'svelte';
  import { useInteraction, useInteractionState } from '@embedpdf/svelte/interaction';

  const TOOLS = [
    { id: 'note', label: 'Note' },
    { id: 'square', label: 'Rectangle' },
  ];

  let { opacity = $bindable() }: { opacity: number | null } = $props();

  const interaction = useInteraction();
  const tools = useInteractionState();

  // The note tool is active on load: move the pointer over the page.
  onMount(() => {
    interaction.activateTool('note');
  });
</script>

<div class="toolbar">
  <div class="segmented" role="group" aria-label="Tool">
    {#each TOOLS as tool (tool.id)}
      <button
        type="button"
        aria-pressed={tools.activeToolId === tool.id}
        onclick={() => interaction.activateTool(tool.id)}
      >
        {tool.label}
      </button>
    {/each}
  </div>
  <label class="range">
    From CSS
    <input
      type="range"
      min={0.1}
      max={0.9}
      step={0.1}
      value={opacity ?? 0.5}
      oninput={(event) => (opacity = Number(event.currentTarget.value))}
    />
    <output class="readout">
      {opacity === null ? "each tool's own" : `${Math.round(opacity * 100)}%`}
    </output>
  </label>
  <button type="button" class="button" disabled={opacity === null} onclick={() => (opacity = null)}>
    Reset
  </button>
</div>
```

> **Try it:** move the pointer over the page: the note follows it, faintly. Then move the slider.

It shows only where a click would place something. Over an annotation the click selects it, and
over text a text tool selects text, so the preview hides there. It hides too when the person may
not create annotations. A tool whose click makes nothing, like ink, a polygon, or a tool with
`clickCreate: false`, never shows one. While you press, the preview stays, since letting go is
still a click; once you drag, the drawing takes its place.

It's on by default for tools you use by clicking: stamps, notes, attachments, and the form
plugin's field tools, which show the field a click places (a radio button round, as it will be).
It's off for tools you mostly drag, like shapes and lines, since the size usually comes from the
drag. `ghost: true` turns it on for them.

The cursor and the preview do different jobs. The cursor says *which* tool is active; the preview
says *where* the annotation will land. You can use either, or both.

## Stamps and files

The `stamp` and `attachment` tools ask for a file after the click.

The file dialog comes from a small function: call it once, in a component that's always
mounted, such as your toolbar:

**`file-picker.svelte`**

```svelte
<script lang="ts">
  import { useFilePickerProvider } from '@embedpdf/svelte/annotation';

  useFilePickerProvider();
</script>

<div role="toolbar"><!-- your tools --></div>
```

**`App.svelte`**

```svelte
<script lang="ts" module>
  import { DocumentGate, Viewer, type OpenInput } from '@embedpdf/svelte/runtime';
  import { Stage, stagePlugin } from '@embedpdf/svelte/stage';
  import { RenderLayer, renderPlugin } from '@embedpdf/svelte/render';
  import { interactionPlugin } from '@embedpdf/svelte/interaction';
  import { AnnotationLayer, annotationPlugin } from '@embedpdf/svelte/annotation';
  import { localEngine } from '@embedpdf/engine';
  import StartWithAFile from './StartWithAFile.svelte';
  import Toolbar from './Toolbar.svelte';

  import '../file-picker.css';

  const engine = localEngine();
  const plugins = [stagePlugin(), renderPlugin(), interactionPlugin(), annotationPlugin()];

  const ebook = async (): Promise<OpenInput> => {
    const response = await fetch('https://snippet.embedpdf.com/ebook.pdf');
    return { kind: 'bytes', id: 'ebook', bytes: new Uint8Array(await response.arrayBuffer()) };
  };
</script>

<Viewer {engine} {plugins} initialDocuments={[{ source: ebook }]}>
  <DocumentGate>
    {#snippet fallback()}
      <p class="loading">Loading…</p>
    {/snippet}
    <StartWithAFile />
    <Toolbar />
    <Stage class="stage">
      <RenderLayer />
      <AnnotationLayer />
    </Stage>
  </DocumentGate>
</Viewer>
```

**`file-picker.css`**

```css
/* Colors */
:root {
  --ink: #1a2748;
  --text: #3b4b7a;
  --muted: #5a6b92;
  --line: #e6eaf2;
  --line-strong: #dce8fc;
  --surface: #ffffff;
  --tint: #f3f7fe;
  --canvas: #f7f9fc;
  --accent: #054fb3;
  --focus: #7db6ff;
  --shadow: rgb(7 32 76 / 0.04);
}

/* The controls, above the pages */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

/* One choice of a few: the pressed button is the current one */
.segmented {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 3px;
  background: var(--tint);
  border: 1px solid var(--line);
  border-radius: 11px;
}

.segmented button {
  font: inherit;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}

.segmented button:hover {
  color: var(--ink);
}

.segmented button[aria-pressed='true'] {
  color: var(--accent);
  background: var(--surface);
  box-shadow: 0 1px 2px rgb(7 32 76 / 0.1);
}

.segmented button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.hint {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

/* The pages */
.stage {
  height: 420px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--canvas);
  box-shadow:
    0 1px 2px var(--shadow),
    0 18px 40px -28px rgb(7 32 76 / 0.28);
}

.loading {
  margin: 0;
  padding: 24px 0;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}
```

**`StartWithAFile.svelte`**

```svelte
<!-- On load: a text file pinned to the cover, and the stamp tool active. -->
<script lang="ts">
  import { usePageList } from '@embedpdf/svelte/runtime';
  import { useInteraction } from '@embedpdf/svelte/interaction';
  import { useAnnotation, useAnnotationState } from '@embedpdf/svelte/annotation';

  const annotation = useAnnotation();
  const interaction = useInteraction();
  const ready = useAnnotationState((state) => state.status === 'ready');
  const pages = usePageList();
  let added = false;

  $effect(() => {
    const cover = pages.current[0]?.ref;
    if (!ready.current || !cover || added) return;
    added = true;
    const notes = new File(['Questions for the next review.'], 'notes.txt', { type: 'text/plain' });
    void annotation.create(
      cover,
      { subtype: 'file-attachment', rect: { x: 470, y: 232, width: 20, height: 20 } },
      { file: notes },
    );
    interaction.activateTool('stamp');
  });
</script>
```

**`Toolbar.svelte`**

```svelte
<!-- The toolbar is always mounted, so it gives the stamp and attachment tools their file dialog. -->
<script lang="ts">
  import { useInteraction, useInteractionState } from '@embedpdf/svelte/interaction';
  import { useFilePickerProvider } from '@embedpdf/svelte/annotation';

  const TOOLS = [
    { id: 'pointer', label: 'Select' },
    { id: 'stamp', label: 'Stamp' },
    { id: 'attachment', label: 'Attach a file' },
  ];

  useFilePickerProvider();
  const interaction = useInteraction();
  const tools = useInteractionState();
</script>

<div class="toolbar" role="toolbar">
  <div class="segmented" role="group" aria-label="Tool">
    {#each TOOLS as tool (tool.id)}
      <button
        type="button"
        aria-pressed={tools.activeToolId === tool.id}
        onclick={() => interaction.activateTool(tool.id)}
      >
        {tool.label}
      </button>
    {/each}
  </div>
  <p class="hint">
    {#if tools.activeToolId === 'stamp'}
      Click the page, then pick a PNG or a JPEG
    {:else if tools.activeToolId === 'attachment'}
      Click the page, then pick any file
    {/if}
  </p>
</div>
```

> **Try it:** click the page with **Stamp**, and pick a PNG or a JPEG. The paper clip on the cover
> is a file attached from code.

To pick from your own library or a cloud drive instead, pass a function. It gets the tool and the
spot, and returns the file, or `null` to cancel:

**`file-picker-custom.svelte`**

```svelte
<script lang="ts">
  import { useFilePickerProvider } from '@embedpdf/svelte/annotation';

  import { openStampLibrary } from './stamp-library';

  useFilePickerProvider(async ({ toolId, page, point }) => {
    if (toolId === 'stamp') return { data: await openStampLibrary() };
    return null;
  });
</script>

<div role="toolbar"><!-- your tools --></div>
```

A stamp tool can also carry fixed bytes, so a click places it at once:

```ts
annotationPlugin({
  tools: [{ id: 'approved', extends: 'stamp', source: { kind: 'bytes', source: approvedPng } }],
});
```

For libraries of stamps, see [Stamps](stamps).

## Methods

What `useAnnotation()` gives you for tools and stamps: its `tools` and `stamps` parts. The rest is on
[Annotations in code](with-code#methods).

| Method                                  | What it does                                                                                                                                                                                             |
| :-------------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tools.list()`                          | Every tool, the built-in ones and yours, so a toolbar can build itself from it. [Make your own tool](#make-your-own-tool)                                                                                |
| `tools.get(id)`                         | One tool, or `null`                                                                                                                                                                                      |
| `tools.register(tool)`                  | Adds a tool while the app runs, or replaces the one with the same id, and returns a function that removes it                                                                                             |
| `tools.getDefaults(id)`                 | The fields a tool's next annotation starts with. In a component, use `useAnnotationDefaults(id)`. [Let people change the defaults](#let-people-change-the-defaults)                                      |
| `tools.updateDefaults(id, changes)`     | Changes a tool's defaults for the drawings that follow. They stay in this viewer and are never shared.                                                                                                   |
| `tools.getProperties(id)`               | What a style panel for the tool shows: `{ properties, values, mixed }`, the same shape as for the [selection](selecting#a-style-panel)                                                                   |
| `stamps.arm(stamp)`, `stamps.disarm()`  | Arms a stamp's bytes so the next click places them, as the `stamp` tool does after picking a file, or disarms them. For stamps from a library, use [Stamps](stamps).                                     |
| `stamps.isArmed()`                      | Whether a stamp is armed                                                                                                                                                                                 |
| `stamps.place(stamp, { page, center })` | Places a stamp's bytes with its middle on `center`, without a click. Resolves `{ annotation }`.                                                                                                          |
| `setFilePickerProvider(provider)`       | Sets the function that gives the `stamp` and `attachment` tools their file, and returns a function that removes it. In a component, use `useFilePickerProvider()`. [Stamps and files](#stamps-and-files) |

Every method that returns a promise also takes a `signal` in its options, to cancel it.

## Events

**`defaults-changed.svelte`**

```svelte
<script lang="ts">
  import { useAnnotationEvent } from '@embedpdf/svelte/annotation';

  import { saveStyle } from './styles';

  useAnnotationEvent(
    (annotation) => annotation.tools.onDefaultsChanged,
    ({ toolId, defaults }) => saveStyle(toolId, defaults),
  );
</script>
```

| Event                     | Fires when                                           | Carries              |
| :------------------------ | :--------------------------------------------------- | :------------------- |
| `tools.onDefaultsChanged` | A tool's defaults changed, from your UI or your code | `toolId`, `defaults` |

## Good to know

### Rotated pages

On a rotated page, what a tool places looks the way its author saw it. Stamps and text boxes are
turned so they read upright. A clicked square or circle keeps its `clickCreate` size as the page
shows (it isn't turned, its sides swap), and a clicked line points the way its `clickCreate` says.
`upright: false` turns that off; `upright: true` turns it on for your own tools.

### Touch

On a touch screen, drawing tools take a single finger: it draws, and two fingers still scroll and
zoom. Click tools (notes, stamps) place with a tap, and a drag still scrolls.

### Every option

| Option        | What it is                                                                              |
| :------------ | :-------------------------------------------------------------------------------------- |
| `id`          | The tool's name, used with `interaction.activateTool(id)`.                              |
| `extends`     | A tool to start from.                                                                   |
| `subtype`     | The kind of annotation it draws, for a tool that doesn't extend another.                |
| `defaults`    | The fields every new annotation starts with.                                            |
| `cursor`      | The cursor while the tool is active.                                                    |
| `ghost`       | A preview that follows the pointer: `true`, `false` or `{ opacity }`.                   |
| `clickCreate` | What a single click makes, and which part lands on the click, or `false` for drag only. |
| `afterCreate` | Select the new annotation? Keep the tool? Start typing?                                 |
| `upright`     | Turn new annotations upright on rotated pages.                                          |
| `source`      | For stamp tools: fixed bytes, or ask for a file.                                        |
| `meta`        | Your own data, such as a label and an icon for a toolbar.                               |
