# Components

Components are fenced code blocks with a name, like ```` ```chart ````. Hatch shows the result, and Obsidian or GitHub show the plain text, which still reads fine.

## Writing one

- Type `/` and pick one (Chart, Progress bar, Tabs, Steps, Columns, Cards, Button, Table of contents, Post list, Form). You get a working example to edit.
- Or type ```` ``` ```` at the start of a line. Block names come up as you type.
- Click a component to edit its source. Click elsewhere to see the result.
- Suggestions come up as you type keys and values. Press Ctrl+Space on an empty line to see them all. Enter or Tab picks one, Escape closes the list.
- Problems get a wavy underline, and the reason is listed under the source.

## The blocks

### chart

The same format as the Obsidian Charts plugin, so charts work in both apps.

````md
```chart
type: bar
labels: [Mon, Tue, Wed]
series:
  - title: Pages read
    data: [12, 30, 8]
```
````

- `type`: `bar`, `line` or `pie`.
- `labels`: names along the bottom.
- `series`: one `title` and `data` each. A pie chart uses the first series.
- `title` and `beginAtZero` are optional.
- Hover a chart to see each value, like the tooltip in most chart tools. This works on published sites too.

A chart can count a collection instead of listing numbers:

````md
```chart
type: bar
collection: Books.base
x: status
y: pages
aggregate: average
```
````

- `collection` is the `.base` file to count.
- `x` is the property to group by. `y` is the property to add up. Without `y`, it counts notes.
- `aggregate` is how `y` is added up: `sum` (the default), `average`, `min`, `max` or `count`.
- `view` picks one of the collection's views. Without `x`, the view's own chart settings are used.
- The chart redraws as your notes change. On a published site it counts published notes only.

### progress

````md
```progress
title: Chapters written
value: 7
max: 12
```
````

- `value` is how much is done. `max` is the total (default 100).
- `title` is shown above the bar. `label` still works, as an older name for it.

### tabs

Each `tab: Name` line starts a tab. Everything under it is Markdown.

````md
```tabs
tab: Mac
Install with **brew**.
tab: Linux
Install with apt.
```
````

A tab can hold a code block. Write the outer fence with four backticks so the inner one doesn't close it. When every tab holds only a code block, the tabs show as one code box with the names along its top, for the same command in several languages:

`````md
````tabs
tab: npm
```sh
npm install hatch
```
tab: pnpm
```sh
pnpm add hatch
```
````
`````

### steps

Numbered steps for a guide. Each `step: Title` line starts one, and everything under it is Markdown. A step can hold code too (use four backticks on the outer fence, as for tabs).

````md
```steps
step: Install the app
Download it and open it.

step: Make your first note
Press **New note** and start typing.
```
````

### columns

Content side by side: a picture next to text, or two paragraphs. Each `column:` line starts one, and everything under it is Markdown. `column: 2` makes a column twice as wide as a plain one. Up to 4 columns; on a phone they stack. Two pictures alone don't need this: written on one line, they already sit side by side.

````md
```columns
column: 2
![[porch.jpg]]

column:
The porch faces south, so it gets the afternoon sun.
```
````

### preview

Shows Markdown or HTML the way it looks, in a frame, with its source folded under View code. For documenting what something looks like. Everything inside is Markdown, and a code block inside needs four backticks on the outer fence.

````md
```preview
> [!tip] Previews
> Readers see this as it looks, and its source under View code.
```
````

### demo

A working page from your vault in a frame, like a built component example. The page and the files next to it (its scripts, styles and pictures) are published with the site, and its scripts run in a sandbox: they can't reach the site, its cookies or the reader's storage. The folder must hold only the built example: a folder with notes in it, or the vault's top, isn't published. In the app the block shows where the demo goes. The page gets `?theme=dark` when the reader's site is dark. A menu or popover that opens past the frame's bottom grows the frame while it's open. Build examples as classic scripts: module scripts need CORS in the sandbox, which only public sites on Hatch hosting send.

| Key | What it does |
| --- | --- |
| `src` | An HTML page in your vault, like `"[[previews/button/index.html]]"`. Required |
| `height` | How tall the frame is, in pixels, 80 to 1200. Default: 280 |
| `title` | What screen readers call the demo. Default: Demo |

````md
```demo
src: "[[previews/button/index.html]]"
height: 240
```
````

### files

Folders and files as a tree. Write a list: a name ending in `/` is a folder, the items indented under it are inside it, `**name**` marks one to notice, and text after ` # ` is a note beside it. Folders open and close.

````md
```files
- src/
  - components/
    - **button.tsx** # the one you add
  - app.tsx
- package.json
```
````

### cards

Linked cards in a grid, for pointing readers to the next pages.

````md
```cards
cols: 2
items:
  - title: Quickstart
    text: Set up in five minutes.
    link: "[[Quickstart]]"
  - title: API reference
    text: Every endpoint.
    link: https://example.com/api
```
````

