Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavList: Fix when subNav opens automatically and shows current indicator #3611

Merged
merged 2 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/tiny-numbers-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@primer/react": patch
---

NavList: Fix when subNav opens automatically and shows current indicator

<!-- Changed components: NavList -->
46 changes: 46 additions & 0 deletions src/NavList/NavList.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,50 @@ export const WithNextJSLink = () => (
</PageLayout>
)

export const WithReloads: Story = () => {
// eslint-disable-next-line ssr-friendly/no-dom-globals-in-react-fc
const location = window.location

const storyId = new URLSearchParams(location.search).get('id')
const urlBase = `${location.origin + location.pathname}?id=${storyId}`
const itemId = new URLSearchParams(location.search).get('itemId')

return (
<>
<PageLayout>
<PageLayout.Pane position="start">
<NavList>
<NavList.Item href={`${urlBase}&itemId=1`} aria-current={itemId === '1' ? 'page' : 'false'}>
Item 1
</NavList.Item>
<NavList.Item>
Item 2
<NavList.SubNav>
<NavList.Item href={`${urlBase}&itemId=2.1`} aria-current={itemId === '2.1' ? 'page' : 'false'}>
Sub item 2.1
</NavList.Item>
<NavList.Item href={`${urlBase}&itemId=2.2`} aria-current={itemId === '2.2' ? 'page' : 'false'}>
Sub item 2.2
</NavList.Item>
</NavList.SubNav>
</NavList.Item>
<NavList.Item>
Item 3
<NavList.SubNav>
<NavList.Item href={`${urlBase}&itemId=3.1`} aria-current={itemId === '3.1' ? 'page' : 'false'}>
Sub item 3.1
</NavList.Item>
<NavList.Item href={`${urlBase}&itemId=3.2`} aria-current={itemId === '3.2' ? 'page' : 'false'}>
Sub item 3.2
</NavList.Item>
</NavList.SubNav>
</NavList.Item>
</NavList>
</PageLayout.Pane>
<PageLayout.Content></PageLayout.Content>
</PageLayout>
</>
)
}

export default meta
6 changes: 4 additions & 2 deletions src/NavList/NavList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ function ItemWithSubNav({children, subNav, depth, sx: sxProp = defaultSxProp}: I
useIsomorphicLayoutEffect(() => {
if (subNavRef.current) {
// Check if SubNav contains current item
const currentItem = subNavRef.current.querySelector('[aria-current]')
if (currentItem && currentItem.getAttribute('aria-current') !== 'false') {
// valid values: page, step, location, date, time, true and false
const currentItem = subNavRef.current.querySelector('[aria-current]:not([aria-current=false])')

if (currentItem) {
setContainsCurrentItem(true)
setIsOpen(true)
}
Expand Down
Loading