Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove element.current from useEffect in BubbleMenu and FloatingMenu #2297

Merged
merged 6 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions packages/react/src/BubbleMenu.tsx
Original file line number Diff line number Diff line change
@@ -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<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
Expand All @@ -8,10 +10,14 @@ export type BubbleMenuProps = Omit<Optional<BubbleMenuPluginProps, 'pluginKey'>,
}

export const BubbleMenu: React.FC<BubbleMenuProps> = props => {
const element = useRef<HTMLDivElement>(null)
const [element, setElement] = useState<HTMLDivElement | null>(null)

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

if (props.editor.isDestroyed) {
return
}

Expand All @@ -22,24 +28,23 @@ export const BubbleMenu: React.FC<BubbleMenuProps> = 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 (
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}>
<div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
{props.children}
</div>
)
Expand Down
27 changes: 16 additions & 11 deletions packages/react/src/FloatingMenu.tsx
Original file line number Diff line number Diff line change
@@ -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<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
Expand All @@ -8,10 +10,14 @@ export type FloatingMenuProps = Omit<Optional<FloatingMenuPluginProps, 'pluginKe
}

export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
const element = useRef<HTMLDivElement>(null)
const [element, setElement] = useState<HTMLDivElement | null>(null)

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

if (props.editor.isDestroyed) {
return
}

Expand All @@ -22,24 +28,23 @@ export const FloatingMenu: React.FC<FloatingMenuProps> = 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 (
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}>
<div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
{props.children}
</div>
)
Expand Down