Show & Hide BubbleMenu programmatically
See original GitHub issueWhat problem are you facing?
I am trying to make a prompt for link input and want to reuse the BubbleMenu plugin. I am using BubbleMenu to open a toolbar on selection, which contains a button for creating a link. I want to open a second BubbleMenu positioned at the selection when the user clicks the link toolbar button but I haven’t been able to come up with a way to do this. I know BubbleMenu has the shouldShow
option but I can’t figure out a way to use this to be toggled by a button press.
What’s the solution you would like to see?
It would be nice to expose the tooltip’s show & hide methods as editor commands so I could do something like this, editor.commands.showBubbleMenu(id)
.
What alternatives did you consider?
I am trying to get it to work with shouldShow but haven’t gotten anywhere. I am considering taking BubbleMenu and making a similar extension but with the ability to programmatically trigger the tooltip but I wanted to open an issue first.
Anything to add? (optional)
No response
Are you sponsoring us?
- Yes, I’m a sponsor. 💖
Issue Analytics
- State:
- Created 2 years ago
- Reactions:7
- Comments:15 (3 by maintainers)
I ended up implementing my own simple “Bubble Menu” to get it to follow React paradigms more closely (so that changes to “should show” are always rendered/reflected). If anyone is seeing this and happens to be using MaterialUI (or would want to use https://github.com/atomiks/tippyjs-react/, since that could easily be swapped for the
Popper
here), I’ve pasted my example below in case it helps.The implementation below has two main advantages:
open
prop. Any updates to the prop will re-render as you’d intend. (Resolves this issue.)appendTo: () => document.body
there as a workaround due to the way in which the element is dynamically created/destroyed via tippy inside Tiptap, thereby preventing interactivity (see https://github.com/ueberdosis/tiptap/issues/2292).Minimal version of the component code:
which can be used nearly identically to the
BubbleMenu
from@tiptap/react
, like:where
shouldShow
is whatever you want it to be (e.g. based on some React state variable and/oreditor
state)Here’s a
floating-ui
implementation of @sjdemartini’sControlledBubbleMenu
:Usage: