react-spectrum icon indicating copy to clipboard operation
react-spectrum copied to clipboard

Prevent focus on press of a button

Open ritz078 opened this issue 1 year ago โ€ข 3 comments

Provide a general summary of the feature here

I am developing a rich text editor. When text is selected and a formatting button in the toolbar is clicked, the mobile keyboard closes because the focus shifts to the button. To prevent this, I tried adding event.preventDefault() to onMouseDown, but this doesn't work because usePress employs various other events to change focus, making onMouseDown inaccessible.

It will be great to have a prop named preventFocusOnPress if we cannot have onMouseDown

๐Ÿค” Expected Behavior?

There should be a way to prevent focus on buttons.

๐Ÿ˜ฏ Current Behavior

There is no way to prevent focus on click of a button

๐Ÿ’ Possible Solution

No response

๐Ÿ”ฆ Context

In text editors, even when you click a button, the focus should not shift from the main textbox but there is no way to do that in the current button hooks.

๐Ÿ’ป Examples

No response

๐Ÿงข Your Company/Team

PSPDFKit

๐Ÿ•ท Tracking Issue

No response

ritz078 avatar Jun 12 '24 15:06 ritz078

We actually have preventFocusOnPress in useButton already but it is undocumented since it was more for specific use cases (calendar cells, combobox button, etc) and we didn't want it to be misused. I can bring it up with the team to see if we want to pivot on that and expose it fully.

LFDanLu avatar Jun 12 '24 16:06 LFDanLu

Talked with the team, we are ok with exposing it as public api, will need to document it and mention that it should only be used in particular use cases.

LFDanLu avatar Jun 12 '24 22:06 LFDanLu

This is great. Thank you for taking the time and coming up with a solution this fast.

ritz078 avatar Jun 13 '24 04:06 ritz078