(
ref: RefObject<Element | null>,
defaultOptions: FocusManagerOptions = {}
)
| 953 | * Creates a FocusManager object that can be used to move focus within an element. |
| 954 | */ |
| 955 | export 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) { |
no outgoing calls
no test coverage detected