-
Notifications
You must be signed in to change notification settings - Fork 5.3k
fix: Popover toggle #35746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Popover toggle #35746
Changes from all commits
0a498bd
b9b1329
61f81d5
b5bdf6e
0dc8661
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,29 +86,30 @@ export const Popover: PopoverComponent = React.forwardRef( | |
width: matchWidth ? referenceElement?.clientWidth : 'auto', | ||
}; | ||
|
||
// Esc key press | ||
const handleEscKey = (event: KeyboardEvent) => { | ||
if (event.key === 'Escape') { | ||
// Close the popover when the "Esc" key is pressed | ||
if (onPressEscKey) { | ||
onPressEscKey(); | ||
useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// Esc key press | ||
const handleEscKey = (event: KeyboardEvent) => { | ||
if (event.key === 'Escape') { | ||
// Close the popover when the "Esc" key is pressed | ||
if (onPressEscKey) { | ||
onPressEscKey(); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
const handleClickOutside = (event: MouseEvent) => { | ||
if ( | ||
isOpen && | ||
popoverRef.current && | ||
!popoverRef.current.contains(event.target as Node) | ||
) { | ||
if (onClickOutside) { | ||
onClickOutside(); | ||
const handleClickOutside = (event: MouseEvent) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if ( | ||
isOpen && | ||
popoverRef.current && | ||
!popoverRef.current.contains(event.target as Node) && | ||
!referenceElement?.contains(event.target as Node) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
) { | ||
if (onClickOutside) { | ||
onClickOutside(); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
useEffect(() => { | ||
document.addEventListener('keydown', handleEscKey, { capture: true }); | ||
if (isOpen) { | ||
document.addEventListener('click', handleClickOutside, { | ||
|
@@ -122,7 +123,7 @@ export const Popover: PopoverComponent = React.forwardRef( | |
document.removeEventListener('keydown', handleEscKey); | ||
document.removeEventListener('click', handleClickOutside); | ||
}; | ||
}, [onPressEscKey, isOpen, onClickOutside]); | ||
}, [onPressEscKey, isOpen, onClickOutside, referenceElement]); | ||
|
||
const PopoverContent = ( | ||
<Box | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,15 +139,21 @@ export const AppHeaderUnlockedContent = ({ | |
origin && | ||
origin !== browser.runtime.id; | ||
|
||
const handleMainMenuOpened = () => { | ||
trackEvent({ | ||
event: MetaMetricsEventName.NavMainMenuOpened, | ||
category: MetaMetricsEventCategory.Navigation, | ||
properties: { | ||
location: 'Home', | ||
}, | ||
const handleMainMenuToggle = () => { | ||
setAccountOptionsMenuOpen((previous) => { | ||
const isMenuOpen = !previous; | ||
if (isMenuOpen) { | ||
trackEvent({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we creating a MetaMetrics event in a React state change handler? Shouldn't we do that afterward?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mcmire That is existing prod code - see line 143, tracking whenever the menu opens Was trying to keep the focus of this PR about fixing the toggle There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. Does this not work? setAccountOptionsMenuOpen((previous) => !previous);
if (accountOptionsMenuOpen) {
trackEvent({
event: MetaMetricsEventName.NavMainMenuOpened,
category: MetaMetricsEventCategory.Navigation,
properties: {
location: 'Home',
},
});
} Or does it make sense to move the tracking into a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (This is non-blocking, I will let you decide, but just wanted to point it out in case there is a better way to do this)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Calling
directly like that will skew the tracking especially knowing the extension has existing excessive re-renders useEffect might work but again susceptible to re-renders IMO safest option without a full rewrite of this component and popover is the setState and logically close to the button click event I mean we could also put events on the Menu component itself when it renders but I think these are for another PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay no problem! |
||
event: MetaMetricsEventName.NavMainMenuOpened, | ||
category: MetaMetricsEventCategory.Navigation, | ||
properties: { | ||
location: 'Home', | ||
}, | ||
}); | ||
} | ||
|
||
return isMenuOpen; | ||
}); | ||
setAccountOptionsMenuOpen(true); | ||
}; | ||
|
||
const handleConnectionsRoute = () => { | ||
|
@@ -287,25 +293,25 @@ export const AppHeaderUnlockedContent = ({ | |
style={{ position: 'relative' }} | ||
> | ||
{!accountOptionsMenuOpen && ( | ||
<Box onClick={() => handleMainMenuOpened()}> | ||
<Box onClick={handleMainMenuToggle}> | ||
<NotificationsTagCounter noLabel /> | ||
</Box> | ||
)} | ||
<ButtonIcon | ||
iconName={IconName.Menu} | ||
data-testid="account-options-menu-button" | ||
ariaLabel={t('accountOptions')} | ||
onClick={() => { | ||
handleMainMenuOpened(); | ||
}} | ||
onClick={handleMainMenuToggle} | ||
size={ButtonIconSize.Lg} | ||
/> | ||
</Box> | ||
</Box> | ||
<GlobalMenu | ||
anchorElement={menuRef.current} | ||
isOpen={accountOptionsMenuOpen} | ||
closeMenu={() => setAccountOptionsMenuOpen(false)} | ||
closeMenu={() => { | ||
setAccountOptionsMenuOpen(false); | ||
}} | ||
/> | ||
<VisitSupportDataConsentModal | ||
isOpen={showSupportDataConsentModal} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like
openThreeDotMenu
accounts for differences on Firefox vs. Chrome. Do we not need to do that anymore?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original test had a flaw itself, it called both methods when only 1 click really mattered because of the popover bug