diff --git a/packages/react/src/BubbleMenu.tsx b/packages/react/src/BubbleMenu.tsx index 35631034092..2d9481edffa 100644 --- a/packages/react/src/BubbleMenu.tsx +++ b/packages/react/src/BubbleMenu.tsx @@ -1,4 +1,6 @@ -import React, { useEffect, useRef } from 'react' +import React, { + useEffect, useState, +} from 'react' import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu' type Optional = Pick, K> & Omit @@ -8,10 +10,14 @@ export type BubbleMenuProps = Omit, } export const BubbleMenu: React.FC = props => { - const element = useRef(null) + const [element, setElement] = useState(null) useEffect(() => { - if (!element.current) { + if (!element) { + return + } + + if (props.editor.isDestroyed) { return } @@ -22,24 +28,23 @@ export const BubbleMenu: React.FC = props => { shouldShow = null, } = props - editor.registerPlugin(BubbleMenuPlugin({ + const plugin = BubbleMenuPlugin({ pluginKey, editor, - element: element.current as HTMLElement, + element, tippyOptions, shouldShow, - })) + }) - return () => { - editor.unregisterPlugin(pluginKey) - } + editor.registerPlugin(plugin) + return () => editor.unregisterPlugin(pluginKey) }, [ props.editor, - element.current, + element, ]) return ( -
+
{props.children}
) diff --git a/packages/react/src/FloatingMenu.tsx b/packages/react/src/FloatingMenu.tsx index fb20f851656..6420d8dcf64 100644 --- a/packages/react/src/FloatingMenu.tsx +++ b/packages/react/src/FloatingMenu.tsx @@ -1,4 +1,6 @@ -import React, { useEffect, useRef } from 'react' +import React, { + useEffect, useState, +} from 'react' import { FloatingMenuPlugin, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu' type Optional = Pick, K> & Omit @@ -8,10 +10,14 @@ export type FloatingMenuProps = Omit = props => { - const element = useRef(null) + const [element, setElement] = useState(null) useEffect(() => { - if (!element.current) { + if (!element) { + return + } + + if (props.editor.isDestroyed) { return } @@ -22,24 +28,23 @@ export const FloatingMenu: React.FC = props => { shouldShow = null, } = props - editor.registerPlugin(FloatingMenuPlugin({ + const plugin = FloatingMenuPlugin({ pluginKey, editor, - element: element.current as HTMLElement, + element, tippyOptions, shouldShow, - })) + }) - return () => { - editor.unregisterPlugin(pluginKey) - } + editor.registerPlugin(plugin) + return () => editor.unregisterPlugin(pluginKey) }, [ props.editor, - element.current, + element, ]) return ( -
+
{props.children}
)