How to use the @chakra-ui/hooks.usePrevious function in @chakra-ui/hooks

To help you get started, we’ve selected a few @chakra-ui/hooks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github chakra-ui / chakra-ui / packages / chakra-ui / src / Select / Selectv2.tsx View on Github external
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]);
}