Skip to content

Commit 8025736

Browse files
committed
fix: prevent errors when accessing DOM during editor unmount
1 parent ec19b21 commit 8025736

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,23 @@ export const CreateLinkButton = () => {
7070
return;
7171
}
7272

73-
editor.prosemirrorView.dom.addEventListener("keydown", callback);
73+
// Capture the DOM element at the time the effect runs. Guard against
74+
// cases where the editor view is not available (e.g. during unmount).
75+
const dom = editor.prosemirrorView?.dom;
76+
if (!dom) {
77+
return;
78+
}
79+
80+
dom.addEventListener("keydown", callback);
7481

7582
return () => {
76-
editor.prosemirrorView.dom.removeEventListener("keydown", callback);
83+
// Use the captured `dom` reference for cleanup. If the editor view
84+
// was torn down, this avoids accessing `editor.prosemirrorView.dom`.
85+
if (dom && dom.removeEventListener) {
86+
dom.removeEventListener("keydown", callback);
87+
}
7788
};
78-
}, [editor.prosemirrorView, editor.headless]);
89+
}, [editor.headless, editor.prosemirrorView]);
7990

8091
const update = useCallback(
8192
(url: string) => {

0 commit comments

Comments
 (0)