Files
docx/docs/live-examples/live-examples.css
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded

The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now
an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with
docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types,
the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and
the pages side by side, or one of them. The demos a page includes are live too.

Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its
imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain.

Running them showed docs that were wrong, fixed here:
- Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page
  marked as landscape
- Comments: `comments.children` takes plain objects, not `new Comment(...)`
- Custom properties are text only
- APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...),
  createParagraph().heading1(), a border's `value`, TextRun's `.break()`
- The templates and patcher examples read files that aren't in the repository. They now patch the templates in
  demo/assets, or make their own

`npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they
describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

* docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document

"ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on
the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example
now awaits patchDocument and writes the document it makes, as the example after it does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-28 13:00:29 +01:00

138 lines
3.2 KiB
CSS

/* Live examples: see live-examples.js. The colours are the docs theme's, so they follow it between light and dark */
.markdown-section .live-example {
margin: 1em 0;
border: 1px solid var(--borderColor);
border-radius: 4px;
/* Clipped rather than hidden, which would stop the pages sticking to the top of the window in the split view */
overflow: clip;
container-type: inline-size;
}
.live-example__panes {
display: grid;
grid-template-columns: minmax(0, 1fr);
background: var(--codeBackgroundColor);
}
.markdown-section .live-example pre {
margin: 0;
border-radius: 0;
cursor: text;
}
.markdown-section .live-example pre:focus-visible {
outline: 2px solid var(--accent);
outline-offset: -2px;
}
.live-example__editor {
min-height: 60px;
}
.live-example__bar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
padding: 6px 10px;
border-bottom: 1px solid var(--borderColor);
font-size: 0.8rem;
}
.live-example__status {
flex: 1;
color: var(--blockQuoteColor);
}
.live-example__status--error {
color: var(--highlightColor);
font-family: var(--codeFontFamily);
white-space: pre-wrap;
}
.live-example__bar button {
padding: 4px 12px;
border: 1px solid var(--accent);
border-radius: 4px;
background: transparent;
color: var(--accent);
font: inherit;
font-weight: 600;
cursor: pointer;
}
.live-example__bar button:hover:not(:disabled),
.live-example__bar button[aria-pressed="true"] {
background: var(--accent);
color: #fff;
}
.live-example__bar button:disabled {
opacity: 0.5;
cursor: default;
}
.live-example__views {
display: inline-flex;
}
.live-example__bar .live-example__views button {
border-radius: 0;
}
.live-example__bar .live-example__views button + button {
margin-left: -1px;
}
.live-example__bar .live-example__views button:first-child {
border-radius: 4px 0 0 4px;
}
.live-example__bar .live-example__views button:last-child {
border-radius: 0 4px 4px 0;
}
.live-example__preview {
max-height: 70vh;
min-height: 80px;
overflow: auto;
border-top: 1px solid var(--borderColor);
transition: opacity 0.2s;
}
.live-example[data-view="code"] .live-example__preview,
.live-example[data-view="preview"] .live-example__code {
display: none;
}
/* Only the pages are shown, so all of them are */
.live-example[data-view="preview"] .live-example__preview {
max-height: none;
border-top: none;
}
/* Side by side when there's room, with the pages kept in sight as the code scrolls past. Otherwise the pages are under the code */
@container (min-width: 720px) {
.live-example[data-view="split"] .live-example__panes {
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
.live-example[data-view="split"] .live-example__code {
border-right: 1px solid var(--borderColor);
}
.live-example[data-view="split"] .live-example__preview {
position: sticky;
top: 0;
align-self: start;
max-height: 100vh;
border-top: none;
}
}
/* The code doesn't run, so the pages are what it last drew */
.live-example__preview--stale {
opacity: 0.5;
}