Skip to content

Commit f752804

Browse files
authored
fix: set active nav item (#7467)
## Description Nav items not displaying different style when active. We were previously using `NavLink` which determines if the item is active and applies the classname. Now we are using the standard `Link` and need to add the `active` classname manually. - [X] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [ ] Chore (non-breaking change which does not add functionality) - [X] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] Change to the [templates](https://github.com/payloadcms/payload/tree/main/templates) directory (does not affect core functionality) - [ ] Change to the [examples](https://github.com/payloadcms/payload/tree/main/examples) directory (does not affect core functionality) - [ ] This change requires a documentation update ## Checklist: - [ ] I have added tests that prove my fix is effective or that my feature works - [X] Existing test suite passes locally with my changes - [ ] I have made corresponding changes to the documentation
1 parent a18d406 commit f752804

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

packages/next/src/elements/Nav/index.client.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,18 @@ export const DefaultNavClient: React.FC = () => {
8585

8686
const LinkElement = Link || 'a'
8787

88+
const activeCollection = window?.location?.pathname
89+
?.split('/')
90+
.find(
91+
(_, index, arr) =>
92+
arr[index - 1] === 'collections' || arr[index - 1] === 'globals',
93+
)
94+
8895
return (
8996
<LinkElement
90-
className={`${baseClass}__link`}
97+
className={[`${baseClass}__link`, activeCollection === entity?.slug && `active`]
98+
.filter(Boolean)
99+
.join(' ')}
91100
href={href}
92101
id={id}
93102
key={i}

0 commit comments

Comments
 (0)