Skip to content

Commit 73d1888

Browse files
authored
Bdbch/6446 cleanup bubble menu resize (#6496)
* unregister window resize handler when bubble menu is destroyed * added changeset * added docs comment
1 parent ac897e7 commit 73d1888

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

.changeset/great-terms-jam.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@tiptap/extension-bubble-menu': patch
3+
'@tiptap/react': patch
4+
'@tiptap/vue-2': patch
5+
'@tiptap/vue-3': patch
6+
---
7+
8+
Fixed a bug where the global resize handler of the BubbleMenu plugin would not be unregistered on destroy

packages/extension-bubble-menu/src/bubble-menu-plugin.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,7 @@ export class BubbleMenuView implements PluginView {
254254
this.view.dom.addEventListener('dragstart', this.dragstartHandler)
255255
this.editor.on('focus', this.focusHandler)
256256
this.editor.on('blur', this.blurHandler)
257-
window.addEventListener('resize', () => {
258-
if (this.resizeDebounceTimer) {
259-
clearTimeout(this.resizeDebounceTimer)
260-
}
261-
262-
this.resizeDebounceTimer = window.setTimeout(() => {
263-
this.updatePosition()
264-
}, this.resizeDelay)
265-
})
257+
window.addEventListener('resize', this.resizeHandler)
266258

267259
this.update(view, view.state)
268260

@@ -279,6 +271,21 @@ export class BubbleMenuView implements PluginView {
279271
this.hide()
280272
}
281273

274+
/**
275+
* Handles the window resize event to update the position of the bubble menu.
276+
* It uses a debounce mechanism to prevent excessive updates.
277+
* The delay is defined by the `resizeDelay` property.
278+
*/
279+
resizeHandler = () => {
280+
if (this.resizeDebounceTimer) {
281+
clearTimeout(this.resizeDebounceTimer)
282+
}
283+
284+
this.resizeDebounceTimer = window.setTimeout(() => {
285+
this.updatePosition()
286+
}, this.resizeDelay)
287+
}
288+
282289
focusHandler = () => {
283290
// we use `setTimeout` to make sure `selection` is already updated
284291
setTimeout(() => this.update(this.editor.view))
@@ -464,6 +471,7 @@ export class BubbleMenuView implements PluginView {
464471
this.hide()
465472
this.element.removeEventListener('mousedown', this.mousedownHandler, { capture: true })
466473
this.view.dom.removeEventListener('dragstart', this.dragstartHandler)
474+
window.removeEventListener('resize', this.resizeHandler)
467475
this.editor.off('focus', this.focusHandler)
468476
this.editor.off('blur', this.blurHandler)
469477

0 commit comments

Comments
 (0)