mirror of
https://github.com/ueberdosis/tiptap.git
synced 2024-12-22 08:07:50 +08:00
8c6751f0c6
* chore: add precommit hook for eslint fixes, fix linting issues * chore: add eslint import sort plugin
53 lines
1.1 KiB
TypeScript
53 lines
1.1 KiB
TypeScript
import { FloatingMenuPlugin, FloatingMenuPluginProps } from '@tiptap/extension-floating-menu'
|
|
import React, {
|
|
useEffect, useState,
|
|
} from 'react'
|
|
|
|
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>
|
|
|
|
export type FloatingMenuProps = Omit<Optional<FloatingMenuPluginProps, 'pluginKey'>, 'element'> & {
|
|
className?: string,
|
|
children: React.ReactNode
|
|
}
|
|
|
|
export const FloatingMenu = (props: FloatingMenuProps) => {
|
|
const [element, setElement] = useState<HTMLDivElement | null>(null)
|
|
|
|
useEffect(() => {
|
|
if (!element) {
|
|
return
|
|
}
|
|
|
|
if (props.editor.isDestroyed) {
|
|
return
|
|
}
|
|
|
|
const {
|
|
pluginKey = 'floatingMenu',
|
|
editor,
|
|
tippyOptions = {},
|
|
shouldShow = null,
|
|
} = props
|
|
|
|
const plugin = FloatingMenuPlugin({
|
|
pluginKey,
|
|
editor,
|
|
element,
|
|
tippyOptions,
|
|
shouldShow,
|
|
})
|
|
|
|
editor.registerPlugin(plugin)
|
|
return () => editor.unregisterPlugin(pluginKey)
|
|
}, [
|
|
props.editor,
|
|
element,
|
|
])
|
|
|
|
return (
|
|
<div ref={setElement} className={props.className} style={{ visibility: 'hidden' }}>
|
|
{props.children}
|
|
</div>
|
|
)
|
|
}
|