Skip to content

Commit c58203f

Browse files
authoredJul 3, 2020
fix(www): Fix getting active item when pathname does not end with / (#24965)
1 parent 1a1177e commit c58203f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed
 

‎www/src/components/sidebar/__tests__/sidebar.js

+10
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ describe(`sidebar`, () => {
8383
const { queryByText } = renderSidebar(`/characters/jay-gatsby/`)
8484
expect(queryByText(`Jay Gatsby`)).toBeInTheDocument()
8585
})
86+
87+
it(`opens sections with active items when the pathname is missing a trailing slash`, () => {
88+
const { queryByText } = renderSidebar(`/characters/jay-gatsby`)
89+
expect(queryByText(`Jay Gatsby`)).toBeInTheDocument()
90+
})
8691
})
8792

8893
describe(`toggle section`, () => {
@@ -127,6 +132,11 @@ describe(`sidebar`, () => {
127132
expect(scrollIntoViewMock).toHaveBeenCalled()
128133
})
129134

135+
it(`scrolls the sidebar into view on load when the pathname is missing a trailing slash`, () => {
136+
renderSidebar(`/characters/jay-gatsby`)
137+
expect(scrollIntoViewMock).toHaveBeenCalled()
138+
})
139+
130140
it(`does not scroll into view when loaded with a hash`, () => {
131141
renderSidebar(`/themes/`, {
132142
activeItemHash: `the-american-dream`,

‎www/src/utils/sidebar/get-active-item.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { getLocaleAndBasePath } from "../i18n"
22

3+
function addTrailingSlashIfMissing(pathname) {
4+
return pathname.endsWith(`/`) ? pathname : `${pathname}/`
5+
}
6+
37
const isItemActive = (location, item, activeItemHash) => {
4-
const { basePath } = getLocaleAndBasePath(location.pathname)
8+
const pathnameWithTrailingSlash = addTrailingSlashIfMissing(location.pathname)
9+
const { basePath } = getLocaleAndBasePath(pathnameWithTrailingSlash)
510
const linkMatchesPathname = item.link === basePath
611
const linkWithoutHashMatchesPathname =
712
item.link.replace(/#.*/, ``) === basePath

0 commit comments

Comments
 (0)
Please sign in to comment.