Support for options in .focus()
See original GitHub issueHey @theKashey, I am looking at adding support for focus options when calling .focus() in returnFocus.
See here: https://github.com/theKashey/react-focus-lock/blob/master/src/Lock.js#L60
The reason for that is that currently when the focus gets returned, if the page has been scrolled down and the element supposed to receive the focus back is not in view anymore, the browser will scroll to reveal that element being focused.
I understand that setting the returning the focus is absolutely important for accessibility and I wouldn’t change that. However, it can be a bit disorientating for users to be scrolled to a difference place for what seems to be no apparent reason (to them).
Most latest browsers actually support an option to disable that behaviour if needed.
See the preventScroll option (false by default) on mdn.
Here’s the current support for it. I think support is not too much of an issue as this can be seen as an enhancement anyway.
I actually need this feature as part of react-focus-on but I know the code for the returnFocus handler actually lives in react-focus-lock.
Would you be willing to add a new prop returnFocusOptions to support that optional behaviour?
If so, I can draft you a PR today.
Let me know!
✌️
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:9 (8 by maintainers)

Top Related StackOverflow Question
Drilling a new prop thought all components would be not super easy, however - do we need a new prop?
returnFocus=falsedo not return focusreturnFocus=truereturn focusreturnFocus="smooth"(however, sounds weird) - this behavior.returnFocus="preventScroll"sounds less weird, but is too concrete.Any other ideas in mind?
There seem to be focus-options-polyfill for edge/safari