Sequential focus navigation starting point (Tab from the last click or fragment target)

Open
#1,354 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
html, rust
Domain
frontend

Research direction

Start by reading BaseDocument focus handling and the Tab/Shift+Tab navigation code, then review the existing per-node cleanup for removed subtrees. Use the WPT sequential-focus-navigation-starting-point.tentative.html tests as a guide for cases that Blitz supports, and add corresponding tests to blitz-tests. Done means navigation starts from the specified point after clicks, focus changes, and fragment navigation, including when its node is removed.

Written by the indexing model from the issue text.

Description

(Sorry for the duplicate open, the previous is my work account, oops.)

Blitz doesn't track the HTML "sequential focus navigation starting point" (HTML 6.6.5), so Tab always starts from the focused element, or from the top of the document when nothing is focused.

In browsers the starting point is separate from focus. Focusing an element moves it there, following an in-page link moves it to the target, and clicking moves it to wherever you clicked, focusable or not. Tab and Shift+Tab then search from it. Reading it for Tab is part of the algorithm the spec says the user agent "must use" ("If there is a sequential focus navigation starting point defined and it is inside starting point, then let starting point be the sequential focus navigation starting point instead"), and so is setting it on fragment navigation ("User agents are required to set the sequential focus navigation starting point to the target element when navigating to a fragment"). Setting it on a click is optional ("the user agent could set it to the position of the user's click"), but Chrome has done it since Chrome 50 (CL 1718153002) and Firefox does too.

To reproduce:

<p><a href="#first">first link</a></p>
<p><a href="#middle">jump to the middle</a></p>
<div style="height: 1500px">Scroll down.</div>
<p id="middle">Click this sentence, or follow "jump to the middle", then press Tab.</p>
<p><a href="#next">next link</a></p>
<div style="height: 1500px"></div>
<p id="first">end</p>

Driving that through BaseDocument on 0.3.0-beta.3: clicking the middle sentence and pressing Tab focuses "first link" at the top; so does following "jump to the middle" (the viewport does scroll) and then Tab; and if "next link" was focused, clicking the sentence above it clears focus, and Tab goes back to "first link". In Firefox, Tab focuses "next link" in all three.

My thoughts on how to implement this are below in the pseudocode. This stores a location in reference to a node, because when removing a node, in order to match the WPT and Chromium behaviors, there needs to be a location referenced between nodes. That's why the before/after reference is included alongside the relation to a given node.

BaseDocument gets a start point next to focus_node_id: one node reference
plus where the point sits relative to it.

start point: unset (default) | <node> + at / before / after

on pointer down on content  -> at the node under the pointer
                               (only if the new config option is on)
on set_focus_to(node)       -> at node
on fragment navigation      -> at the target

on Tab / Shift+Tab:
    search forward (back for Shift+Tab) from the start point if set,
    else from focus, else the document start
        found     -> focus it (which also moves the start point)
        not found -> unset the start point (per spec)

when a node is removed (the same per-node cleanup that already clears
focus/hover/mousedown for a removed subtree):
    if the start point is at it, inside it, or anchored to it:
        previous sibling exists -> after <previous sibling>
        else next sibling       -> before <next sibling>
        else (only child)       -> at <parent>

Since focusing also sets it, Tab behaves as it does now whenever something is focused. The removal rule means that when the focused element (or something around it) is removed, Tab goes to the element after where it was and Shift+Tab to the one before. The HTML spec doesn't say what happens on removal, but this matches Chromium and what WPT's test checks.

Setting a start point on a click is the part the spec leaves optional, so I'd put that behind an option, on by default like incremental. Happy to drop the option if you'd rather not add one and have it be always on.

Most of WPT's sequential-focus-navigation-starting-point.tentative.html tests can carry over to blitz-tests as a starting point. There are 6 tests that use shadow DOM, slots, or positive tabindex ordering, which Blitz doesn't have and so can't carry over yet.

There is one edge case I'd accept for now: if the only child of a focusable element is removed, Shift+Tab skips that now-empty element. The start point becomes at the parent, and Shift+Tab searches from just before it, so the parent itself is skipped. From reading its code, Chromium does the same.

Beyond aligning with the spec, why this comes up for me is I'm embedding Blitz and wanting to enable handling of keyboard scrolling. The starting point being built into Blitz makes it much easier to implement. This can include things like scrollable elements (code blocks, wide <pre>, etc), or the page as a whole in general.

This overlaps with PR #1213. It would set the start point in the same pointerdown step where #1213 moves focus to the nearest focusable ancestor, and it has to run after that step. So clicking a span inside a focusable <div> focuses the div, but leaves the start point on the span (one of the WPT cases). Happy to build on top of #1213 or wait for it to land in main and build and submit a PR from there.

@nicoburns what do you think? Any thoughts? Want me to go ahead and build and submit it?

Dominant language
Rust
Stars
4.5k
Forks
281
Avg merge
6h 6m
Merged PRs (30d)
184

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DioxusLabs/blitz

All issues in DioxusLabs/blitz

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.