tiptap/packages/react/src/FloatingMenu.tsx

38 lines
862 B
TypeScript
Raw Normal View History

2021-04-01 21:55:19 +08:00
import React, { useEffect, useRef } from 'react'
import { FloatingMenuPlugin, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu'
2021-04-01 21:55:19 +08:00
export type FloatingMenuProps = Omit<FloatingMenuPluginProps, 'element'> & {
className?: string,
}
export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
const element = useRef<HTMLDivElement>(null)
useEffect(() => {
const {
2021-08-13 18:33:06 +08:00
pluginKey,
editor,
tippyOptions,
shouldShow,
} = props
2021-04-01 21:55:19 +08:00
editor.registerPlugin(FloatingMenuPlugin({
2021-08-13 18:33:06 +08:00
pluginKey,
2021-04-01 21:55:19 +08:00
editor,
element: element.current as HTMLElement,
2021-04-16 18:42:56 +08:00
tippyOptions,
shouldShow,
2021-04-01 21:55:19 +08:00
}))
return () => {
2021-08-13 18:33:06 +08:00
editor.unregisterPlugin(pluginKey)
2021-04-01 21:55:19 +08:00
}
}, [])
return (
2021-04-01 23:55:32 +08:00
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}>
2021-04-01 21:55:19 +08:00
{props.children}
</div>
)
}