|
| 1 | +import { useEffect, useRef } from "react"; |
| 2 | +import { App } from "../app"; |
| 3 | +import { applyHostStyles } from "../styles"; |
| 4 | +import { McpUiHostContext } from "../types"; |
| 5 | + |
| 6 | +/** |
| 7 | + * React hook that applies host styles as CSS custom properties. |
| 8 | + * |
| 9 | + * This hook listens to host context changes and automatically applies the |
| 10 | + * `styles` CSS variables to `document.documentElement`. This allows your |
| 11 | + * app to use the host's theming values via CSS variables like |
| 12 | + * `var(--color-background-primary)`. |
| 13 | + * |
| 14 | + * The hook also applies styles from the initial host context when the app |
| 15 | + * first connects. |
| 16 | + * |
| 17 | + * @param app - The connected App instance, or null during initialization |
| 18 | + * @param initialContext - Initial host context from the connection (optional). |
| 19 | + * If provided, styles will be applied immediately on mount. |
| 20 | + * |
| 21 | + * @example Basic usage with useApp |
| 22 | + * ```tsx |
| 23 | + * import { useApp } from '@modelcontextprotocol/ext-apps/react'; |
| 24 | + * import { useHostStyles } from '@modelcontextprotocol/ext-apps/react'; |
| 25 | + * |
| 26 | + * function MyApp() { |
| 27 | + * const { app, isConnected } = useApp({ |
| 28 | + * appInfo: { name: "MyApp", version: "1.0.0" }, |
| 29 | + * capabilities: {}, |
| 30 | + * }); |
| 31 | + * |
| 32 | + * // Automatically apply host styles as CSS variables |
| 33 | + * useHostStyles(app); |
| 34 | + * |
| 35 | + * return ( |
| 36 | + * <div style={{ background: 'var(--color-background-primary)' }}> |
| 37 | + * Hello! |
| 38 | + * </div> |
| 39 | + * ); |
| 40 | + * } |
| 41 | + * ``` |
| 42 | + * |
| 43 | + * @example With initial context |
| 44 | + * ```tsx |
| 45 | + * const [hostContext, setHostContext] = useState<McpUiHostContext | null>(null); |
| 46 | + * |
| 47 | + * // ... get initial context from app.connect() result |
| 48 | + * |
| 49 | + * useHostStyles(app, hostContext); |
| 50 | + * ``` |
| 51 | + * |
| 52 | + * @see {@link applyHostStyles} for the underlying function |
| 53 | + * @see {@link McpUiStyles} for available CSS variables |
| 54 | + */ |
| 55 | +export function useHostStyles( |
| 56 | + app: App | null, |
| 57 | + initialContext?: McpUiHostContext | null, |
| 58 | +): void { |
| 59 | + const initialStylesApplied = useRef(false); |
| 60 | + |
| 61 | + // Apply initial styles once on mount |
| 62 | + useEffect(() => { |
| 63 | + if (initialStylesApplied.current) { |
| 64 | + return; |
| 65 | + } |
| 66 | + if (initialContext?.styles) { |
| 67 | + applyHostStyles(initialContext.styles); |
| 68 | + initialStylesApplied.current = true; |
| 69 | + } |
| 70 | + }, [initialContext]); |
| 71 | + |
| 72 | + // Listen for host context changes and apply updated styles |
| 73 | + useEffect(() => { |
| 74 | + if (!app) { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + app.onhostcontextchanged = (params) => { |
| 79 | + if (params.styles) { |
| 80 | + applyHostStyles(params.styles); |
| 81 | + } |
| 82 | + }; |
| 83 | + }, [app]); |
| 84 | +} |
0 commit comments