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

Function getFocusableTreeWalker

packages/react-aria/src/focus/FocusScope.tsx:893–950  ·  view source on GitHub ↗
(
  root: Element,
  opts?: FocusManagerOptions,
  scope?: Element[]
)

Source from the content-addressed store, hash-verified

891 * that matches all focusable/tabbable elements.
892 */
893export function getFocusableTreeWalker(
894 root: Element,
895 opts?: FocusManagerOptions,
896 scope?: Element[]
897): ShadowTreeWalker | TreeWalker {
898 let filter = opts?.tabbable ? isTabbable : isFocusable;
899
900 // Ensure that root is an Element or fall back appropriately
901 let rootElement = root?.nodeType === Node.ELEMENT_NODE ? (root as Element) : null;
902
903 // Determine the document to use
904 let doc = getOwnerDocument(rootElement);
905
906 // Create a TreeWalker, ensuring the root is an Element or Document
907 let walker = createShadowTreeWalker(doc, root || doc, NodeFilter.SHOW_ELEMENT, {
908 acceptNode(node) {
909 // Skip nodes inside the starting node.
910 if (nodeContains(opts?.from, node)) {
911 return NodeFilter.FILTER_REJECT;
912 }
913
914 if (
915 opts?.tabbable &&
916 (node as Element).tagName === 'INPUT' &&
917 (node as HTMLInputElement).getAttribute('type') === 'radio'
918 ) {
919 // If the radio is in a form, we can get all the other radios by name
920 if (!isTabbableRadio(node as HTMLInputElement)) {
921 return NodeFilter.FILTER_REJECT;
922 }
923 // If the radio is in the same group as the current node and none are selected, we can skip it
924 if (
925 (walker.currentNode as Element).tagName === 'INPUT' &&
926 (walker.currentNode as HTMLInputElement).type === 'radio' &&
927 (walker.currentNode as HTMLInputElement).name === (node as HTMLInputElement).name
928 ) {
929 return NodeFilter.FILTER_REJECT;
930 }
931 }
932
933 if (
934 filter(node as Element) &&
935 (!scope || isElementInScope(node as Element, scope)) &&
936 (!opts?.accept || opts.accept(node as Element))
937 ) {
938 return NodeFilter.FILTER_ACCEPT;
939 }
940
941 return NodeFilter.FILTER_SKIP;
942 }
943 });
944
945 if (opts?.from) {
946 walker.currentNode = opts.from;
947 }
948
949 return walker;
950}

Callers 15

onTriggerKeyDownFunction · 0.90
focusFunction · 0.90
onKeyDownCaptureFunction · 0.90
onKeyDownFunction · 0.90
tabFunction · 0.90
focusFunction · 0.90
onKeyDownCaptureFunction · 0.90
onKeyDownFunction · 0.90
getNextElementFunction · 0.90
updateFunction · 0.90
focusLastFunction · 0.90
findNextSegmentFunction · 0.90

Calls 2

getOwnerDocumentFunction · 0.90
createShadowTreeWalkerFunction · 0.90

Tested by

no test coverage detected