diff --git a/components/notes/InlineMarkdownEditor.test.tsx b/components/notes/InlineMarkdownEditor.test.tsx index 635d80a5f..31953ea6b 100644 --- a/components/notes/InlineMarkdownEditor.test.tsx +++ b/components/notes/InlineMarkdownEditor.test.tsx @@ -459,6 +459,17 @@ test("note markdown toolbar remains usable in narrow panes", () => { ); }); +test("MDX link dialog overlay stays inside the retained notes editor container", () => { + const source = readFileSync(new URL("./InlineMarkdownEditor.tsx", import.meta.url), "utf8"); + + // Default MDXEditor popup target is document.body. Keep the overlay in the + // editor container so section `hidden` and vault visibility:hidden hide it (#4257). + assert.match(source, /const \[overlayContainer, setOverlayContainer\] = useState\(null\);/); + assert.match(source, /setOverlayContainer\(\(current\) => \(current === node \? current : node\)\);/); + assert.match(source, /ref=\{setEditorContainer\}/); + assert.match(source, /overlayContainer=\{overlayContainer \?\? undefined\}/); +}); + test("preview mode opens links directly without showing the edit hover action", () => { const source = readFileSync(new URL("./InlineMarkdownEditor.tsx", import.meta.url), "utf8"); diff --git a/components/notes/InlineMarkdownEditor.tsx b/components/notes/InlineMarkdownEditor.tsx index fe4cbfbcc..fa6130f2f 100644 --- a/components/notes/InlineMarkdownEditor.tsx +++ b/components/notes/InlineMarkdownEditor.tsx @@ -946,6 +946,14 @@ export const InlineMarkdownEditor = React.memo( }); const containerRef = useRef(null); + // MDXEditor portals .mdxeditor-popup-container to document.body unless + // overlayContainer is set. Notes stays mounted and is only CSS-hidden, so a + // body portal would leave the link preview toolbar over other pages (#4257). + const [overlayContainer, setOverlayContainer] = useState(null); + const setEditorContainer = useCallback((node: HTMLDivElement | null) => { + containerRef.current = node; + setOverlayContainer((current) => (current === node ? current : node)); + }, []); const getSelectedText = useCallback((): string => { const container = containerRef.current; @@ -2063,7 +2071,7 @@ export const InlineMarkdownEditor = React.memo( return (