Blocks
Docs
Everything a product's documentation needs around the words: tabs for each part of the docs, a sidebar of pages, search on ⌘K, a contents rail that follows the reader, code in several languages, and each page as Markdown for people who paste docs into an AI tool.
Installation
npx sinaui add blocks/docsCopy src/lib/blocks/docs/ with the components it uses: Docs nav, Table of contents, Command
palette, Dialog, Breadcrumb, Code block, Tabs, Copy button, Theme toggle, Kbd, Button and Icon.
Example
The docs of Warraq, a pretend manuscript catalog: four pages under two tabs. Pick a code language once and every sample on the page follows it. Switch to Phone to see the sidebar fold into a menu.
Usage
The layout goes in your docs' +layout.svelte and takes the nav as data; each page
renders a DocsPage with its content as children. The example's own files:
<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/state';
import DocsLayout from '#lib/blocks/docs/DocsLayout.svelte';
import { href, markdownHref, product, root, sections } from './content.js';
let { children } = $props();
</script>
<DocsLayout
name={product}
{sections}
current={page.url.pathname}
home={root}
action={{ label: 'Dashboard', href: href('') }}
onnavigate={(to) => goto(to)}
>
{#snippet logo()}
<span class="mark" aria-hidden="true">W</span>
{product}
{/snippet}
{#snippet footer()}
{product} documentation, a Sina UI demo.
<a href={markdownHref(page.params.page ?? '')} data-sveltekit-reload>This page as Markdown</a>
{/snippet}
{@render children()}
</DocsLayout>
<style>
.mark {
display: grid;
place-items: center;
inline-size: 1.5rem;
block-size: 1.5rem;
border-radius: var(--ui-radius);
background: var(--ui-accent);
color: var(--ui-on-accent);
font-size: 0.8125rem;
}
</style>
<script lang="ts">
import CodeTabs from '#lib/blocks/docs/CodeTabs.svelte';
import DocsPage from '#lib/blocks/docs/DocsPage.svelte';
import { locate } from '#lib/blocks/docs/docs.js';
import { find, href, markdown, markdownHref, product, sections, toc } from '../content.js';
import type { PageProps } from './$types';
let { data }: PageProps = $props();
const doc = $derived(find(data.slug)!);
const place = $derived(locate(sections, href(doc.slug)));
// One pick for every code sample on the page.
let lang = $state<string>();
// `code` spans in the text.
const parts = (text: string) => text.split('`').map((t, i) => ({ t, code: i % 2 === 1 }));
</script>
<svelte:head>
<title>{doc.title} — {product} docs</title>
<meta name="description" content={doc.description} />
<meta property="og:title" content="{doc.title} — {product} docs" />
<meta property="og:description" content={doc.description} />
</svelte:head>
{#snippet text(s: string)}{#each parts(s) as p, i (i)}{#if p.code}<code>{p.t}</code
>{:else}{p.t}{/if}{/each}{/snippet}
<DocsPage
title={doc.title}
description={doc.description}
crumbs={[
{ label: place.section.label, href: place.section.href },
...(place.group?.title ? [{ label: place.group.title }] : []),
{ label: doc.title }
]}
markdown={markdown(doc)}
markdownHref={markdownHref(doc.slug)}
toc={toc(doc)}
prev={place.prev}
next={place.next}
>
{#each doc.blocks as b, i (i)}
{#if b.kind === 'h2'}
<h2 id={b.id}>{b.text}</h2>
{:else if b.kind === 'h3'}
<h3 id={b.id}>{b.text}</h3>
{:else if b.kind === 'p'}
<p>{@render text(b.text)}</p>
{:else if b.kind === 'list'}
<ul>
{#each b.items as item, j (j)}<li>{@render text(item)}</li>{/each}
</ul>
{:else}
<CodeTabs samples={b.samples} bind:value={lang} />
{/if}
{/each}
</DocsPage>
Props
DocsLayout
| Prop | Type | Default |
|---|---|---|
name | string: the product | required |
sections | { label, href, groups }[]: top-bar tabs; one shows no tabs | required |
current | string: the page's path | required |
home | string | '/' |
logo | Snippet | the name |
action | { label, href } | |
search | (query) => Command[]: your own index; pages by title without it | |
searchEmpty | string | “Nothing matches…” |
onnavigate | (href) => void: pass goto | a full page load |
footer | Snippet |
DocsPage
| Prop | Type | Default |
|---|---|---|
title, description | string | |
crumbs | { label, href? }[] | |
markdown | string | () => Promise<string>: shows “Copy page as Markdown” | |
markdownHref | string: shows “View as Markdown” | |
toc | { id, label, level? }[] | [] |
prev, next | { label, href } |
locate(sections, current) finds a page's tab, group and its previous and next pages,
so the crumbs and pager come from the same nav as the sidebar. CodeTabs takes samples: { label, code, lang?, filename? }[] and a bindable value.
The content keeps one rhythm: every block sits --prose-gap (1.25rem; set it to change
it) below the last, and headings get more room above than below. Wrap a component that sets its
own margins, such as a lone CodeBlock or an Alert, in a div; CodeTabs already is, and each of its tabs is only as tall as its own code.
Accessibility
- A skip link, a banner, one main region and named navigations for the tabs, pages and contents.
- The current tab and page carry
aria-current="page"; the section being read,aria-current="location". - Search opens on ⌘K (Ctrl+K elsewhere) and is a full keyboard command palette.
- Below 56rem the sidebar moves into a sheet; below 75rem the contents rail hides.
- Everything is server-rendered: the page reads without JavaScript, and reduced motion is respected.