import { FloatingMenuPlugin, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu' import React, { useEffect, useState, } from 'react' import { useCurrentEditor } from './Context.js' type Optional = Pick, K> & Omit export type FloatingMenuProps = Omit, 'element'> & { className?: string, children: React.ReactNode } export const FloatingMenu = (props: FloatingMenuProps) => { const [element, setElement] = useState(null) const { editor: currentEditor } = useCurrentEditor() useEffect(() => { if (!element) { return } if (props.editor?.isDestroyed || currentEditor?.isDestroyed) { return } const { pluginKey = 'floatingMenu', editor, tippyOptions = {}, shouldShow = null, } = props const menuEditor = editor || currentEditor if (!menuEditor) { console.warn('FloatingMenu component is not rendered inside of an editor component or does not have editor prop.') return } const plugin = FloatingMenuPlugin({ pluginKey, editor: menuEditor, element, tippyOptions, shouldShow, }) menuEditor.registerPlugin(plugin) return () => menuEditor.unregisterPlugin(pluginKey) }, [ props.editor, currentEditor, element, ]) return (
{props.children}
) }