(scope: Element[], tabbable = true)
| 555 | } |
| 556 | |
| 557 | function getFirstInScope(scope: Element[], tabbable = true): FocusableElement | null { |
| 558 | let sentinel = scope[0].previousElementSibling!; |
| 559 | let scopeRoot = getScopeRoot(scope); |
| 560 | let walker = getFocusableTreeWalker(scopeRoot, {tabbable}, scope); |
| 561 | walker.currentNode = sentinel; |
| 562 | let nextNode = walker.nextNode(); |
| 563 | |
| 564 | // If the scope does not contain a tabbable element, use the first focusable element. |
| 565 | if (tabbable && !nextNode) { |
| 566 | scopeRoot = getScopeRoot(scope); |
| 567 | walker = getFocusableTreeWalker(scopeRoot, {tabbable: false}, scope); |
| 568 | walker.currentNode = sentinel; |
| 569 | nextNode = walker.nextNode(); |
| 570 | } |
| 571 | |
| 572 | // TreeWalker.nextNode() returns null when the scope contains no focusable element. |
| 573 | return nextNode as FocusableElement | null; |
| 574 | } |
| 575 | |
| 576 | function focusFirstInScope(scope: Element[], tabbable: boolean = true) { |
| 577 | focusElement(getFirstInScope(scope, tabbable)); |
no test coverage detected