theKashey / react-focus-lock

It is a trap! A lock for a Focus. 🔓
MIT License
1.27k stars 67 forks source link

Lock focusing on first focusable element when drawer component is closed #318

Closed jamestalmond closed 1 month ago

jamestalmond commented 1 month ago

I've got a strange issue which involves a focusable element being out of view within a dismissable container. See the screen recording below followed by an explanation:

https://github.com/user-attachments/assets/e7afc010-e20b-4485-9941-a863787d881c

My onClick function to dismiss the drawer isn't doing anything fancy, just setting state in React which shows and hides the drawer.

The issue disappears when the focus lock is removed from the component entirely. The problem doesn't occur if the first focusable element is within view.

I'm using autofocus={false} on the focus lock, but that doesn't solve the problem.

Any thoughts?

theKashey commented 1 month ago

This could be related to the "scroll on focus" browser behaviour. Please try setting focusOptions={{ preventScroll: true }} to the Lock.

focusOptions marked as @deprecated only to inform you to be careful.

jamestalmond commented 1 month ago

Looks like this fixed it! Thanks!