diff --git a/src/styles.test.ts b/src/styles.test.ts new file mode 100644 index 000000000..bc86ee715 --- /dev/null +++ b/src/styles.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from "bun:test"; + +import { applyHostStyleVariables } from "./styles.js"; +import type { McpUiStyles } from "./types.js"; + +function fakeRoot() { + const props = new Map(); + return { + style: { + setProperty: (key: string, value: string) => props.set(key, value), + removeProperty: (key: string) => props.delete(key), + getPropertyValue: (key: string) => props.get(key) ?? "", + }, + } as unknown as HTMLElement; +} + +describe("applyHostStyleVariables", () => { + it("clears variables that the host drops from its context", () => { + const root = fakeRoot(); + applyHostStyleVariables( + { "--color-background-primary": "red" } as McpUiStyles, + root, + ); + expect(root.style.getPropertyValue("--color-background-primary")).toBe( + "red", + ); + + applyHostStyleVariables( + { "--color-background-primary": undefined } as McpUiStyles, + root, + ); + expect(root.style.getPropertyValue("--color-background-primary")).toBe(""); + }); +}); diff --git a/src/styles.ts b/src/styles.ts index 895073b8d..07aeb77b5 100644 --- a/src/styles.ts +++ b/src/styles.ts @@ -141,7 +141,9 @@ export function applyHostStyleVariables( root: HTMLElement = document.documentElement, ): void { for (const [key, value] of Object.entries(styles)) { - if (value !== undefined) { + if (value === undefined) { + root.style.removeProperty(key); + } else { root.style.setProperty(key, value); } }