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

Function createFocusManager

packages/react-aria/src/focus/FocusScope.tsx:901–996  ·  view source on GitHub ↗
(
  ref: RefObject<Element | null>,
  defaultOptions: FocusManagerOptions = {}
)

Source from the content-addressed store, hash-verified

899 * Creates a FocusManager object that can be used to move focus within an element.
900 */
901export function createFocusManager(
902 ref: RefObject<Element | null>,
903 defaultOptions: FocusManagerOptions = {}
904): FocusManager {
905 return {
906 focusNext(opts: FocusManagerOptions = {}) {
907 let root = ref.current;
908 if (!root) {
909 return null;
910 }
911 let {
912 from,
913 tabbable = defaultOptions.tabbable,
914 wrap = defaultOptions.wrap,
915 accept = defaultOptions.accept
916 } = opts;
917 let node = from || getActiveElement(getOwnerDocument(root));
918 let walker = getFocusableTreeWalker(root, {tabbable, accept});
919 if (nodeContains(root, node)) {
920 walker.currentNode = node!;
921 }
922 let nextNode = walker.nextNode() as FocusableElement;
923 if (!nextNode && wrap) {
924 walker.currentNode = root;
925 nextNode = walker.nextNode() as FocusableElement;
926 }
927 if (nextNode) {
928 focusElement(nextNode, true);
929 }
930 return nextNode;
931 },
932 focusPrevious(opts: FocusManagerOptions = defaultOptions) {
933 let root = ref.current;
934 if (!root) {
935 return null;
936 }
937 let {
938 from,
939 tabbable = defaultOptions.tabbable,
940 wrap = defaultOptions.wrap,
941 accept = defaultOptions.accept
942 } = opts;
943 let node = from || getActiveElement(getOwnerDocument(root));
944 let walker = getFocusableTreeWalker(root, {tabbable, accept});
945 if (nodeContains(root, node)) {
946 walker.currentNode = node!;
947 } else {
948 let next = last(walker);
949 if (next) {
950 focusElement(next, true);
951 }
952 return next ?? null;
953 }
954 let previousNode = walker.previousNode() as FocusableElement;
955 if (!previousNode && wrap) {
956 walker.currentNode = root;
957 let lastNode = last(walker);
958 if (!lastNode) {

Callers 7

useToolbarFunction · 0.90
useActionGroupFunction · 0.90
useDateRangePickerFunction · 0.90
useDatePickerGroupFunction · 0.90
useDateFieldFunction · 0.90
useDatePickerFunction · 0.90
focusFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected