Skip to content

Commit

Permalink
fix: prevent nav from collapsing during local development (#1510)
Browse files Browse the repository at this point in the history
* fix: use env var, rerendering of value returned from usePathprefix causes nav to collapse

* chore: cleanup

* docs: add note about PATH_PREFIX

* chore: format

* refactor: set prefix in context and pass as prop

* chore: remove doc updates given new approach
  • Loading branch information
matthewgallo authored Sep 11, 2024
1 parent ddf2806 commit b907d88
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const LeftNav = (props) => {
leftNavScrollTop,
setLeftNavScrollTop,
toggleNavState,
pathPrefix,
} = useContext(NavContext);

const [isTreeView, setIsTreeView] = useState();
Expand Down Expand Up @@ -97,7 +98,11 @@ const LeftNav = (props) => {
onClick={closeSwitcher}
onKeyPress={closeSwitcher}>
{isTreeView ? (
<LeftNavTree items={navItems} theme={props.theme} />
<LeftNavTree
items={navItems}
theme={props.theme}
pathPrefix={pathPrefix}
/>
) : (
<SideNav
ref={sideNavRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import * as styles from './LeftNavTree.module.scss';
import { dfs } from '../../util/NavTree';

import LeftNavResourceLinks from './ResourceLinks';
import usePathprefix from '../../util/hooks/usePathprefix';

const LeftNavTree = ({ items, theme }) => {
const LeftNavTree = ({ items, theme, pathPrefix }) => {
const [itemNodes, setItemNodes] = useState([]);
const [treeActiveItem, setTreeActiveItem] = useState({});
const [activePath, setActivePath] = useState('');
const location = useLocation();
const pathPrefix = usePathprefix();

const themeValue = theme === 'dark' ? 'g100' : theme;

Expand Down Expand Up @@ -68,12 +66,11 @@ const LeftNavTree = ({ items, theme }) => {
useEffect(() => {
const stripTrailingSlash = (str) =>
str.endsWith('/') ? str.slice(0, -1) : str;

const base = pathPrefix
? location.pathname.replace(pathPrefix, '')
: location.pathname;
setActivePath(stripTrailingSlash(base));
}, [location.pathname]);
}, [location.pathname, pathPrefix]);

const getItemPath = (item) =>
item.path || slugify(item.title, { lower: true, strict: true });
Expand Down Expand Up @@ -103,7 +100,7 @@ const LeftNavTree = ({ items, theme }) => {
activeNode = dfs(itemNodes, isTabActive);
}
setTreeActiveItem(activeNode);
}, [isTreeNodeActive, itemNodes]);
}, [isTreeNodeActive, itemNodes, isTabActive]);

const isTreeNodeExpanded = (node) =>
!!dfs([node], (evalNode) =>
Expand Down
4 changes: 4 additions & 0 deletions packages/gatsby-theme-carbon/src/util/context/NavContext.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useMemo, useReducer, useState } from 'react';
import usePathprefix from '../hooks/usePathprefix';

const NavContext = React.createContext({
leftNavIsOpen: false,
Expand All @@ -19,6 +20,7 @@ const reducer = (state, action) => {
}
};
export const NavContextProvider = ({ children }) => {
const pathPrefix = usePathprefix();
const [
{ leftNavIsOpen, searchIsOpen, switcherIsOpen, leftNavScrollOffset },
dispatch,
Expand Down Expand Up @@ -47,6 +49,7 @@ export const NavContextProvider = ({ children }) => {
leftNavScrollOffset,
leftNavScrollTop,
setLeftNavScrollTop,
pathPrefix,
}),
[
leftNavIsOpen,
Expand All @@ -58,6 +61,7 @@ export const NavContextProvider = ({ children }) => {
leftNavScrollOffset,
leftNavScrollTop,
setLeftNavScrollTop,
pathPrefix,
]
);

Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10595,7 +10595,7 @@ __metadata:
dependencies:
"@carbon/icons-react": "npm:^11.43.0"
gatsby: "npm:^5.13.6"
gatsby-theme-carbon: "npm:^4.0.8"
gatsby-theme-carbon: "npm:^4.0.9"
react: "npm:^18.3.1"
react-dom: "npm:^18.3.1"
languageName: unknown
Expand Down Expand Up @@ -11905,7 +11905,7 @@ __metadata:
languageName: unknown
linkType: soft

"gatsby-theme-carbon@npm:^4.0.8, gatsby-theme-carbon@workspace:packages/gatsby-theme-carbon":
"gatsby-theme-carbon@npm:^4.0.9, gatsby-theme-carbon@workspace:packages/gatsby-theme-carbon":
version: 0.0.0-use.local
resolution: "gatsby-theme-carbon@workspace:packages/gatsby-theme-carbon"
dependencies:
Expand Down

0 comments on commit b907d88

Please sign in to comment.