Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

remount EditorBubbleMenu on hide (#77575)

Nick Fitzpatrickee29a82916d7
diff --git a/frontend/src/metabase/rich_text_editing/tiptap/components/EditorBubbleMenu/EditorBubbleMenu.tsx b/frontend/src/metabase/rich_text_editing/tiptap/components/EditorBubbleMenu/EditorBubbleMenu.tsx
index 4c45e59508d..627752416e9 100644
--- a/frontend/src/metabase/rich_text_editing/tiptap/components/EditorBubbleMenu/EditorBubbleMenu.tsx
+++ b/frontend/src/metabase/rich_text_editing/tiptap/components/EditorBubbleMenu/EditorBubbleMenu.tsx
@@ -55,6 +55,7 @@ export const EditorBubbleMenu: React.FC<EditorBubbleMenuProps> = ({
}) => {
const forceUpdate = useForceUpdate();
const [initialLinkUrl, setInitialLinkUrl] = useState<string | null>(null);
+ const [contentKey, setContentKey] = useState(0);
const handleLinkClick = () => {
const existingLink = editor.getAttributes("link");
@@ -92,8 +93,13 @@ export const EditorBubbleMenu: React.FC<EditorBubbleMenuProps> = ({
className={className}
editor={editor}
options={{
- onHide: () => setInitialLinkUrl(null),
...options,
+ onHide: () => {
+ setInitialLinkUrl(null);
+ //remount the content to clean up any lingering tooltips
+ setContentKey((key) => key + 1);
+ options?.onHide?.();
+ },
}}
shouldShow={({
editor,
@@ -139,6 +145,7 @@ export const EditorBubbleMenu: React.FC<EditorBubbleMenuProps> = ({
}}
>
<Flex
+ key={contentKey}
gap={4}
p="2px"
className={S.bubbleMenu}
diff --git a/frontend/src/metabase/rich_text_editing/tiptap/components/EditorBubbleMenu/EditorBubbleMenu.unit.spec.tsx b/frontend/src/metabase/rich_text_editing/tiptap/components/EditorBubbleMenu/EditorBubbleMenu.unit.spec.tsx
new file mode 100644
index 00000000000..e306cc93a8a
--- /dev/null
+++ b/frontend/src/metabase/rich_text_editing/tiptap/components/EditorBubbleMenu/EditorBubbleMenu.unit.spec.tsx
@@ -0,0 +1,90 @@
+import userEvent from "@testing-library/user-event";
+import { type Editor, EditorContent, useEditor } from "@tiptap/react";
+import { StarterKit } from "@tiptap/starter-kit";
+
+import { act, renderWithProviders, screen, waitFor } from "__support__/ui";
+
+import { EditorBubbleMenu } from "./EditorBubbleMenu";
+
+function setup() {
+ let editor: Editor | null = null;
+
+ function TestEditor() {
+ const instance = useEditor({
+ extensions: [StarterKit],
+ content: "<p>Hello world</p>",
+ immediatelyRender: true,
+ });
+
+ editor = instance;
+
+ if (!instance) {
+ return null;
+ }
+
+ return (
+ <>
+ <EditorContent editor={instance} />
+ <EditorBubbleMenu editor={instance} disallowedNodes={[]} />
+ </>
+ );
+ }
+
+ renderWithProviders(<TestEditor />);
+
+ return {
+ selectText: () =>
+ act(() => {
+ editor?.commands.setTextSelection({ from: 1, to: 6 });
+ }),
+ deleteText: () =>
+ act(() => {
+ editor?.chain().selectAll().deleteSelection().run();
+ }),
+ restoreText: () =>
+ act(() => {
+ editor?.commands.setContent("<p>Hello world</p>");
+ }),
+ };
+}
+
+function findBoldButton() {
+ return screen.findByRole(
+ "button",
+ { name: "text_bold icon" },
+ { timeout: 2000 },
+ );
+}
+
+describe("EditorBubbleMenu", () => {
+ it("should not leave an orphaned tooltip in the DOM after the menu hides (metabase#76365)", async () => {
+ const { selectText, deleteText } = setup();
+
+ selectText();
+ await userEvent.hover(await findBoldButton());
+ expect(await screen.findByRole("tooltip")).toHaveTextContent("Bold");
+ deleteText();
+
+ await waitFor(() => {
+ expect(screen.queryByRole("tooltip")).not.toBeInTheDocument();
+ });
+ });
+
+ it("should still show the bubble menu again after it has been hidden", async () => {
+ const { selectText, deleteText, restoreText } = setup();
+
+ selectText();
+ expect(await findBoldButton()).toBeInTheDocument();
+
+ deleteText();
+ await waitFor(() => {
+ expect(
+ screen.queryByRole("button", { name: "text_bold icon" }),
+ ).not.toBeInTheDocument();
+ });
+
+ restoreText();
+ selectText();
+ expect(await findBoldButton()).toBeInTheDocument();
+ });
+});