Skip to content

Commit

Permalink
fix(menu): prevent duplicate visible menus
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Jun 1, 2023
1 parent f4eb4e5 commit e4b9267
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
6 changes: 4 additions & 2 deletions src/components/Header/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ describe('<Header />', () => {
onCloseSearchButtonClick={vi.fn()}
onLoginButtonClick={vi.fn()}
userMenuOpen={false}
toggleUserMenu={vi.fn()}
openUserMenu={vi.fn()}
closeUserMenu={vi.fn()}
openLanguageMenu={vi.fn()}
closeLanguageMenu={vi.fn()}
isLoggedIn={false}
canLogin={true}
showPaymentsMenuItem={true}
supportedLanguages={[]}
toggleLanguageMenu={vi.fn()}
currentLanguage={undefined}
languageMenuOpen={false}
onLanguageClick={vi.fn()}
Expand Down
38 changes: 14 additions & 24 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ type Props = {
onCloseSearchButtonClick?: () => void;
onLoginButtonClick?: () => void;
onSignUpButtonClick?: () => void;
toggleUserMenu: (value: boolean) => void;
toggleLanguageMenu: (value: boolean) => void;
openUserMenu: () => void;
closeUserMenu: () => void;
openLanguageMenu: () => void;
closeLanguageMenu: () => void;
children?: ReactFragment;
isLoggedIn: boolean;
userMenuOpen: boolean;
Expand All @@ -62,8 +64,10 @@ const Header: React.FC<Props> = ({
isLoggedIn,
userMenuOpen,
languageMenuOpen,
toggleUserMenu,
toggleLanguageMenu,
openUserMenu,
closeUserMenu,
openLanguageMenu,
closeLanguageMenu,
canLogin = false,
showPaymentsMenuItem,
supportedLanguages,
Expand Down Expand Up @@ -117,19 +121,12 @@ const Header: React.FC<Props> = ({

return isLoggedIn ? (
<React.Fragment>
<IconButton
className={classNames(styles.iconButton, styles.actionButton)}
aria-label={t('open_user_menu')}
onClick={() => {
toggleLanguageMenu(false);
toggleUserMenu(!userMenuOpen);
}}
>
<IconButton className={classNames(styles.iconButton, styles.actionButton)} aria-label={t('open_user_menu')} onClick={openUserMenu}>
<AccountCircle />
</IconButton>
<Popover isOpen={userMenuOpen} onClose={() => toggleUserMenu(false)}>
<Popover isOpen={userMenuOpen} onClose={closeUserMenu}>
<Panel>
<UserMenu onClick={() => toggleUserMenu(false)} showPaymentsItem={showPaymentsMenuItem} small />
<UserMenu onClick={closeUserMenu} showPaymentsItem={showPaymentsMenuItem} small />
</Panel>
</Popover>
</React.Fragment>
Expand All @@ -146,22 +143,15 @@ const Header: React.FC<Props> = ({

return (
<React.Fragment>
<IconButton
className={classNames(styles.iconButton, styles.actionButton)}
aria-label={t('select_language')}
onClick={() => {
toggleUserMenu(false);
toggleLanguageMenu(!languageMenuOpen);
}}
>
<IconButton className={classNames(styles.iconButton, styles.actionButton)} aria-label={t('select_language')} onClick={openLanguageMenu}>
<Language />
</IconButton>
<Popover isOpen={languageMenuOpen} onClose={() => toggleLanguageMenu(false)}>
<Popover isOpen={languageMenuOpen} onClose={closeLanguageMenu}>
<Panel>
<LanguageMenu
onClick={(code) => {
onLanguageClick(code);
toggleLanguageMenu(false);
closeLanguageMenu();
}}
languages={supportedLanguages}
currentLanguage={currentLanguage}
Expand Down
22 changes: 10 additions & 12 deletions src/containers/Layout/Layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useRef, useState } from 'react';
import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { Helmet } from 'react-helmet';
import { useTranslation } from 'react-i18next';
import { Outlet, useLocation, useNavigate } from 'react-router';
Expand Down Expand Up @@ -82,15 +82,11 @@ const Layout = () => {
await i18n.changeLanguage(code);
};

const toggleUserMenu = (value: boolean) =>
useUIStore.setState({
userMenuOpen: value,
});

const toggleLanguageMenu = (value: boolean) =>
useUIStore.setState({
languageMenuOpen: value,
});
// useCallbacks are used here to fix a bug in the Popover when using a Reactive onClose callback
const openUserMenu = useCallback(() => useUIStore.setState({ userMenuOpen: true }), []);
const closeUserMenu = useCallback(() => useUIStore.setState({ userMenuOpen: false }), []);
const openLanguageMenu = useCallback(() => useUIStore.setState({ languageMenuOpen: true }), []);
const closeLanguageMenu = useCallback(() => useUIStore.setState({ languageMenuOpen: false }), []);

const renderUserActions = () => {
if (!clientId) return null;
Expand Down Expand Up @@ -137,8 +133,10 @@ const Layout = () => {
isLoggedIn={isLoggedIn}
userMenuOpen={userMenuOpen}
languageMenuOpen={languageMenuOpen}
toggleUserMenu={toggleUserMenu}
toggleLanguageMenu={toggleLanguageMenu}
openUserMenu={openUserMenu}
closeUserMenu={closeUserMenu}
openLanguageMenu={openLanguageMenu}
closeLanguageMenu={closeLanguageMenu}
canLogin={!!clientId}
showPaymentsMenuItem={accessModel !== 'AVOD'}
>
Expand Down

0 comments on commit e4b9267

Please sign in to comment.