Move JSDoc examples to type-checked .examples.ts files
Extract inline code examples from JSDoc comments into companion
`.examples.ts`/`.examples.tsx` files. Examples are now:
- Type-checked by wrapping them in functions
- Referenced via `{@includeCode ./file.examples.ts#regionName}` tags
- Organized using `//#region` and `//#endregion` markers
This keeps the main source files cleaner while ensuring examples
compile correctly. Updated TypeDoc config with `exampleTag: false`
to support title text on `@example` tags.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
e5c90c7385
commit
ad04e89745
1 changed files with 1661 additions and 838 deletions
112
src/styles.examples.ts
Normal file
112
src/styles.examples.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
/**
|
||||
* Type-checked examples for style utilities in {@link ./styles.ts}.
|
||||
*
|
||||
* These examples are included in the API documentation via `@includeCode` tags.
|
||||
* Each function's region markers define the code snippet that appears in the docs.
|
||||
*
|
||||
* @module
|
||||
*/
|
||||
|
||||
import type { McpUiHostContext } from "./types.js";
|
||||
import { App } from "./app.js";
|
||||
import {
|
||||
getDocumentTheme,
|
||||
applyDocumentTheme,
|
||||
applyHostStyleVariables,
|
||||
applyHostFonts,
|
||||
} from "./styles.js";
|
||||
|
||||
// Stub declarations for examples
|
||||
declare const app: App;
|
||||
declare const hostContext: McpUiHostContext;
|
||||
|
||||
/**
|
||||
* Example: Check current theme.
|
||||
*/
|
||||
function getDocumentTheme_checkCurrent() {
|
||||
//#region getDocumentTheme_checkCurrent
|
||||
const theme = getDocumentTheme();
|
||||
console.log(`Current theme: ${theme}`);
|
||||
//#endregion getDocumentTheme_checkCurrent
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Apply theme from host context.
|
||||
*/
|
||||
function applyDocumentTheme_fromHostContext() {
|
||||
//#region applyDocumentTheme_fromHostContext
|
||||
app.onhostcontextchanged = (params) => {
|
||||
if (params.theme) {
|
||||
applyDocumentTheme(params.theme);
|
||||
}
|
||||
};
|
||||
//#endregion applyDocumentTheme_fromHostContext
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Apply style variables from host context.
|
||||
*/
|
||||
function applyHostStyleVariables_fromHostContext() {
|
||||
//#region applyHostStyleVariables_fromHostContext
|
||||
app.onhostcontextchanged = (params) => {
|
||||
if (params.styles?.variables) {
|
||||
applyHostStyleVariables(params.styles.variables);
|
||||
}
|
||||
};
|
||||
//#endregion applyHostStyleVariables_fromHostContext
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Apply to a specific element.
|
||||
*/
|
||||
function applyHostStyleVariables_toElement() {
|
||||
//#region applyHostStyleVariables_toElement
|
||||
const container = document.getElementById("app-root");
|
||||
if (container && hostContext.styles?.variables) {
|
||||
applyHostStyleVariables(hostContext.styles.variables, container);
|
||||
}
|
||||
//#endregion applyHostStyleVariables_toElement
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Apply fonts from host context.
|
||||
*/
|
||||
function applyHostFonts_fromHostContext() {
|
||||
//#region applyHostFonts_fromHostContext
|
||||
app.onhostcontextchanged = (params) => {
|
||||
if (params.styles?.css?.fonts) {
|
||||
applyHostFonts(params.styles.css.fonts);
|
||||
}
|
||||
};
|
||||
//#endregion applyHostFonts_fromHostContext
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Host providing self-hosted fonts.
|
||||
*/
|
||||
function applyHostFonts_selfHosted() {
|
||||
//#region applyHostFonts_selfHosted
|
||||
// Example of what a host might provide:
|
||||
const fontCss = `
|
||||
@font-face {
|
||||
font-family: "Anthropic Sans";
|
||||
src: url("https://assets.anthropic.com/.../Regular.otf") format("opentype");
|
||||
font-weight: 400;
|
||||
}
|
||||
`;
|
||||
applyHostFonts(fontCss);
|
||||
//#endregion applyHostFonts_selfHosted
|
||||
}
|
||||
|
||||
/**
|
||||
* Example: Host providing Google Fonts.
|
||||
*/
|
||||
function applyHostFonts_googleFonts() {
|
||||
//#region applyHostFonts_googleFonts
|
||||
// Example of what a host might provide:
|
||||
const fontCss = `
|
||||
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
|
||||
`;
|
||||
applyHostFonts(fontCss);
|
||||
//#endregion applyHostFonts_googleFonts
|
||||
}
|
||||
Loading…
Reference in a new issue