theKashey / react-focus-on

🎯 Solution for WAI ARIA compatible modal dialogs or full-screen tasks, you were looking for
MIT License
336 stars 14 forks source link

returnToFocus option #11

Closed benoitgrelard closed 5 years ago

benoitgrelard commented 5 years ago

Hi,

First, thanks for this great library. I was wondering why returnToFocus was hardcoded as true and not passed as an option to FocusLock like the others. (see https://github.com/theKashey/react-focus-on/blob/master/src/UI.tsx#L37)

The use case I am trying to provide is, by default let your library handle the return focus, but if the user passes a custom ref to focus when the modal gets close, focus on that instead.

The issue is that for this to work, I have to write my ref.focus() in something like setImmediate or requestAnimationFrame to wait for the normal returnToFocus to do its thing first (I'm doing this in onDeactivation callback). Instead I would like to set returnToFocus={false} when a custom ref is passed into my component.

I hope this make sense. I tested it with react-focus-lock and it worked well by setting returnToFocus={false} and focusing my own thing using onDeactivation callback.

Thanks 🙏

theKashey commented 5 years ago

Sounds legit 👍

theKashey commented 5 years ago

3.0.4

benoitgrelard commented 5 years ago

Wow, thanks for the quick turnaround! 👌