Consolidate host styling patterns into unified documentation

Merge the separate "Support native host styling using CSS variables" and
"Reacting to light/dark theme changes" sections into a single "Matching
host styling (CSS variables, theme, and fonts)" pattern. This provides
clearer guidance by covering all three concerns together.

- Rename `docs/patterns.ts` to `docs/patterns.tsx` for JSX support
- Add `hostStylingVanillaJs` and `hostStylingReact` consolidated examples
- Enhance `applyHostFonts_fromHostContext` to apply initial fonts after
  `connect()`, not just on context change
- Add CSS usage examples to `applyHostStyleVariables()` JSDoc
- Update font example to use `var(--font-sans, system-ui, sans-serif)`

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Jonathan Hefner 2026-01-19 13:19:36 -06:00
commit 566ac58ab7
2 changed files with 108 additions and 26 deletions

View file

@ -91,11 +91,20 @@ function applyHostStyleVariables_toElement(app: App) {
*/
function applyHostFonts_fromHostContext(app: App) {
//#region applyHostFonts_fromHostContext
// Apply when host context changes
app.onhostcontextchanged = (params) => {
if (params.styles?.css?.fonts) {
applyHostFonts(params.styles.css.fonts);
}
};
// Apply initial fonts after connecting
app.connect().then(() => {
const ctx = app.getHostContext();
if (ctx?.styles?.css?.fonts) {
applyHostFonts(ctx.styles.css.fonts);
}
});
//#endregion applyHostFonts_fromHostContext
}

View file

@ -77,6 +77,19 @@ export function applyDocumentTheme(theme: McpUiTheme): void {
* @example Apply to a specific element
* {@includeCode ./styles.examples.ts#applyHostStyleVariables_toElement}
*
* @example Use host style variables in CSS
* ```css
* body {
* background-color: var(--color-background-primary);
* color: var(--color-text-primary);
* }
*
* .card {
* background-color: var(--color-background-secondary);
* border: 1px solid var(--color-border-primary);
* }
* ```
*
* @see {@link McpUiStyles} for the available CSS variables
* @see {@link McpUiHostContext} for the full host context structure
*/
@ -116,7 +129,7 @@ export function applyHostStyleVariables(
* @example Use host fonts in CSS
* ```css
* body {
* font-family: "Anthropic Sans", sans-serif;
* font-family: var(--font-sans, system-ui, sans-serif);
* }
* ```
*