This commit is contained in:
Philipp Kühn
2021-12-22 12:27:10 +01:00
2 changed files with 32 additions and 22 deletions

View File

@@ -1,4 +1,6 @@
import React, { useEffect, useRef } from 'react' import React, {
useEffect, useState,
} from 'react'
import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu' import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu'
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K> type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
@@ -8,10 +10,14 @@ export type BubbleMenuProps = Omit<Optional<BubbleMenuPluginProps, 'pluginKey'>,
} }
export const BubbleMenu: React.FC<BubbleMenuProps> = props => { export const BubbleMenu: React.FC<BubbleMenuProps> = props => {
const element = useRef<HTMLDivElement>(null) const [element, setElement] = useState<HTMLDivElement | null>(null)
useEffect(() => { useEffect(() => {
if (!element.current) { if (!element) {
return
}
if (props.editor.isDestroyed) {
return return
} }
@@ -22,24 +28,23 @@ export const BubbleMenu: React.FC<BubbleMenuProps> = props => {
shouldShow = null, shouldShow = null,
} = props } = props
editor.registerPlugin(BubbleMenuPlugin({ const plugin = BubbleMenuPlugin({
pluginKey, pluginKey,
editor, editor,
element: element.current as HTMLElement, element,
tippyOptions, tippyOptions,
shouldShow, shouldShow,
})) })
return () => { editor.registerPlugin(plugin)
editor.unregisterPlugin(pluginKey) return () => editor.unregisterPlugin(pluginKey)
}
}, [ }, [
props.editor, props.editor,
element.current, element,
]) ])
return ( return (
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}> <div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
{props.children} {props.children}
</div> </div>
) )

View File

@@ -1,4 +1,6 @@
import React, { useEffect, useRef } from 'react' import React, {
useEffect, useState,
} from 'react'
import { FloatingMenuPlugin, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu' import { FloatingMenuPlugin, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu'
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K> type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
@@ -8,10 +10,14 @@ export type FloatingMenuProps = Omit<Optional<FloatingMenuPluginProps, 'pluginKe
} }
export const FloatingMenu: React.FC<FloatingMenuProps> = props => { export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
const element = useRef<HTMLDivElement>(null) const [element, setElement] = useState<HTMLDivElement | null>(null)
useEffect(() => { useEffect(() => {
if (!element.current) { if (!element) {
return
}
if (props.editor.isDestroyed) {
return return
} }
@@ -22,24 +28,23 @@ export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
shouldShow = null, shouldShow = null,
} = props } = props
editor.registerPlugin(FloatingMenuPlugin({ const plugin = FloatingMenuPlugin({
pluginKey, pluginKey,
editor, editor,
element: element.current as HTMLElement, element,
tippyOptions, tippyOptions,
shouldShow, shouldShow,
})) })
return () => { editor.registerPlugin(plugin)
editor.unregisterPlugin(pluginKey) return () => editor.unregisterPlugin(pluginKey)
}
}, [ }, [
props.editor, props.editor,
element.current, element,
]) ])
return ( return (
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}> <div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
{props.children} {props.children}
</div> </div>
) )