Skip to content

Commit

Permalink
NavList: Fix when subNav opens automatically and shows current indica…
Browse files Browse the repository at this point in the history
…tor (#3611)

* check current=false

* Create tiny-numbers-sip.md
  • Loading branch information
siddharthkp authored Aug 14, 2023
1 parent e6a0855 commit ace603d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
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

0 comments on commit ace603d

Please sign in to comment.