mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-22 16:17:50 +08:00
40 lines
986 B
TypeScript
40 lines
986 B
TypeScript
import React, { useEffect, useRef } from 'react'
|
|
import { FloatingMenuPlugin, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu'
|
|
|
|
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
|
|
|
|
export type FloatingMenuProps = Omit<Optional<FloatingMenuPluginProps, 'pluginKey'>, 'element'> & {
|
|
className?: string,
|
|
}
|
|
|
|
export const FloatingMenu: React.FC<FloatingMenuProps> = props => {
|
|
const element = useRef<HTMLDivElement>(null)
|
|
|
|
useEffect(() => {
|
|
const {
|
|
pluginKey = 'floatingMenu',
|
|
editor,
|
|
tippyOptions = {},
|
|
shouldShow = null,
|
|
} = props
|
|
|
|
editor.registerPlugin(FloatingMenuPlugin({
|
|
pluginKey,
|
|
editor,
|
|
element: element.current as HTMLElement,
|
|
tippyOptions,
|
|
shouldShow,
|
|
}))
|
|
|
|
return () => {
|
|
editor.unregisterPlugin(pluginKey)
|
|
}
|
|
}, [])
|
|
|
|
return (
|
|
<div ref={element} className={props.className} style={{ visibility: 'hidden' }}>
|
|
{props.children}
|
|
</div>
|
|
)
|
|
}
|