Skip to content

Commit

Permalink
fix: active les items dans le menu et le megamenu selon la route (#379)
Browse files Browse the repository at this point in the history
* fix: active les items dans le menu et le megamenu selon la route

* fix: checks sonar & tests

* fix: tests

* refactor: suppression de trim() (isStringAreEquals)

---------

Co-authored-by: PassCulture <passculture@macstudelaurent.home>
  • Loading branch information
lheneman-pass and PassCulture committed Jun 20, 2024
1 parent 64b72da commit 84b0d6d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
2 changes: 2 additions & 0 deletions public_website/__tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const headerDataFixtures: HeaderProps = {
URL: '#',
},
},
id: 0,
},
],
aboutItems: [
Expand Down Expand Up @@ -66,6 +67,7 @@ export const headerDataFixtures: HeaderProps = {
URL: '#',
},
},
id: 0,
},
],
login: {
Expand Down
49 changes: 45 additions & 4 deletions public_website/src/ui/components/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MegaMenu } from './MegaMenu'
import { MobileMenu } from './mobile/MobileMenu'
import { CTA } from '@/types/CTA'
import { Link } from '@/ui/components/Link'
import { isStringAreEquals } from '@/utils/stringAreEquals'

export type HeaderProps = {
targetItems: HeaderNavigationItemProps[]
Expand All @@ -27,6 +28,7 @@ export type HeaderProps = {
}

type HeaderNavigationItemProps = {
id: number
label: string
megaMenu: {
title: string
Expand All @@ -43,6 +45,42 @@ type HeaderNavigationItemProps = {
cardSecondEmoji: string
}
}
/**
*
*
* @param {CTA[]} items
* @param {string} path
* @return {*} {boolean}
*/
const findInMenu = (items: CTA[], str: string): boolean => {
for (const item of items) {
if (isStringAreEquals(item.URL, str)) return true
}
return false
}
/**
*
*
* @param {string} path
* @param {HeaderNavigationItemProps[]} collections
* @return {*} {(number | null)}
*/
const findCollectionIdByPath = (
path: string,
collections: HeaderNavigationItemProps[]
): number | null => {
for (const collection of collections) {
const { megaMenu } = collection
if (megaMenu.primaryListItems) {
if (findInMenu(megaMenu.primaryListItems, path)) return collection.id
}
if (megaMenu.secondaryListItems) {
if (findInMenu(megaMenu.secondaryListItems, path)) return collection.id
}
}

return null
}

export function Header({
targetItems,
Expand All @@ -51,10 +89,15 @@ export function Header({
signup,
}: HeaderProps) {
const [activeMegaMenuId, setActiveMegaMenuId] = useState<number | null>(null)

const router = usePathname()
const megaMenuButtonRefs = useRef<(HTMLButtonElement | null)[]>([])

const navItems = [...targetItems, ...aboutItems]
const activeId = findCollectionIdByPath(router, navItems)

// Set active menu on hover and if asPath is including in navItems
const isActive = (i: number): string =>
i === activeMegaMenuId || activeId === i + 1 ? 'mega-menu-active' : ''

// Toggle mega menu panel
function toggleMegaMenu(id: number) {
Expand Down Expand Up @@ -193,9 +236,7 @@ export function Header({
id={`mega-menu-button-${i}`}
aria-controls={`mega-menu-${i}`}
aria-expanded={i === activeMegaMenuId}
className={
i === activeMegaMenuId ? 'mega-menu-active' : ''
}
className={isActive(i)}
onClick={() => toggleMegaMenu(i)}
onKeyDown={(e) => onMegaMenuKeyDown(e, i)}
onMouseEnter={() => toggleMegaMenu(i)}>
Expand Down
13 changes: 12 additions & 1 deletion public_website/src/ui/components/header/MegaMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect, useRef } from 'react'
import { usePathname } from 'next/navigation'
import styled, { css } from 'styled-components'

import { AppBanner } from '../app-banner/AppBanner'
Expand All @@ -9,6 +10,7 @@ import { onClickAnalytics } from '@/lib/analytics/helpers'
import { CTA } from '@/types/CTA'
import { Link } from '@/ui/components/Link'
import { parseText } from '@/utils/parseText'
import { isStringAreEquals } from '@/utils/stringAreEquals'

type MegaMenuProps = {
onBlur: () => void
Expand Down Expand Up @@ -43,6 +45,7 @@ export function MegaMenu({
data,
}: MegaMenuProps) {
const megaMenuRef = useRef<HTMLDivElement>(null)
const path = usePathname()

function onClickOutside(e: MouseEvent) {
if (!megaMenuRef.current?.contains(e.target as HTMLElement)) {
Expand Down Expand Up @@ -104,6 +107,9 @@ export function MegaMenu({
return (
<li key={item.Label}>
<Link
className={
isStringAreEquals(path, item.URL) ? 'active' : ''
}
href={item.URL}
aria-label={parseText(item.Label).accessibilityLabel}>
{parseText(item.Label).processedText}
Expand All @@ -117,6 +123,9 @@ export function MegaMenu({
return (
<li key={item.Label}>
<Link
className={
isStringAreEquals(path, item.URL) ? 'active' : ''
}
href={item.URL}
aria-label={parseText(item.Label).accessibilityLabel}>
{parseText(item.Label).processedText}
Expand Down Expand Up @@ -207,7 +216,9 @@ const StyledMegaMenuLists = styled.div`
font-weight: ${theme.fonts.weights.semiBold};
color: ${theme.colors.black};
opacity: 0.9;
&.active {
text-decoration: underline;
}
&:hover {
text-decoration: underline;
}
Expand Down
6 changes: 6 additions & 0 deletions public_website/src/utils/stringAreEquals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const isStringAreEquals = (str1: string, str2: string): boolean => {
if (str1 === undefined || str2 === undefined) return false
if (str1.trim().toLocaleLowerCase() === str2.trim().toLocaleLowerCase())
return true
return false
}

0 comments on commit 84b0d6d

Please sign in to comment.