Skip to content

Commit 359fbd0

Browse files
committed
Add example showing how to do focus delegation in js
1 parent 46cc66e commit 359fbd0

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

index.bs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ The {{Element/seqNavSearch()}} method must follow these steps:
435435

436436
</div>
437437

438-
<div class=example>
438+
<div class=example id=focus-only>
439439
The following code changes the behavior of spatial navigation
440440
from scrolling when there is no focusable element visible,
441441
to jumping to focusable elements even when they are not visible.
@@ -457,6 +457,32 @@ The {{Element/seqNavSearch()}} method must follow these steps:
457457
</code></pre>
458458
</div>
459459

460+
<div class=example id=delegation>
461+
The following code changes the behavior of spatial navigation
462+
so that when a scroll container would get focused,
463+
if it has at least one visible focusable descendant,
464+
the focus is automatically transfered to it.
465+
466+
<pre><code highlight=javascript>
467+
document.addEventListener("navbeforefocus", function(e) {
468+
e.preventDefault();
469+
470+
var e1 = e.relatedTarget;
471+
while (e1.isSameNode(e1.getSpatnavContainer())) {
472+
var areas = e1.focusableAreas();
473+
474+
if (areas.length == 0)) { break; }
475+
476+
e1 = e1.spatNavSearch({
477+
dir: e.dir,
478+
candidates: areas
479+
});
480+
}
481+
e1.focus();
482+
});
483+
</code></pre>
484+
</div>
485+
460486

461487
<h2 id="events-navigationevent">
462488
Navigation Events</h2>

0 commit comments

Comments
 (0)