Skip to content

Commit 545c1ef

Browse files
author
Josep Martins
authored
Fixes selected state in top navigation (#618)
1 parent 44b1d2c commit 545c1ef

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

.changeset/friendly-dodos-shave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/gatsby-theme-doctocat': patch
3+
---
4+
5+
Add missing `pathPrefix`` to the selected state in Underlinenav items

theme/src/components/header.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Link as GatsbyLink} from 'gatsby'
55
import React from 'react'
66
import primerNavItems from '../primer-nav.yml'
77
import useSiteMetadata from '../use-site-metadata'
8+
import usePathPrefix from '../use-path-prefix'
89
import MobileSearch from './mobile-search'
910
import NavDrawer, {useNavDrawerState} from './nav-drawer'
1011
import Search from './search'
@@ -17,6 +18,7 @@ function Header({isSearchEnabled, path}) {
1718
const [isNavDrawerOpen, setIsNavDrawerOpen] = useNavDrawerState(theme.breakpoints[2])
1819
const [isMobileSearchOpen, setIsMobileSearchOpen] = React.useState(false)
1920
const siteMetadata = useSiteMetadata()
21+
const pathPrefix = usePathPrefix()
2022

2123
return (
2224
<ThemeProvider>
@@ -92,7 +94,7 @@ function Header({isSearchEnabled, path}) {
9294
</Box>
9395
<Box>
9496
<Box sx={{display: ['none', null, null, 'flex'], alignItems: 'center'}}>
95-
<PrimerNavItems path={path} siteMetadata={siteMetadata} items={primerNavItems} />
97+
<PrimerNavItems path={path} siteMetadata={siteMetadata} pathPrefix={pathPrefix} items={primerNavItems} />
9698
{isSearchEnabled ? (
9799
<Box sx={{display: ['none', null, null, 'block'], ml: 3}}>
98100
<Search />
@@ -138,7 +140,11 @@ Header.defaultProps = {
138140
isSearchEnabled: true
139141
}
140142

141-
function PrimerNavItems({siteMetadata, items, path}) {
143+
function PrimerNavItems({siteMetadata, items, path, pathPrefix}) {
144+
console.log('header url', siteMetadata.header.url)
145+
console.log('pathPrefix', pathPrefix)
146+
console.log('path', path)
147+
142148
return (
143149
<>
144150
<VisuallyHidden>
@@ -150,7 +156,7 @@ function PrimerNavItems({siteMetadata, items, path}) {
150156
<UnderlineNav.Link
151157
key={index}
152158
href={item.url}
153-
selected={item.url === siteMetadata.header.url + path}
159+
selected={item.url === siteMetadata.header.url + pathPrefix + path}
154160
sx={{fontSize: 2, lineHeight: 'condensed'}}
155161
>
156162
{item.title}

theme/src/use-path-prefix.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {useStaticQuery, graphql} from 'gatsby'
2+
3+
function usePathPrefix() {
4+
const data = useStaticQuery(graphql`
5+
{
6+
site {
7+
pathPrefix
8+
}
9+
}
10+
`)
11+
return data.site.pathPrefix
12+
}
13+
14+
export default usePathPrefix

0 commit comments

Comments
 (0)