Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(bubble-menu): always take the latest shouldShow callback, expose default implementation #5865

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions packages/extension-bubble-menu/src/bubble-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,18 @@ export class BubbleMenuView {

private updateDebounceTimer: number | undefined

public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({
public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null>

/**
* The default `shouldShow` function.
*/
public static shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({
view,
state,
from,
to,
element,
editor,
}) => {
const { doc, selection } = state
const { empty } = selection
Expand All @@ -94,11 +101,11 @@ export class BubbleMenuView {
// When clicking on a element inside the bubble menu the editor "blur" event
// is called and the bubble menu item is focussed. In this case we should
// consider the menu as part of the editor and keep showing the menu
const isChildOfMenu = this.element.contains(document.activeElement)
const isChildOfMenu = element.contains(document.activeElement)

const hasEditorFocus = view.hasFocus() || isChildOfMenu

if (!hasEditorFocus || empty || isEmptyTextBlock || !this.editor.isEditable) {
if (!hasEditorFocus || empty || isEmptyTextBlock || !editor.isEditable) {
return false
}

Expand All @@ -120,6 +127,8 @@ export class BubbleMenuView {

if (shouldShow) {
this.shouldShow = shouldShow
} else {
this.shouldShow = BubbleMenuView.shouldShow
}

this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
Expand Down
20 changes: 11 additions & 9 deletions packages/react/src/BubbleMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BubbleMenuPlugin, BubbleMenuPluginProps } from '@tiptap/extension-bubble-menu'
import { BubbleMenuPlugin, BubbleMenuPluginProps, BubbleMenuView } from '@tiptap/extension-bubble-menu'
import React, { useEffect, useState } from 'react'

import { useCurrentEditor } from './Context.js'
Expand All @@ -15,6 +15,9 @@ export type BubbleMenuProps = Omit<Optional<BubbleMenuPluginProps, 'pluginKey'>,
export const BubbleMenu = (props: BubbleMenuProps) => {
const [element, setElement] = useState<HTMLDivElement | null>(null)
const { editor: currentEditor } = useCurrentEditor()
const latestProps = React.useRef(props)

latestProps.current = props

useEffect(() => {
if (!element) {
Expand All @@ -25,24 +28,23 @@ export const BubbleMenu = (props: BubbleMenuProps) => {
return
}

const {
pluginKey = 'bubbleMenu', editor, tippyOptions = {}, updateDelay, shouldShow = null,
} = props

const menuEditor = editor || currentEditor
const menuEditor = props.editor || currentEditor
const pluginKey = latestProps.current.pluginKey ?? 'bubbleMenu'

if (!menuEditor) {
console.warn('BubbleMenu component is not rendered inside of an editor component or does not have editor prop.')
return
}

const plugin = BubbleMenuPlugin({
updateDelay,
updateDelay: latestProps.current.updateDelay,
editor: menuEditor,
element,
pluginKey,
shouldShow,
tippyOptions,
shouldShow: (...args) => {
return (latestProps.current.shouldShow ?? BubbleMenuView.shouldShow)?.(...args) ?? false
},
tippyOptions: latestProps.current.tippyOptions,
})

menuEditor.registerPlugin(plugin)
Expand Down