Skip to content

Commit 88b4536

Browse files
fix: Formatting Toolbar not hiding when clicking on elements outside it (#233)
* Fixed condition which checks if the formatting toolbar is being clicked * Made hyperlink toolbar hide when editor loses focus * Removed unnecessary listeners & made link button popup child of formatting toolbar * Added comments
1 parent cbd2adc commit 88b4536

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,15 @@ export class FormattingToolbarView<BSchema extends BlockSchema> {
110110
return;
111111
}
112112

113+
// Checks if the focus is moving to an element outside the editor. If it is,
114+
// the toolbar is hidden.
113115
if (
114-
event?.relatedTarget &&
115-
this.formattingToolbar.element?.parentNode?.contains(
116-
event.relatedTarget as Node
117-
)
116+
// An element is clicked.
117+
event &&
118+
event.relatedTarget &&
119+
// Element is outside the toolbar.
120+
(this.formattingToolbar.element === (event.relatedTarget as Node) ||
121+
this.formattingToolbar.element?.contains(event.relatedTarget as Node))
118122
) {
119123
return;
120124
}
@@ -169,12 +173,6 @@ export class FormattingToolbarView<BSchema extends BlockSchema> {
169173
this.formattingToolbar.render(this.getDynamicParams(), true);
170174
this.toolbarIsOpen = true;
171175

172-
// TODO: Is this necessary? Also for other menu plugins.
173-
// Listener stops focus moving to the menu on click.
174-
this.formattingToolbar.element!.addEventListener("mousedown", (event) =>
175-
event.preventDefault()
176-
);
177-
178176
return;
179177
}
180178

@@ -197,12 +195,6 @@ export class FormattingToolbarView<BSchema extends BlockSchema> {
197195
this.formattingToolbar.hide();
198196
this.toolbarIsOpen = false;
199197

200-
// Listener stops focus moving to the menu on click.
201-
this.formattingToolbar.element!.removeEventListener(
202-
"mousedown",
203-
(event) => event.preventDefault()
204-
);
205-
206198
return;
207199
}
208200
}

packages/core/src/extensions/HyperlinkToolbar/HyperlinkToolbarPlugin.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class HyperlinkToolbarView {
5757
};
5858

5959
this.editor.view.dom.addEventListener("mouseover", this.mouseOverHandler);
60+
document.addEventListener("click", this.clickHandler, true);
6061
document.addEventListener("scroll", this.scrollHandler);
6162
}
6263

@@ -101,6 +102,24 @@ class HyperlinkToolbarView {
101102
return false;
102103
};
103104

105+
clickHandler = (event: MouseEvent) => {
106+
if (
107+
// Toolbar is open.
108+
this.hyperlinkMark &&
109+
// An element is clicked.
110+
event &&
111+
event.target &&
112+
// Element is outside the editor.
113+
this.editor.view.dom !== (event.target as Node) &&
114+
!this.editor.view.dom.contains(event.target as Node) &&
115+
// Element is outside the toolbar.
116+
this.hyperlinkToolbar.element !== (event.target as Node) &&
117+
!this.hyperlinkToolbar.element?.contains(event.target as Node)
118+
) {
119+
this.hyperlinkToolbar.hide();
120+
}
121+
};
122+
104123
scrollHandler = () => {
105124
if (this.hyperlinkMark !== undefined) {
106125
this.hyperlinkToolbar.render(this.getDynamicParams(), false);

packages/react/src/FormattingToolbar/components/LinkToolbarButton.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ export const LinkToolbarButton = (props: HyperlinkButtonProps) => {
6262

6363
return (
6464
<Tippy
65-
appendTo={document.body}
6665
content={creationMenu}
6766
onShow={updateCreationMenu}
6867
interactive={true}

0 commit comments

Comments
 (0)