Skip to content

Commit

Permalink
Merge pull request #886 from criblio/criblio/localStorage-fix
Browse files Browse the repository at this point in the history
localStorage fix for gatsby build
  • Loading branch information
seanvaleo authored Apr 12, 2022
2 parents b026b24 + 915802c commit 5f63d31
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions website/src/components/DocsNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import "../utils/font-awesome";

const searchIndices = [{ name: `Pages`, title: `Pages` }];
const DARK_MODE_KEY = 'DARK_MODE';
// Check if window is defined (so if in the browser or in node.js).
const isBrowser = typeof window !== 'undefined';

export default function DocsNav() {
const [darkMode, toggleDarkMode] = useState(localStorage.getItem(DARK_MODE_KEY) === 'true');
const isDarkMode = isBrowser ? localStorage.getItem(DARK_MODE_KEY) === 'true' : false;
const [darkMode, toggleDarkMode] = useState(isDarkMode);
const [mobileNav, openMobileNav] = useState(false);
const data = useStaticQuery(graphql`
query DocumentationNav {
Expand All @@ -27,7 +30,9 @@ export default function DocsNav() {
`);
const navItems = data.allDocumentationNavYaml.nodes;
const darkModeClick = () => {
localStorage.setItem(DARK_MODE_KEY, !darkMode);
if (isBrowser) {
localStorage.setItem(DARK_MODE_KEY, !darkMode);
}
toggleDarkMode(!darkMode);
}

Expand Down

0 comments on commit 5f63d31

Please sign in to comment.