BlockNote icon indicating copy to clipboard operation
BlockNote copied to clipboard

Unable to have "focus stealing" components in Suggestion menu and formatting toolbar

Open clementprevot opened this issue 1 year ago • 5 comments

Describe the bug Hi!

I was trying to fully customize the UI of Blocknote by first replacing the Suggestion menu component (still keeping the controller though).

I was using my own design system component (Fractal, based on RadixUI) to build the slash menu and everything worked fine at first.
But then, I wanted to support "submenus" still using one of my components based on the Popover of RadixUI.
It occurs that RadixUI is using focus trap in there Popover component and this caused an issue with BlockNote: as soon as I hovered the trigger of the Popover, the whole suggestion menu disappeared.

I dig a bit in the BlockNote source code (cloning the repo in local and testing different things in the playground) and quickly realized that it came from this bit of code: when the Popover is shown, RadixUI steals the focus, which in turn triggers a blur event in the suggestion menu plugin which in turn completely hide the suggestion menu.

Getting rid of RadixUI (and thus the focus trap) made the sub-menu display properly, but the first click on an item of the submenu didn't triggered the "onItemClick" function but a blur. However a second click did worked properly 🤔.

To Reproduce Here is a StackBlitz with the full reproduction of the custom suggestion menu that is using Fractal's menu and submenus: https://stackblitz.com/edit/vitejs-vite-x7ytx5?file=src%2FApp.tsx

Misc

  • Node version: 18.9.1
  • Package manager: Yarn 4.1.1
  • Browser: Opera 107.0.5045.21

clementprevot avatar Mar 19 '24 14:03 clementprevot

Thanks for filing this. Would it be possible to prevent the Radix UI focus behavior? e.g.: https://github.com/radix-ui/primitives/discussions/915

The thing is, you probably do want the editor to remain focused (instead of the menu), so that the user can start typing and filter results

YousefED avatar Mar 20 '24 09:03 YousefED

Sadly we can't remove the focus trap behavior when using Radix.

Maybe having some sort of freeze/unfreeze method like in the side menu would be an acceptable solution?

clementprevot avatar Mar 20 '24 10:03 clementprevot

Sadly we can't remove the focus trap behavior when using Radix.

Why not? Doesn't the method I linked to work?

Maybe having some sort of freeze/unfreeze method like in the side menu would be an acceptable solution?

If we want to support type-to-filter, then I don't think any solution that removes the focus from the editor is viable

YousefED avatar Mar 20 '24 11:03 YousefED

@clementprevot Whilst working on https://github.com/TypeCellOS/BlockNote/pull/652 I noticed it's probably because the Radix Popover uses a Portal. You could try disabling the portal for your popover.

As part of the #652 PR I'll also investigate an issue if you need to keep using portals, and make it easier to integrate Radix / Shadcn

YousefED avatar Mar 26 '24 16:03 YousefED

The suggestion menu issue is still unresolved since we need the editor to be focused for the user to type queries, but you might be able to focus formatting toolbar elements now that #718 is merged.

matthewlipski avatar Jun 11 '24 10:06 matthewlipski