Skip to content

Commit

Permalink
fix: Add editor dependency when registering BubbleMenuPlugin and …
Browse files Browse the repository at this point in the history
…`FloatingMenuPlugin` (#2018)

* Add `editor` dependency when registering `BubbleMenuPlugin`

When we are initializing editor via the `useEditor` hook with dependencies the `BubbleMenu` component is only registered the first time the editor is initialized.

Adding editor to the dependency array registering/unregistering the `BubbleMenuPlugin` fixes this. (I tested exactly this code in our project.)

I also added a check that ensures that the menu element referenced via the `useRef` is defined when registering the plugin - otherwise, there is no point in registering the plugin.

* Add `editor` dependency when registering `FloatingMenuPlugin`
  • Loading branch information
ValentaTomas authored Oct 12, 2021
1 parent 0e0b0b6 commit e9465ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 6 additions & 1 deletion packages/react/src/BubbleMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const BubbleMenu: React.FC<BubbleMenuProps> = props => {
const element = useRef<HTMLDivElement>(null)

useEffect(() => {
if (!element.current) return

const {
pluginKey = 'bubbleMenu',
editor,
Expand All @@ -29,7 +31,10 @@ export const BubbleMenu: React.FC<BubbleMenuProps> = props => {
return () => {
editor.unregisterPlugin(pluginKey)
}
}, [])
}, [
props.editor,
element.current,
])

return (
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}>
Expand Down
7 changes: 6 additions & 1 deletion packages/react/src/FloatingMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
const element = useRef<HTMLDivElement>(null)

useEffect(() => {
if (!element.current) return

const {
pluginKey = 'floatingMenu',
editor,
Expand All @@ -29,7 +31,10 @@ export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
return () => {
editor.unregisterPlugin(pluginKey)
}
}, [])
}, [
props.editor,
element.current,
])

return (
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}>
Expand Down

0 comments on commit e9465ec

Please sign in to comment.