Authoring components
Write ordinary Markdown for most pages. Use MDX when a page needs components,
and import them from @tenphi/cookbook/components. Local MDX is supported by
default; npm MDX requires an explicit trust: "mdx" source declaration.
A page’s title comes from its frontmatter title, then the source
declaration’s title, then its first # heading, then its file name. When the
page starts with an # heading equal to its title, Cookbook removes that heading
from the body so the title is not shown twice. The description comes from
frontmatter description, then the source declaration, then the first prose
paragraph of at least 20 characters, cut to 240 characters.
See page frontmatter for every field,
including slug, draft, sidebar, prev, and next.
Every panel requires a label. Arrow keys, Home, and End move between tabs;
right-to-left layouts reverse the horizontal arrow direction. Without
JavaScript, all panels remain readable under their headings.
import { Tab, Tabs } from "@tenphi/cookbook/components";
<Tabs label="Installation">
<Tab label="npm">Run `npm install your-package`.</Tab>
<Tab label="pnpm">Run `pnpm add your-package`.</Tab>
</Tabs>npm
npm install your-package.pnpm
pnpm add your-package.Use kind="note", "tip", "caution", or "danger"; title overrides the
visible label. The default kind is note. Status colors come from
theme.palette.info, success, warning, and danger and adapt to all schemes
and contrast modes. For callouts written in plain Markdown, use
GitHub alerts.
<Callout kind="tip" title="Keep content close to code">
Relative Markdown links continue to work after Cookbook assigns public routes.
</Callout>Pass labeled source strings to create an accessible tabbed code group. language
selects the Shiki grammar (defaults to plain text). Groups share semantic syntax
colors, configured grammars/transformers, and copy controls with Markdown fences.
Each panel retains keyboard-accessible tabs and readable fallback content.
<CodeGroup
label="Install Cookbook"
items={[
{ label: "npm", language: "shell", code: "npm install @tenphi/cookbook" },
{ label: "pnpm", language: "shell", code: "pnpm add @tenphi/cookbook" },
]}
/>npm
shell
npm install @tenphi/cookbookpnpm
shell
pnpm add @tenphi/cookbookUse a card to link to another page, or omit href to present a standalone
summary. href is used as written: give a public URL, including any Astro
base, or a #fragment. Unlike Markdown links, .md paths are not rewritten;
cookbook check-build reports broken targets. Wrap cards in CardGrid to show
them in two equal-height columns on wider screens and a single column on narrow
ones. Steps renders an ordered list; put ordinary li elements inside it.
import { Card, CardGrid, Steps } from "@tenphi/cookbook/components";
<Card title="On-page navigation" href="#on-page-navigation">
Offer a compact contents list on smaller screens.
</Card>
<CardGrid>
<Card title="Tabs" href="#tabs">
Switch between equivalent examples.
</Card>
<Card title="Code groups" href="#code-groups">
Show the same command for several package managers.
</Card>
</CardGrid>
<Steps>
<li>Choose a content source.</li>
<li>Configure the navigation.</li>
<li>Build and check the output.</li>
</Steps>On-page navigation
Offer a compact contents list on smaller screens.
Tabs
Switch between equivalent examples.
Code groups
Show the same command for several package managers.
- Choose a content source.
- Configure the navigation.
- Build and check the output.
Preview runs HTML and optional JavaScript in a sandboxed iframe with a source
disclosure. It cannot access the documentation page’s origin. Its css prop is
for the isolated preview document; style the documentation shell with Tasty.
import { Preview } from "@tenphi/cookbook/components";
<Preview
title="A simple button"
html={'<button type="button">Try it</button>'}
css={"button { padding: 0.75rem; }"}
/>View source
<!doctype html><meta charset="utf-8"><style>button { padding: 0.75rem; }</style><button type="button">Try it</button>To demonstrate your own library, build it into the site’s public directory
(public/ by default) and list the files in scripts by their public path,
without the Astro base. Cookbook inlines each file, in order, before
javascript, so the authored code can use what they define. Each file runs as
a classic script, so use a self-contained IIFE or UMD build without import or
export:
<Preview
title="Library button"
html={"<acme-button>Save</acme-button>"}
scripts={["/demo/button.js"]}
javascript={"document.querySelector('acme-button').loading = true;"}
/>Inlining keeps the preview isolated and needs no request, so the demo works in
astro dev, astro preview, and offline as well as on the deployed site. A
<script src> in html is a cross-site request from the sandboxed frame:
astro dev rejects it, and Chrome blocks it from local servers. View source
shows each inlined file as a <script src> tag. Every preview embeds its own
copy, so keep demo bundles small. A missing file or a path outside the public
directory fails the build.
A preview that runs scripts, with javascript or scripts, fits its frame to
its content. The frame grows and shrinks as the content changes, the page
width changes, or fonts load. It is at least 10rem tall and at most as tall
as the browser window; taller content scrolls inside the frame. A preview
without scripts keeps a 10rem frame, and content beyond it scrolls.
<Preview
title="Growing list"
html={'<button type="button">Add item</button><ol></ol>'}
javascript={
"document.querySelector('button').addEventListener('click', () => document.querySelector('ol').append(document.createElement('li')));"
}
/>View source
<!doctype html><meta charset="utf-8"><style></style><button type="button">Add item</button><ol></ol><script>document.querySelector('button').addEventListener('click', () => document.querySelector('ol').append(document.createElement('li')));</script>The frame fits the content in the document’s flow. An overlay, such as a listbox, a popover, a modal dialog, or an absolutely positioned menu, doesn’t make it taller when it opens, so reserve room for it:
bufferadds room below the content for a picker’s list, such as a select’s or a combobox’s. The room follows the content as the page width changes it. It defaults to no extra room. A preview without scripts can’t measure its content, sobufferhas no effect there.heightsets the frame’s minimum height in place of10rem, for an overlay that uses the whole frame, such as a modal dialog. It also applies to previews without scripts.
The frame is as tall as the content plus buffer, up to the window’s height,
and never shorter than height. Both take a CSS length with a unit, such as
12rem, 320px, or 0px; any other value, including a unitless 0, a
percentage, or calc(), fails the build. To change every preview, style
theme.styles.Preview.Frame instead.
<Preview
title="Library select"
buffer="15rem"
html={"<acme-select label='Fruit'></acme-select>"}
scripts={["/demo/select.js"]}
/>
<Preview
title="Library dialog"
height="24rem"
html={"<acme-dialog open>Saved</acme-dialog>"}
scripts={["/demo/dialog.js"]}
/>Don’t size the content by the frame, for example with 100vh or
height: 100% on the root element: the frame can’t fit such content, and it
can grow to the window’s height.
Forms in a preview can be submitted. To show what a form sends, call
event.preventDefault() in a submit listener and read its FormData;
otherwise the submission navigates the frame away from the demo. The
documentation page itself is never navigated, and the frame keeps its separate
origin.
<Preview
title="Form data"
html={
'<form><label>Name <input name="name" value="Ada"></label> <button>Send</button></form><output></output>'
}
javascript={
"document.querySelector('form').addEventListener('submit', (event) => { event.preventDefault(); document.querySelector('output').value = JSON.stringify(Object.fromEntries(new FormData(event.target))); });"
}
/>View source
<!doctype html><meta charset="utf-8"><style></style><form><label>Name <input name="name" value="Ada"></label> <button>Send</button></form><output></output><script>document.querySelector('form').addEventListener('submit', (event) => { event.preventDefault(); document.querySelector('output').value = JSON.stringify(Object.fromEntries(new FormData(event.target))); });</script>Logo renders the Cookbook mark. Use label when it carries meaning or
decorative beside nearby text that already names the site. To display your
own artwork in the header and drawer, configure
site.logo.
Fenced code blocks get Shiki highlighting and a copy control. Use a diff or
patch fence to highlight additions and deletions. The + and - markers
remain visible independently of their theme-aware backgrounds:
-formatOkhsl(v.h, v.s * 100, l * 100);
+formatOkhsl(v.h, v.s, l);A mermaid fence renders a static, theme-aware diagram; see
Mermaid diagrams.
| Component | Props and behavior |
|---|---|
Tabs |
Optional label, the tab list’s accessible name (default “Examples”); contains Tab elements |
Tab |
Required label, used as the tab button text and as the panel heading without JavaScript |
Callout |
Optional kind (note by default, tip, caution, or danger) and title |
CodeGroup |
Optional label (default “Code examples”) and required items: { label, code, language? }[] |
Card |
Optional title and href; renders an article or link with slot content |
CardGrid |
Grid container for Card elements: one column on narrow screens, two equal-height columns from 40rem |
Steps |
Ordered list container; supply li elements |
Logo |
Optional label, decorative, and root class; renders the Cookbook mark |
Preview |
Required title; optional html, css, javascript, public-file scripts, minimum frame height, and overlay buffer; isolates examples in a sandboxed iframe that fits scripted content |
The public package exports each component’s props type, such as TabProps,
CalloutProps, and CodeGroupProps. Styles and all named sub-elements are
listed in Component styles.
Desktop pages show a table of contents for heading levels 2 and 3. Set the
site defaults with the top-level
tableOfContents key; set the same
field in a page’s frontmatter to change one page:
tableOfContents:
mobile: true
maxHeadingLevel: 4A page’s tableOfContents object replaces the site’s heading levels; omitted
levels fall back to 2 and 3. Only mobile falls back to docs.config.ts.
tableOfContents: false disables both versions. Selecting a mobile contents
link closes the disclosure and moves keyboard focus to the heading. The desktop
contents list highlights the current section and scrolls its own list when the
active link moves out of view. Customize theme.styles.TableOfContents (desktop
list), MobileTableOfContents, and TableOfContentsLayout.
Add hero to a page’s frontmatter to show a hero above the content. Home
pages usually combine it with the wide template: splash layout:
template: splash
hero:
title: Build something useful
tagline: Documentation that stays with the code.
image:
file: ./assets/hero.png
alt: A preview of the documentation
actions:
- text: Get started
link: ./getting-started.md
variant: primary
- text: Compare tools
link: /comparison
variant: secondarytitle defaults to the page title. Each action needs text and link;
variant is primary, secondary, or minimal. Action links go through the
same link rewriting as Markdown, so a relative .md path works.
Use a relative local image. Cookbook reads its intrinsic dimensions and
reserves the correct aspect ratio as the layout responds to screen width.
Use light and dark instead of file for appearance variants; both must have
the same aspect ratio. Remote or public-path images require explicit width and
height; explicit dimensions on a local image must match its aspect ratio.
image.html accepts inline HTML, such as an SVG, in local content and is
removed from untrusted package Markdown. Hero images load eagerly with high fetch priority;
use ordinary Markdown images for content farther down the page.
theme.styles.Hero.Visual controls responsive sizing without changing the
intrinsic dimensions.