- `items` lists the cards. Each has a `title`, and an optional `text` and `link` (a web address or a `[[note]]` in quotes).
- `cols` is how many cards sit in a row on wide screens, 1 to 4 (default 2). Phones show one.
- `look` set to `list` draws them as quiet rows instead of boxes: the title over its line, one under another. Good for a list of projects on a home page.

### posts

Where a published blog or portfolio lists its posts, newest first. Put it in the home note to choose where the list goes: the page then leaves out the list it adds at the end.

````md
```posts
title: Writing
limit: 10
```
````

- `title` is shown above the list (none by default).
- `limit` is how many posts to show (all by default).
- In the editor it marks where the list will be. On any site that isn't a blog or portfolio it stays a note that says so.

### Code blocks with a file name

Not a component, but handy in docs: put a file name after the language and it shows above the code.

````md
```js app.js
console.log('hi')
```
````

`title="app.js"` works too.

### button

````md
```button
text: Read the guide
link: "[[Welcome]]"
style: secondary
```
````

- `text` is what the button says.
- `link` is a web address or a `[[note]]`. Put a `[[note]]` in quotes.
- `style` is `primary` (the default) or `secondary`.

### toc

Lists the note's headings. `depth` is the deepest level shown (default 3), and `title` replaces "Contents".

### form

````md
```form
title: Get in touch
folder: Leads
fields:
  - Name*
  - Email (email)*
  - Topic (Sales | Support)
  - Message (long)
button: Send
thanks: Thanks. We'll reply within a day.
```
````

- `folder` is where responses are saved. `fields` lists one field per line.
- `title`, `button` and `thanks` are optional.
- `align` places the form: `left` (the default), `center`, `right`, or `full` for the whole width of the text.
- In a note, the form is a working preview: you can type and pick, but nothing is sent.
- Field types and where responses go are in `forms.md`.

## Your own components

You can add components of your own, like a carousel or a pull quote. They show up in the / menu next to the built-in ones, get the same suggestions and checks as you type, and work on published sites.

- The quickest way: open Settings, then Your components, and pick Make one with the chat. Describe what you want ("Make a component that shows a quote with who said it"). The chat writes it, checks it, and shows it to you to accept.
- Each component is one file, `.hatch/components/<name>.yaml`. The file name is the block's name. Change the file and every note using it redraws.
- Nothing ships pre-made. Ask the chat for what you want, for example "Make a carousel component for the pictures in Projects/Cabin.assets". It reads the guide, writes the file, checks it, and proposes it. Accept it, then type / and its name in any note. Ask it to put one in a note too.

A carousel's arrows (or a swipe, or the arrow keys once it has focus) move one picture at a time. That scrolling is the one thing a component can do.

### What a component file holds

````yaml
title: Pull quote
description: A quote that stands out, with who said it.
icon: ❝
keywords: quote citation testimonial
keys:
  quote:
    type: text
    required: true
    doc: What they said
  by:
    type: text
    doc: Who said it
example: |
  quote: Simple things should be simple.
  by: Alan Kay
