💄(app-desk) keep highlighting menu when sub menu

When sub menu was open, the parent menu was not
highlighted.
This commit fixes this issue.
This commit is contained in:
Anthony LC
2024-05-03 11:42:32 +02:00
committed by Anthony LC
parent c599757d7a
commit b7b90d1bf3
3 changed files with 30 additions and 5 deletions

View File

@@ -22,7 +22,12 @@ export const Menu = () => {
$margin="none" $margin="none"
> >
<Box $padding={{ top: 'large' }} $direction="column" $gap="0.8rem"> <Box $padding={{ top: 'large' }} $direction="column" $gap="0.8rem">
<MenuItem Icon={IconGroup} label={t('Teams')} href="/" /> <MenuItem
Icon={IconGroup}
label={t('Teams')}
href="/"
alias={['/teams']}
/>
<MenuItem Icon={IconMail} label={t('Mails')} href="/mails" /> <MenuItem Icon={IconMail} label={t('Mails')} href="/mails" />
</Box> </Box>
</Box> </Box>

View File

@@ -12,16 +12,22 @@ interface MenuItemProps {
Icon: SVGComponent; Icon: SVGComponent;
label: string; label: string;
href: string; href: string;
alias?: string[];
} }
const MenuItem = ({ Icon, label, href }: MenuItemProps) => { const MenuItem = ({ Icon, label, href, alias }: MenuItemProps) => {
const { t } = useTranslation(); const { t } = useTranslation();
const pathname = usePathname(); const pathname = usePathname();
const { colorsTokens } = useCunninghamTheme(); const { colorsTokens } = useCunninghamTheme();
const parentRef = useRef(null); const parentRef = useRef(null);
const [isTooltipOpen, setIsTooltipOpen] = useState(false); const [isTooltipOpen, setIsTooltipOpen] = useState(false);
const isActive = pathname === href; const isActive =
pathname === href ||
alias?.includes(pathname) ||
pathname.startsWith(`${href}/`) ||
alias?.some((a) => pathname.startsWith(`${a}/`));
const { color, background, colorTooltip, backgroundTooltip } = isActive const { color, background, colorTooltip, backgroundTooltip } = isActive
? { ? {
color: colorsTokens()['primary-600'], color: colorsTokens()['primary-600'],
@@ -42,7 +48,7 @@ const MenuItem = ({ Icon, label, href }: MenuItemProps) => {
aria-current={isActive && 'page'} aria-current={isActive && 'page'}
ref={parentRef} ref={parentRef}
onMouseOver={() => setIsTooltipOpen(true)} onMouseOver={() => setIsTooltipOpen(true)}
onMouseOut={() => setIsTooltipOpen(false)} onMouseLeave={() => setIsTooltipOpen(false)}
style={{ display: 'block' }} style={{ display: 'block' }}
> >
<Box <Box

View File

@@ -1,6 +1,6 @@
import { expect, test } from '@playwright/test'; import { expect, test } from '@playwright/test';
import { keyCloakSignIn } from './common'; import { createTeam, keyCloakSignIn } from './common';
test.beforeEach(async ({ page, browserName }) => { test.beforeEach(async ({ page, browserName }) => {
await page.goto('/'); await page.goto('/');
@@ -61,4 +61,18 @@ test.describe('Menu', () => {
await expect(page).toHaveURL(expectedUrl); await expect(page).toHaveURL(expectedUrl);
}); });
} }
test(`it checks that the sub menu is still highlighted`, async ({
page,
browserName,
}) => {
await createTeam(page, 'team-sub-menu', browserName, 1);
const menu = page.locator('menu').first();
const buttonMenu = menu.locator('li').first();
await expect(buttonMenu).toHaveCSS(
'background-color',
'rgb(227, 227, 253)',
);
});
}); });