MCPcopy Create free account
hub / github.com/adobe/react-spectrum / getFirstInScope

Function getFirstInScope

packages/react-aria/src/focus/FocusScope.tsx:557–574  ·  view source on GitHub ↗
(scope: Element[], tabbable = true)

Source from the content-addressed store, hash-verified

555}
556
557function 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
576function focusFirstInScope(scope: Element[], tabbable: boolean = true) {
577 focusElement(getFirstInScope(scope, tabbable));

Callers 2

focusFirstInScopeFunction · 0.85
getDefaultRestoreTargetFunction · 0.85

Calls 3

getScopeRootFunction · 0.85
getFocusableTreeWalkerFunction · 0.85
nextNodeMethod · 0.80

Tested by

no test coverage detected