react-focus-lock icon indicating copy to clipboard operation
react-focus-lock copied to clipboard

Multiple modals and returnFocus

Open andraaspar opened this issue 6 years ago • 14 comments
trafficstars

Focus is not returned with multiple <FocusLock>s.

Sample CRA project: https://github.com/andraaspar/react-focus-lock-stack-missing

To reproduce:

  1. Click ‘Open modal 1’
  2. On the modal, click ‘Open modal 2’
  3. On the 2nd modal, click ‘X2’ → Focus is set to ‘Open modal 2’ instead of ‘X1’
  4. On the modal, click ‘X1’ → Focus is not returned to ‘Open modal 1’

andraaspar avatar Jun 12 '19 14:06 andraaspar

Just in time ;)

theKashey avatar Jun 12 '19 21:06 theKashey

I'm sorry? I'm not quite sure what you mean?

I tried to make it easy to reproduce. Did it make sense? Is it in scope for this library, or is it not?

andraaspar avatar Jun 13 '19 07:06 andraaspar

I mean that you created the issue just in time, when this package is under my attention.

theKashey avatar Jun 13 '19 07:06 theKashey

So - that's funny.

  • focus is returned when Lock is deactivated
  • when a new Lock is going active the old one ... got deactivated
  • it returns focus
  • and the new one read the "initial focus", not the one it was a moment ago...

theKashey avatar Jun 14 '19 11:06 theKashey

@theKashey: Thank you for looking into it.

andraaspar avatar Jun 14 '19 11:06 andraaspar

Issue resolved in v2.0.0. No API changes, except raising minimal React version to 16.8. I hope that's not a deal breaker for you.

theKashey avatar Jun 15 '19 09:06 theKashey

@theKashey: Thanks a lot for the quick fix! It works great!

andraaspar avatar Jul 09 '19 16:07 andraaspar

I’m experiencing this now, with 2.2.1. The ‘inner’ modal returns focus correctly to its trigger element in the ‘outer’ modal, but the ‘outer’ modal is unable to return focus to its trigger element on the page.

I also tried downgrading to 2.0.0 in case any regressions had been introduced since, unfortunately with no luck.

Haraldson avatar Mar 09 '20 17:03 Haraldson

Sounds like it's not working properly in deeply nested cases.

theKashey avatar Mar 09 '20 23:03 theKashey

To provide a bit more context to my use-case: My modals aren’t really structurally nested, they are both portaled to the same DOM element and become siblings, but virtually they are, indeed, nested.

Haraldson avatar Mar 10 '20 07:03 Haraldson

Actually, you can probably just close this issue again. Perhaps there could be a hint about this behavior in dev environments, but basically what happened is that I disable my initial trigger when it’s clicked so that it appears a certain way through the overlay. When the modal closes the button gets enabled again, but a timing issue (Framer Motion’s <AnimatePresence>) prevents it from being ready in time to receive focus.

Haraldson avatar Mar 10 '20 13:03 Haraldson

But are you able to resolve the issue, or focus-lock should add some timeout support to the return focus logic?

theKashey avatar Mar 11 '20 04:03 theKashey

It’s not super easy to solve it with the current tools provided with focus-lock, so maybe it would be a good idea to somehow have it trigger on an event, or after a timeout as you suggest.

Haraldson avatar Mar 16 '20 12:03 Haraldson

Sure. This might also resolve another issue with a too-fast move focus inside - https://github.com/theKashey/react-focus-on/issues/36

theKashey avatar Mar 17 '20 04:03 theKashey

This issue has been marked as "stale" because there has been no activity for 2 months. If you have any new information or would like to continue the discussion, please feel free to do so. If this issue got buried among other tasks, maybe this message will reignite the conversation. Otherwise, this issue will be closed in 7 days. Thank you for your contributions so far.

stale[bot] avatar Apr 30 '23 12:04 stale[bot]