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

Function createFocusManager

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

Source from the content-addressed store, hash-verified

953 * Creates a FocusManager object that can be used to move focus within an element.
954 */
955export function createFocusManager(
956 ref: RefObject<Element | null>,
957 defaultOptions: FocusManagerOptions = {}
958): FocusManager {
959 return {
960 focusNext(opts: FocusManagerOptions = {}) {
961 let root = ref.current;
962 if (!root) {
963 return null;
964 }
965 let {
966 from,
967 tabbable = defaultOptions.tabbable,
968 wrap = defaultOptions.wrap,
969 accept = defaultOptions.accept
970 } = opts;
971 let node = from || getActiveElement(getOwnerDocument(root));
972 let walker = getFocusableTreeWalker(root, {tabbable, accept});
973 if (nodeContains(root, node)) {
974 walker.currentNode = node!;
975 }
976 let nextNode = walker.nextNode() as FocusableElement;
977 if (!nextNode && wrap) {
978 walker.currentNode = root;
979 nextNode = walker.nextNode() as FocusableElement;
980 }
981 if (nextNode) {
982 focusElement(nextNode, true);
983 }
984 return nextNode;
985 },
986 focusPrevious(opts: FocusManagerOptions = defaultOptions) {
987 let root = ref.current;
988 if (!root) {
989 return null;
990 }
991 let {
992 from,
993 tabbable = defaultOptions.tabbable,
994 wrap = defaultOptions.wrap,
995 accept = defaultOptions.accept
996 } = opts;
997 let node = from || getActiveElement(getOwnerDocument(root));
998 let walker = getFocusableTreeWalker(root, {tabbable, accept});
999 if (nodeContains(root, node)) {
1000 walker.currentNode = node!;
1001 } else {
1002 let next = last(walker);
1003 if (next) {
1004 focusElement(next, true);
1005 }
1006 return next ?? null;
1007 }
1008 let previousNode = walker.previousNode() as FocusableElement;
1009 if (!previousNode && wrap) {
1010 walker.currentNode = root;
1011 let lastNode = last(walker);
1012 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