Skip to content

Commit

Permalink
Improve performance of the Popover
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer committed Jan 19, 2024
1 parent 32e06d7 commit 5693a9a
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/circuit-ui/components/Popover/Popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,14 @@ export const Popover = ({
window.removeEventListener('scroll', update);
}

// Focus the first or last element after opening
return () => {
window.removeEventListener('resize', update);
window.removeEventListener('scroll', update);
};
}, [isOpen, refs.reference, update]);

useEffect(() => {
// Focus the first or last popover item after opening
if (!prevOpen && isOpen) {
const element = (
triggerKey.current && triggerKey.current === 'ArrowUp'
Expand All @@ -287,21 +294,15 @@ export const Popover = ({
}
}

// Focus the trigger button after closing
// Focus the reference element after closing
if (prevOpen && !isOpen) {
const triggerButton = (refs.reference.current &&
refs.reference.current.firstElementChild) as HTMLElement;
triggerButton.focus();
}

triggerKey.current = null;

// Clean up the event listener when the component is unmounted
return () => {
window.removeEventListener('resize', update);
window.removeEventListener('scroll', update);
};
}, [isOpen, prevOpen, refs.reference, update]);
}, [isOpen, prevOpen, refs.reference]);

return (
<Fragment>
Expand Down

0 comments on commit 5693a9a

Please sign in to comment.