question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for options in .focus()

See original GitHub issue

Hey @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:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
theKasheycommented, Sep 12, 2019

Drilling a new prop thought all components would be not super easy, however - do we need a new prop?

  • returnFocus=false do not return focus
  • returnFocus=true return focus
  • returnFocus="smooth" (however, sounds weird) - this behavior.
  • returnFocus="preventScroll" sounds less weird, but is too concrete.

Any other ideas in mind?

0reactions
klimashkincommented, Sep 13, 2019

There seem to be focus-options-polyfill for edge/safari

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTMLElement.focus() - Web APIs | MDN
The HTMLElement.focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will ...
Read more >
Use Focus on your iPhone or iPad - Apple Support
iPhone screen showing how to create a custom Focus. Go to Settings > Focus. Tap the Add button , then tap Custom or...
Read more >
calvellido/focus-options-polyfill - GitHub
JavaScript polyfill for focusOptions , an optional property passed to HTMLElement.focus() that contains the preventScroll boolean, which controls whether the ...
Read more >
Understanding Success Criterion 3.2.1: On Focus | WAI - W3C
When a field receives focus, a help dialog window describing the field and providing options opens. As a keyboard user tabs through the...
Read more >
.focus() | jQuery API Documentation
As the .focus() method is just a shorthand for .on( "focus", handler ) , detaching is possible using .off( " ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found