Skip to content

Commit 4bd6652

Browse files
authored
feat(ui): improve ui widgets bundle size [internal] (#642)
1 parent ebb1de4 commit 4bd6652

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

src/web/src/utils/init-widget.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
11
import "../index.css";
22
import React, { useEffect } from "react";
3-
import { applyDocumentTheme, applyHostFonts, applyHostStyleVariables } from "@modelcontextprotocol/ext-apps";
3+
import type { McpUiStyles, McpUiTheme } from "@modelcontextprotocol/ext-apps";
44
import { UiDependencyProvider } from "@apify/ui-library";
55
import { cssColorsVariablesLight, cssColorsVariablesDark } from "@apify/ui-library";
66
import { ThemeProvider } from "styled-components";
77
import { createRoot } from "react-dom/client";
88
import { McpAppProvider, useMcpApp } from "../context/mcp-app-context";
99

10+
function applyDocumentTheme(theme: McpUiTheme): void {
11+
document.documentElement.setAttribute("data-theme", theme);
12+
document.documentElement.style.colorScheme = theme;
13+
}
14+
15+
function applyHostStyleVariables(styles: McpUiStyles, root: HTMLElement = document.documentElement): void {
16+
for (const [key, value] of Object.entries(styles)) {
17+
if (value !== undefined) {
18+
root.style.setProperty(key, value);
19+
}
20+
}
21+
}
22+
23+
let _fontsInjected = false;
24+
function applyHostFonts(fontCss: string): void {
25+
if (_fontsInjected) return;
26+
const style = document.createElement("style");
27+
style.textContent = fontCss;
28+
document.head.appendChild(style);
29+
_fontsInjected = true;
30+
}
31+
1032
function resolveSystemTheme(): "light" | "dark" {
1133
return window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
1234
}

0 commit comments

Comments
 (0)