template: |
  <figure class="quote">
    <blockquote>{{quote}}</blockquote>
    {{#if by}}<figcaption>{{by}}</figcaption>{{/if}}
  </figure>
style: |
  .quote { margin: 0; padding: 16px 20px; border-left: 2px solid var(--h-border-strong); }
  blockquote { margin: 0; font-size: 1.15em; }
  figcaption { margin-top: 8px; color: var(--h-text-muted); font-size: var(--h-text-sm); }
````

- `keys` are what a block takes. Types: `text`, `number`, `boolean`, `enum` (with `values`, lowercase so they work as classes), `list`, `link`, `image` and `items` (a list of maps with their own `keys`). A key can be `required` and have a `default` of its own type.
- Only these fields are read. A misspelled one (`descripton`, `styles`) is an error that names the field it meant.
- `template` is HTML with holes: `{{key}}`, `{{#each key}}...{{/each}}` and `{{#if key}}...{{else}}...{{/if}}`.
- `style` is CSS for the component only. Colors and fonts come from the theme (`var(--h-text)`, `var(--h-surface)` and the rest), so it matches every theme, light and dark.
- Problems are listed in Settings with the lines around the bad one, numbered and marked (`packages/blocks/src/codeFrame.ts`, like Vite's error overlay), and a Fix with the chat button. The chat and MCP tools get the same lines as text. A block using a broken component says what's wrong instead of drawing. The chat and the MCP server won't save a component while anything is wrong, including design warnings like a picture without alt text, a button without a label, bold text above 500, or a transition over 300ms.

### What a component can't do

Nothing in a component runs, so a component from someone else's vault is as safe as a note:
- No scripts, event handlers, forms, frames, style attributes or `<style>` tags. Only a short list of tags and attributes.
- Values are always escaped. Links and pictures go through the same checks as the rest of Hatch.
- CSS is scoped to the component, can't load anything (`url()`, `@import`) and can't use colors outside the theme.
- A component is clipped to its own box, so it can never cover the app or the page around it.
- The one behavior is scrolling: a button with `data-hb-scroll="prev"` or `"next"` scrolls the element with `data-hb-track`.

The full rules, with every allowed tag, attribute and token, are in the guide the chat and the MCP server get (`componentMakingGuide` in `packages/blocks/src/makeGuide.ts`). It is built from the checker's own lists, so it can't drift from what Hatch accepts.

## Editing a component

- Click a component to see its source under it. The source is colored like other code.
- To close the source, press Escape, use the Done button in its corner, or click anywhere else.
- Escape puts the caret after the component.

## Code blocks and tables

- Code blocks get a copy button in the top right corner when you hover them, in notes and on sites.
- Hover a table to get its controls:
  - The grip above a column inserts, aligns, moves or deletes it.
  - The grip beside a row inserts, moves or deletes it. The header row stays first.
  - The + on the right edge adds a column, and the + along the bottom adds a row.
- A wide table scrolls sideways and keeps its first column in place.
- Column widths aren't saved, since Markdown tables have no place for them.

## Formatting and links

- Select text to get a toolbar over it: bold, italic, strikethrough, code, highlight and link.
- Cmd+K (Ctrl+K) with text selected opens the link field. Paste a web address, or type a note's name to link to that note. The selected words stay as the link's text.
- With the caret in a link, the toolbar shows where it goes, with Edit, Open and Remove.
- With nothing selected, Cmd+K still opens search.

## Pasting from Word, Google Docs and spreadsheets

Paste as usual. Hatch cleans up what those apps put on the clipboard:

- Bold, italic, strikethrough, links and code-style fonts are kept. Underline, colors, fonts and sizes are dropped.
- A Google Doc no longer arrives all in bold.
- Headings keep their level.
- Word lists become real lists, nested the way they were, numbered or bulleted.
- Empty spacer paragraphs and Word's hidden markup are removed.
- Cells copied from Excel, Google Sheets or Numbers become a table. The first row is the header. Merged cells are split into plain cells.
- Columns that were centered or right-aligned stay that way. Numbers from a spreadsheet stay right-aligned.
- A table that asks for more than 50,000 cells comes in as plain lines of text, so a broken page can't freeze Hatch.
- Code blocks keep their line breaks and their language when the page names one.
- Only web and mail links are kept, with their titles. Scripts and styles are never kept.
- Copying between Hatch notes keeps everything as it was. Scripts and addresses that could run code (`javascript:`, `vbscript:`, `file:` and `data:` other than a picture) are still removed, since any web page can pretend to be Hatch.
- When the clipboard holds both text and a picture of it (Excel does this), the text wins.

The cleanup is in `packages/editor/src/pasteClean.ts`.

## For agents

Agents get the exact syntax from the Hatch MCP server, so they don't have to guess:
- `components` returns the rules and every key of every component, with an example each. Pass `names` for just the ones you need.
- `check_component` checks a block the way the editor does and lists any problems.
- `make_component` explains how to make a new component, and `save_component` checks one and saves it to `.hatch/components/`. The chat in the app has the same guide, and proposes the file (`propose_component`) for the person to accept.

Both come from the same definitions the editor uses (`packages/blocks/src/defs.ts`), and a test fails if this page misses a component or key.

## Videos, maps and designs

Write a link as an image, the way Obsidian does. Pasting a YouTube link on its own line does this for you:

```md
![Launch talk](https://www.youtube.com/watch?v=dQw4w9WgXcQ)
```

- Works for YouTube, Vimeo, Loom, Figma, Google Maps, CodePen and Spotify.
- In Hatch, nothing loads until you click it, so opening a note never tells those sites you're reading it. YouTube uses its no-cookie player.
- On a published site the player loads lazily in a sandboxed frame.
- Any other web page written this way shows as a link card in Hatch.

## On published sites

Every component works on a published site. Tabs, chart tooltips, copy buttons and the scroll buttons in your own components use a small script from the site itself, since sites only allow their own scripts.

- The copy button works on plain http too. Browsers only offer the clipboard on https, so there Hatch copies the older way.
- The button says "Copied" when it worked and "Could not copy" when the browser refused.
- For developers: that script is `BLOCKS_JS` in `packages/blocks/src/index.ts`. It is hand-written text (`TABS_JS` in `render.ts`, `CHARTS_JS`, `COPY_JS` and `SCROLL_JS` in `interact.ts`), not text taken from compiled functions, so it runs the same however Hatch was built. Keep it to plain ES2017 that only uses `document`, `navigator` and `setTimeout`. When you change `wireCharts` or `wireCopy`, change the matching script too.

## Adding a block

Blocks live in `packages/blocks`:
- `defs.ts` lists each block's keys. Suggestions and checks come from those lists.
- `render.ts` turns a block into HTML for the editor and for sites.
- `custom.ts` reads the vault's own components (`.hatch/components/`): it checks the template and CSS, and draws them. `makeGuide.ts` holds the guide for making one, with a pull quote as its example. A carousel is kept as a test fixture in `testdata/carousel.yaml`, not shipped. The app loads them in `packages/app/src/ownComponents.ts`.
