Skip to content

Commit

Permalink
Fixes always active menu route (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smilinko authored Dec 10, 2023
1 parent 3b407a6 commit 6bf6805
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/components/PageLayout/MenuMain/MenuMain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,16 @@ const MenuMainItem: FC<{caption: string; url: string}> = ({caption, url}) => {
// potrebne koncove lomitko pre porovnanie s URLkami z BE
const pathWithSlash = `${router.asPath}/`

// ak sme na `/matik/vysledky/44/leto/2`, orezme to na dlzku `url`, v zavere porovnajme
// (teda v podstate zistime, ci `pathWithSlash` zacina znakmi `url`)
const active = pathWithSlash.slice(0, url.length) === url
let active

const urlArray = url.split('/')
if (urlArray.length === 3 && urlArray[0] === '' && urlArray[2] === '') {
// riesi case ked url je napr. /strom/ a teda nestaci porovnanie so .startsWith
// urlArray je tak v tvare ['', 'strom', ''] co overuje dlzka 3 a zaciatok/koniec ako ''
active = pathWithSlash === url
} else {
active = pathWithSlash.startsWith(url)
}

return (
<div className={clsx(styles.menuItem, active && styles.active)}>
Expand Down

0 comments on commit 6bf6805

Please sign in to comment.