Repository navigation
Description
For a while, @robdodson and I have been noting use cases where setting the focus navigation start point, as opposed to the active element, would be preferable.
One motivating example is skip links - currently the technique may require setting focus to a non-interactive element purely to effectively set the focus navigation start point.
Another example is focus management for transient UI - for example, a side menu which appears as a result of a keyboard shortcut. It may not be appropriate to focus any specific element in the newly visible UI, but the user should be able to easily move focus within that UI, even if the UI is not modal.
Strawman API proposal:
el.focus({ setActiveElement: false });
This would unfocus the current active element, and set the focus navigation start point to el.
Additional naming proposals from @othermaciej in #5326 (comment):
element.setSequentialFocusStartPoint();
element.startSequentialFocusHere();
element.setFocusStartPoint();
document.setFocusStartPoint(element); // maybe more obvious that this clears focus,
// & if there was a getter it'd be on documentMore name suggestions from @muan in #5326 (comment)
document.setSequentialFocusStartingPoint(element);
document.setTabFocusStartingPoint(element);
document.setSequentialNavigationStartingPoint(element);
document.setNavigationStartingPoint(element);Another example from @muan in #5326 (comment):
I've been working on adding focus management to the file list on GitHub's repository page. Currently when user navigates through the file directories with a keyboard, each navigation drops user's focus back to the top of the page.
The most basic solution would be to set the file table as the start point. However, with the current recommended pattern, we have to go through 5 steps:
- Setting
tabindex="-1"on the file table Call focus()on the file table- Ensure focus outline does not apply to this element
- Install a one time blur handler to remove the tabindex
With this new API, it'll be:
- Call
table.focus({ setActiveElement: false })
This one line solution would immediately make the experience 10 times better.
Activity
hober commentedon Mar 2, 2020
cc @whatwg/a11y
domenic commentedon Mar 2, 2020
Can you describe the negative impacts of setting focus to a non-interactive element? I kind of was under the impression that's why almost everything is programmatically focusable, is so as to accomplish the use cases you describe here. But I guess something is not good enough about the existing solution?
I worry about having a separate active element and focus navigation starting point, which seems like it would be confusing. So I think it's worth getting a sense of what's wrong with the current coupling of the two so we can evaluate the tradeoff.
patrickhlauke commentedon Mar 2, 2020
for clarity, what is the focus navigation start point? the equivalent of the reading position if you were using a screen reader? is it something internal to the browser?
domenic commentedon Mar 3, 2020
https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-navigation-starting-point
alice commentedon Mar 3, 2020
Most things are not programmatically focusable, unless you add
tabindex=-1, so I'm not sure what you mean here.This would work on any element, without requiring an opt-in on the element.
It can trigger confusing focus styling/indication - both from
:focusbut additionally in some situations focus indication cannot be opted out of (such as when an operating system preference to always show focus is enabled).Focus indication is only helpful (IMO) when the focused element has some kind of keyboard interactivity.
Apologies, I was unclear about my intent there: my intent was that it would still blur the active element, so
document.activeElementwould be body.domenic commentedon Mar 3, 2020
Right, I meant if you add that. So we're comparing this new primitive vs. using the existing focus primitive on a
tabindex=-1element.:focus-visibleis meant to handle this, right?In the case of skip links (for example), it seems like the keyboard interactivity is "you can use the keyboard to navigate focus within the main content". E.g. pressing Tab takes you to the first focusable piece of main content. Similarly for side menus.
Perhaps I'm not understanding what you mean by keyboard interactivity?
I see. This does seem to reduce the potential confusion by making it similar to how browsers seem to behave in the existing case, e.g. of clicking on a non-click-focusable element. So, this complexity does already exist in the platform.
The question is whether it's worth exposing this complexity to JavaScript, and thus letting authors trigger it instead of users. I can see how when used for good, it probably aligns with user expectations. But it feels a bit like we're solving the problem of "people are using
:focusinstead of the new:focus-visibletechnology" by introducing yet another new technology and hoping they'll use that instead?Ultimately what I'm not quite grasping is why it's better for users to have the body focused than to have the container element focused, in these cases. This is likely just a matter of me not having enough experience in these areas, so please take all of this in the spirit of me trying to strenghten your case, and not just being skeptical or resistant to change.
bkardell commentedon Mar 3, 2020
As in an interactive control that can be/is operated via keyboard...
othermaciej commentedon Mar 3, 2020
Setting aside whether this functionality is useful or necessary, I think a way to set focus navigation start point without actually focusing anything should be a new method, not a new parameter to
focus. The proposal overloadsfocusbeyond its plain meaning. Often in such cases a new method is better. Furthermore, whole new methods are easier to feature test for, and it seems likely this functionality would need fallback when not supported.alice commentedon Mar 3, 2020
@othermaciej
The reason I proposed overloading
focus()is that initially my thought was thatfocus()on an unfocusable element should have this behaviour, but that would break any code that assume that that should be a complete no-op.I take your point about overloading focus beyond its current meaning, but in practice
focus()is already being used this way when focus is sent to a non-interactive element in order to simulate this exact behaviour.Adding a new method is definitely a possibility, although it risks being stuck forever in a naming bikeshed, for what I see as debatable value.
@domenic
:focus-visibleheuristics can be "fooled" by using keyboard shortcuts to show modal UI:Suppose you use a keyboard shortcut in a page to show a piece of modal UI, and focus is moved to a container div in order to simulate moving focus navigation start point to that div. Should
:focus-visiblematch, or not? How could you know reliably?I agree with @bkardell's framing. Meaningful keyboard interactivity in this case is, approximately, "handles keyboard events" (modulo event delegation).
alice commentedon Mar 3, 2020
@othermaciej Apologies for not addressing this in my comment above:
This is a good point.
The fallback, I believe, would default to either focusing the element (if it is focusable), or a no-op (if it is not).
Since the latter is what we want to avoid, we would need to seriously consider how to address that if we didn't add a new method.
28 remaining items
Implement setSequentialFocusStartingPoint
Implement setSequentialFocusStartingPoint
Implement setSequentialFocusStartingPoint
Update selection.html
Bug 1854180 [wpt PR 42006] - Implement setSequentialFocusStartingPoin…
Bug 1854180 [wpt PR 42006] - Implement setSequentialFocusStartingPoin…
Bug 1854180 [wpt PR 42006] - Implement setSequentialFocusStartingPoin…
Bug 1854180 [wpt PR 42006] - Implement setSequentialFocusStartingPoin…
Implement setSequentialFocusStartingPoint
daun commentedon Mar 29, 2024
The snippet above by @Rich-Harris works great in Chrome & Firefox.
Safari apparently still requires some handholding. The snippet below, though verbose, seems to work well across browsers.
It works by creating an invisible empty node as the first child of the target element, focussing it, then removing it from the document. This will remove focus, but most modern browsers will remember where focus was lost and restore focus when initiating keyboard navigation again.
patrickhlauke commentedon Mar 29, 2024
note that this leads to focus resetting to the very start of the page when you're running the JAWS screen reader (just tested this with a throwaway codepen in Chrome/JAWS)
daun commentedon Mar 29, 2024
@patrickhlauke Good to know! I assume this would apply equally to the original suggestion of using
element.focus()directly followed byelement.blur()?patrickhlauke commentedon Mar 29, 2024
as noted here #5326 (comment) and yes still happens despite browser error correction
daun commentedon Mar 29, 2024
@patrickhlauke Interesting! We'll keep tightening the screws on our solution and report back 🔩
Bug 1854180 [wpt PR 42006] - Implement setSequentialFocusStartingPoin…