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

FocusManager fails with buttons in safari and firefox

Open TrySound opened this issue 6 years ago • 10 comments

We need to find some solution to work around it both safari and firefox

Ref https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus

Example

<html>

<body>
<button tabindex="-1">Click me</button>

<script>

const button = document.querySelector("button")

// focus event is fixed with custom firing
button.addEventListener("mousedown", event => {
  if (event.currentTarget.tagName === 'BUTTON') {
    event.currentTarget.focus();
  }
});

button.addEventListener("focus", () => {
  console.log("focus");
});

// sadly blur is fired straight after focus
button.addEventListener("blur", () => {
  console.log('blur');
})

</script>
</body>

</html>

/cc @souporserious

TrySound avatar Jul 16 '18 20:07 TrySound

I like the idea of using onMouseUp and event.preventDefault. Maybe we can try that for now and see if anything weird comes up? I can't think of anything off the top of my head that using onMouseUp would break 🤔.

souporserious avatar Jul 16 '18 20:07 souporserious

onMouseUp doesn't work for me. I came to this solution after discussion with you

button.addEventListener('mousedown', event => {
  if (event.currentTarget.tagName === 'BUTTON') {
    event.preventDefault();
    event.currentTarget.focus();
  }
})

button.addEventListener('focus', () => {
  console.log('focus');
});

button.addEventListener('blur', () => {
  console.log('blur')
})

TrySound avatar Jul 17 '18 07:07 TrySound

Here's reproducing example. The problem now is iphone. My friend said blur doesn't work. After clicking at button test became focused and then nothing happend after clicking to whitespace.

https://codesandbox.io/s/ppy6kv0w67

I don't have iphone to test. @souporserious could you take a look?

TrySound avatar Jul 17 '18 08:07 TrySound

I had a little bit of time to look at this, but I'm not sure the best route to fix this 😞. I was able to get it working by listening to touchstart then removing it on blur, but it feels super gross 🤮.

https://codesandbox.io/s/j2jx97422v

souporserious avatar Jul 17 '18 17:07 souporserious

Yep, looks not the best. Even pointer events are not supported by safari :(

TrySound avatar Jul 17 '18 17:07 TrySound

@souporserious What if we will add cursor: pointer to documentElement or body on mouse down? At least for safari.

TrySound avatar Jul 17 '18 17:07 TrySound

Hmm not sure what you mean by cursor: pointer 🤔. Are you talking about styling?

souporserious avatar Jul 17 '18 21:07 souporserious

Yes, style="cursor: pointer" on html or body could enable clickability and probably blur.

TrySound avatar Jul 17 '18 21:07 TrySound

Oh interesting! I didn't know Safari would catch clicks if you add cursor: pointer. I'll try and investigate more when I get some time.

souporserious avatar Jul 17 '18 21:07 souporserious

It's just an idea. I'm not sure this will work.

TrySound avatar Jul 17 '18 22:07 TrySound