tiptap/packages/react/src/BubbleMenu.tsx

47 lines
1.1 KiB
TypeScript
Raw Normal View History

import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu'
import React, { useEffect, useState } from 'react'
2021-03-31 16:21:34 +08:00
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
export type BubbleMenuProps = Omit<Optional<BubbleMenuPluginProps, 'pluginKey'>, 'element'> & {
className?: string;
children: React.ReactNode;
updateDelay?: number;
};
2021-03-31 16:21:34 +08:00
export const BubbleMenu = (props: BubbleMenuProps) => {
const [element, setElement] = useState<HTMLDivElement | null>(null)
2021-03-31 16:21:34 +08:00
useEffect(() => {
if (!element) {
return
}
if (props.editor.isDestroyed) {
2021-10-12 17:38:01 +08:00
return
}
const {
pluginKey = 'bubbleMenu', editor, tippyOptions = {}, updateDelay, shouldShow = null,
} = props
2021-03-31 16:21:34 +08:00
const plugin = BubbleMenuPlugin({
updateDelay,
2021-03-31 16:21:34 +08:00
editor,
element,
pluginKey,
shouldShow,
tippyOptions,
})
2021-03-31 16:21:34 +08:00
editor.registerPlugin(plugin)
return () => editor.unregisterPlugin(pluginKey)
}, [props.editor, element])
2021-03-31 16:21:34 +08:00
return (
<div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
2021-03-31 16:21:34 +08:00
{props.children}
</div>
)
}