Skip to content

Build your personal knowledge base with Trilium Notes

fix(presentation): release a closed presentation

Elian Dorana112f73d6be5
diff --git a/apps/client/src/widgets/react/ShadowDom.spec.tsx b/apps/client/src/widgets/react/ShadowDom.spec.tsx
new file mode 100644
index 0000000000..64e201a60d
--- /dev/null
+++ b/apps/client/src/widgets/react/ShadowDom.spec.tsx
@@ -0,0 +1,25 @@
+import { render } from "preact";
+import { useEffect } from "preact/hooks";
+import { act } from "preact/test-utils";
+import { describe, expect, it, vi } from "vitest";
+
+import ShadowDom from "./ShadowDom";
+
+describe("ShadowDom", () => {
+ it("unmounts the tree in its shadow root along with itself", () => {
+ const cleanup = vi.fn();
+ function Child() {
+ useEffect(() => cleanup, []);
+ return <p>Inside</p>;
+ }
+ const container = document.createElement("div");
+ document.body.append(container);
+
+ act(() => render(<ShadowDom><Child /></ShadowDom>, container));
+ expect(container.firstElementChild?.shadowRoot?.textContent).toBe("Inside");
+
+ act(() => render(null, container));
+ expect(cleanup).toHaveBeenCalledOnce();
+ container.remove();
+ });
+});
diff --git a/apps/client/src/widgets/react/ShadowDom.tsx b/apps/client/src/widgets/react/ShadowDom.tsx
index faf3330608..99ccd9557b 100644
--- a/apps/client/src/widgets/react/ShadowDom.tsx
+++ b/apps/client/src/widgets/react/ShadowDom.tsx
@@ -25,5 +25,11 @@ export default function ShadowDom({ children, containerRef: externalContainerRef
render(<>{children}</>, shadowRoot);
}, [ shadowRoot, children ]);
+ // The shadow tree is a separate render, which unmounting this component does not unmount.
+ useLayoutEffect(() => {
+ if (!shadowRoot) return;
+ return () => render(null, shadowRoot);
+ }, [ shadowRoot ]);
+
return <div ref={containerRef} {...containerProps} />
}