Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(views): stop dialog scroll lock from re-rendering Mermaid diagrams (MUL-7760) (#8889)

Jiayuan Zhang32a396fd520b
diff --git a/packages/views/editor/hooks/use-theme-version.ts b/packages/views/editor/hooks/use-theme-version.ts
index 406af699b6..c0f0f0ec3a 100644
--- a/packages/views/editor/hooks/use-theme-version.ts
+++ b/packages/views/editor/hooks/use-theme-version.ts
@@ -1,18 +1,50 @@
import { useEffect, useState } from "react";
+import { HTML_BLOCK_THEME_TOKENS } from "../utils/html-block-document";
/**
- * A counter that bumps whenever the app theme may have changed: a class,
- * style or data-theme change on <html>/<body>, or the OS color scheme.
+ * Everything that decides what a sandboxed block copies out of the theme: the
+ * attributes that pick a theme, and what it resolves to on the root. Tokens
+ * are read resolved so a theme written straight into `style` still counts.
+ */
+function readThemeSignature(): string {
+ const root = document.documentElement;
+ const styles = getComputedStyle(root);
+ return [
+ root.className,
+ root.getAttribute("data-theme"),
+ document.body?.className,
+ document.body?.getAttribute("data-theme"),
+ styles.colorScheme,
+ styles.fontFamily,
+ ...HTML_BLOCK_THEME_TOKENS.map((name) => styles.getPropertyValue(name)),
+ ].join("|");
+}
+
+/**
+ * A counter that bumps whenever the app theme changes.
*
* Sandboxed blocks copy resolved theme colors into their iframe document, so
* they cannot follow a theme switch through CSS; they rebuild when this bumps.
+ *
+ * A class, style or data-theme change on <html>/<body>, or an OS color-scheme
+ * change, only prompts a check: it bumps when the signature actually moved. A
+ * dialog's scroll lock writes `overflow` into that `style` on every open and
+ * close, and taking it for a theme switch re-rendered every diagram on the
+ * page — on close, a long task that stalled the dialog's exit and made the
+ * page flash (MUL-7760).
*/
export function useThemeVersion(): number {
const [themeVersion, setThemeVersion] = useState(0);
useEffect(() => {
- const bumpThemeVersion = () => setThemeVersion((version) => version + 1);
- const observer = new MutationObserver(bumpThemeVersion);
+ let signature = readThemeSignature();
+ const bumpIfThemeChanged = () => {
+ const next = readThemeSignature();
+ if (next === signature) return;
+ signature = next;
+ setThemeVersion((version) => version + 1);
+ };
+ const observer = new MutationObserver(bumpIfThemeChanged);
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ["class", "style", "data-theme"],
@@ -25,11 +57,11 @@ export function useThemeVersion(): number {
}
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
- mediaQuery.addEventListener("change", bumpThemeVersion);
+ mediaQuery.addEventListener("change", bumpIfThemeChanged);
return () => {
observer.disconnect();
- mediaQuery.removeEventListener("change", bumpThemeVersion);
+ mediaQuery.removeEventListener("change", bumpIfThemeChanged);
};
}, []);
diff --git a/packages/views/editor/mermaid-diagram.test.tsx b/packages/views/editor/mermaid-diagram.test.tsx
index ea3c4db632..24e61b682d 100644
--- a/packages/views/editor/mermaid-diagram.test.tsx
+++ b/packages/views/editor/mermaid-diagram.test.tsx
@@ -67,6 +67,8 @@ beforeEach(() => {
afterEach(() => {
vi.restoreAllMocks();
document.documentElement.className = "";
+ document.documentElement.removeAttribute("style");
+ document.body.removeAttribute("style");
});
function currentScale(): number {
@@ -195,6 +197,51 @@ describe("MermaidDiagram theme changes", () => {
});
expect(document.querySelector(".mermaid-diagram-frame")).not.toBeNull();
});
+
+ it("does not re-render when a dialog's scroll lock rewrites the page style", async () => {
+ render(<MermaidDiagram chart={CHART} />);
+ await waitFor(() => {
+ expect(mermaidRenderMock).toHaveBeenCalledTimes(1);
+ });
+
+ // What the Dialog's scroll lock writes on every open and close. Taken for a
+ // theme switch, it re-rendered every diagram on the page as the viewer
+ // closed, and that stall made the page flash (MUL-7760).
+ await act(async () => {
+ document.body.style.overflow = "hidden";
+ await Promise.resolve();
+ });
+ await act(async () => {
+ document.body.style.removeProperty("overflow");
+ await Promise.resolve();
+ });
+
+ // A real theme switch still re-renders, and it is the only extra render:
+ // any from the scroll lock would already have pushed the count past two.
+ await act(async () => {
+ document.documentElement.classList.add("dark");
+ await Promise.resolve();
+ });
+ await waitFor(() => {
+ expect(mermaidRenderMock).toHaveBeenCalledTimes(2);
+ });
+ });
+
+ it("re-renders when a theme token is written straight into the root style", async () => {
+ render(<MermaidDiagram chart={CHART} />);
+ await waitFor(() => {
+ expect(mermaidRenderMock).toHaveBeenCalledTimes(1);
+ });
+
+ await act(async () => {
+ document.documentElement.style.setProperty("--muted", "rgb(1, 2, 3)");
+ await Promise.resolve();
+ });
+
+ await waitFor(() => {
+ expect(mermaidRenderMock).toHaveBeenCalledTimes(2);
+ });
+ });
});
describe("MermaidDiagram rendering config", () => {