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

Fix: Don’t initialize tippy on requestAnimationFrame to avoid race conditions #1820

Merged
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
46 changes: 28 additions & 18 deletions packages/extension-bubble-menu/src/bubble-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export class BubbleMenuView {

public tippy: Instance | undefined

public tippyOptions?: Partial<Props>

public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({ state, from, to }) => {
const { doc, selection } = state
const { empty } = selection
Expand All @@ -59,7 +61,7 @@ export class BubbleMenuView {
editor,
element,
view,
tippyOptions,
tippyOptions = {},
shouldShow,
}: BubbleMenuViewProps) {
this.editor = editor
Expand All @@ -74,13 +76,10 @@ export class BubbleMenuView {
this.view.dom.addEventListener('dragstart', this.dragstartHandler)
this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
this.tippyOptions = tippyOptions
// Detaches menu content from its current parent
this.element.remove()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: I considered safe to detach the menu element because tippy also detaches the element on mount.

this.element.style.visibility = 'visible'

// We create tippy asynchronously to make sure that `editor.options.element`
// has already been moved to the right position in the DOM
requestAnimationFrame(() => {
this.createTooltip(tippyOptions)
})
}

mousedownHandler = () => {
Expand Down Expand Up @@ -113,17 +112,26 @@ export class BubbleMenuView {
this.hide()
}

createTooltip(options: Partial<Props> = {}) {
this.tippy = tippy(this.editor.options.element, {
duration: 0,
getReferenceClientRect: null,
content: this.element,
interactive: true,
trigger: 'manual',
placement: 'top',
hideOnClick: 'toggle',
...options,
})
createTooltip() {
if (this.tippy) {
return
}

const { element: editorElement } = this.editor.options

// Wait until editor element is attached to the document
if (editorElement.parentElement) {
this.tippy = tippy(editorElement, {
duration: 0,
getReferenceClientRect: null,
content: this.element,
interactive: true,
trigger: 'manual',
placement: 'top',
hideOnClick: 'toggle',
...this.tippyOptions,
})
}
}

update(view: EditorView, oldState?: EditorState) {
Expand All @@ -135,6 +143,8 @@ export class BubbleMenuView {
return
}

this.createTooltip()

// support for CellSelections
const { ranges } = selection
const from = Math.min(...ranges.map(range => range.$from.pos))
Expand Down
46 changes: 28 additions & 18 deletions packages/extension-floating-menu/src/floating-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export class FloatingMenuView {

public tippy: Instance | undefined

public tippyOptions?: Partial<Props>

public shouldShow: Exclude<FloatingMenuPluginProps['shouldShow'], null> = ({ state }) => {
const { selection } = state
const { $anchor, empty } = selection
Expand All @@ -50,7 +52,7 @@ export class FloatingMenuView {
editor,
element,
view,
tippyOptions,
tippyOptions = {},
shouldShow,
}: FloatingMenuViewProps) {
this.editor = editor
Expand All @@ -64,13 +66,10 @@ export class FloatingMenuView {
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
this.tippyOptions = tippyOptions
// Detaches menu content from its current parent
this.element.remove()
this.element.style.visibility = 'visible'

// We create tippy asynchronously to make sure that `editor.options.element`
// has already been moved to the right position in the DOM
requestAnimationFrame(() => {
this.createTooltip(tippyOptions)
})
}

mousedownHandler = () => {
Expand Down Expand Up @@ -99,17 +98,26 @@ export class FloatingMenuView {
this.hide()
}

createTooltip(options: Partial<Props> = {}) {
this.tippy = tippy(this.editor.options.element, {
duration: 0,
getReferenceClientRect: null,
content: this.element,
interactive: true,
trigger: 'manual',
placement: 'right',
hideOnClick: 'toggle',
...options,
})
createTooltip() {
if (this.tippy) {
return
}

const { element: editorElement } = this.editor.options

// Wait until editor element is attached to the document
if (editorElement.parentElement) {
this.tippy = tippy(editorElement, {
duration: 0,
getReferenceClientRect: null,
content: this.element,
interactive: true,
trigger: 'manual',
placement: 'right',
hideOnClick: 'toggle',
...this.tippyOptions,
})
}
}

update(view: EditorView, oldState?: EditorState) {
Expand All @@ -122,6 +130,8 @@ export class FloatingMenuView {
return
}

this.createTooltip()

const shouldShow = this.shouldShow?.({
editor: this.editor,
view,
Expand Down