Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function useFocusManagement(
state: State & { isOpen: boolean },
refs: { listBoxRef: React.RefObject; controlRef: React.RefObject },
) {
const { isOpen } = state;
const { listBoxRef, controlRef } = refs;
const prevIsOpen = usePrevious(isOpen);
useEffect(() => {
if (isOpen && listBoxRef && listBoxRef.current) {
listBoxRef.current.focus();
return;
}
if (prevIsOpen && !isOpen && controlRef && controlRef.current) {
controlRef.current.focus();
}
}, [isOpen, prevIsOpen, listBoxRef, controlRef]);
}