Skip to content

Programmatically setting focus navigation start point? #5326

Open

Description

@alice

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 document

More 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:

  1. Setting tabindex="-1" on the file table
  2. Call focus() on the file table
  3. Ensure focus outline does not apply to this element
  4. Install a one time blur handler to remove the tabindex

With this new API, it'll be:

  1. Call table.focus({ setActiveElement: false })

This one line solution would immediately make the experience 10 times better.

Activity

  1. hober commented on Mar 2, 2020

    @hober
    Contributor

    cc @whatwg/a11y

  2. domenic commented on Mar 2, 2020

    @domenic
    Member

    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.

  3. patrickhlauke commented on Mar 2, 2020

    @patrickhlauke
    Member

    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?

  4. domenic commented on Mar 3, 2020

    @domenic
    Member
  5. alice commented on Mar 3, 2020

    @alice
    ContributorAuthor

    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.

    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.

    Can you describe the negative impacts of setting focus to a non-interactive element?

    It can trigger confusing focus styling/indication - both from :focus but 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.

    I worry about having a separate active element and focus navigation starting point, which seems like it would be confusing.

    Apologies, I was unclear about my intent there: my intent was that it would still blur the active element, so document.activeElement would be body.

  6. domenic commented on Mar 3, 2020

    @domenic
    Member

    Most things are not programmatically focusable, unless you add tabindex=-1, so I'm not sure what you mean here.

    Right, I meant if you add that. So we're comparing this new primitive vs. using the existing focus primitive on a tabindex=-1 element.

    It can trigger confusing focus styling/indication - both from :focus but 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-visible is meant to handle this, right?

    Focus indication is only helpful (IMO) when the focused element has some kind of keyboard interactivity.

    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?

    Apologies, I was unclear about my intent there: my intent was that it would still blur the active element, so document.activeElement would be body.

    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 :focus instead of the new :focus-visible technology" 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.

  7. bkardell commented on Mar 3, 2020

    @bkardell
    Contributor

    Perhaps I'm not understanding what you mean by keyboard interactivity?

    As in an interactive control that can be/is operated via keyboard...

  8. othermaciej commented on Mar 3, 2020

    @othermaciej

    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 overloads focus beyond 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.

  9. alice commented on Mar 3, 2020

    @alice
    ContributorAuthor

    @othermaciej

    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 overloads focus beyond its plain meaning.

    The reason I proposed overloading focus() is that initially my thought was that focus() 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

    It can trigger confusing focus styling/indication - both from :focus but 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-visible is meant to handle this, right?

    1. Not in the case where the operating system preference to always show focus is enabled
    2. :focus-visible heuristics 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-visible match, or not? How could you know reliably?

    ... 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.

    I agree with @bkardell's framing. Meaningful keyboard interactivity in this case is, approximately, "handles keyboard events" (modulo event delegation).

  10. alice commented on Mar 3, 2020

    @alice
    ContributorAuthor

    @othermaciej Apologies for not addressing this in my comment above:

    seems likely this functionality would need fallback when not supported.

    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.

  11. 28 remaining items

  12. added a commit that references this issue on Sep 26, 2023
  13. daun commented on Mar 29, 2024

    @daun

    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.

    function setFocusStartPoint(element) {
     const focusElement = createInvisibleFocusElement();
     element.prepend(focusElement);
     focusElement.focus({ preventScroll: true });
     focusElement.remove();
    }
    
    function createInvisibleFocusElement() {
      const element = document.createElement('div');
      element.setAttribute('tabindex', '-1');
      element.style.position = 'absolute';
      element.style.width = '1px';
      element.style.height = '1px';
      element.style.overflow = 'hidden';
      element.style.clip = 'rect(1px, 1px, 1px, 1px)';
      element.style.clipPath = 'inset(50%)';
      element.style.outline = 'none';
      return element;
    }
  14. patrickhlauke commented on Mar 29, 2024

    @patrickhlauke
    Member

    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.

    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)

  15. daun commented on Mar 29, 2024

    @daun

    @patrickhlauke Good to know! I assume this would apply equally to the original suggestion of using element.focus() directly followed by element.blur()?

  16. patrickhlauke commented on Mar 29, 2024

    @patrickhlauke
    Member

    as noted here #5326 (comment) and yes still happens despite browser error correction

  17. daun commented on Mar 29, 2024

    @daun

    @patrickhlauke Interesting! We'll keep tightening the screws on our solution and report back 🔩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions