diff --git a/.changeset/odd-beds-peel.md b/.changeset/odd-beds-peel.md new file mode 100644 index 00000000..f6bed371 --- /dev/null +++ b/.changeset/odd-beds-peel.md @@ -0,0 +1,5 @@ +--- +'@ithaka/pharos-site': patch +--- + +Fix mobile sidenav diff --git a/packages/pharos-site/src/components/Sidenav.tsx b/packages/pharos-site/src/components/Sidenav.tsx index cb823a70..0746d410 100644 --- a/packages/pharos-site/src/components/Sidenav.tsx +++ b/packages/pharos-site/src/components/Sidenav.tsx @@ -1,4 +1,4 @@ -import { useLayoutEffect, useState } from 'react'; +import { useEffect, useLayoutEffect, useState } from 'react'; import type { FC, ReactElement } from 'react'; import { withPrefix, useStaticQuery, graphql } from 'gatsby'; import { useLocation } from '@reach/router'; @@ -6,7 +6,12 @@ import { useLocation } from '@reach/router'; import handleLinkClick from '../utils/handleLinkClick'; import { siteBrand__title, siteBrand__subTitle, sidenav } from './Sidenav.module.css'; -const Sidenav: FC = () => { +interface SidenavProps { + isOpen: boolean; + showCloseButton?: boolean; +} + +const Sidenav: FC = ({ isOpen, showCloseButton }) => { const [Display, setDisplay] = useState(null); const Pharos = typeof window !== `undefined` ? require('@ithaka/pharos/lib/react-components') : null; @@ -23,7 +28,7 @@ const Sidenav: FC = () => { } `); - useLayoutEffect(() => { + useEffect(() => { const { PharosSidenav, PharosSidenavSection, @@ -55,7 +60,13 @@ const Sidenav: FC = () => { }; const content = ( - +
{data.site.siteMetadata.title}
@@ -167,7 +178,7 @@ const Sidenav: FC = () => { ); setDisplay(content); - }, [Pharos, data, location]); + }, [Pharos, data, location, isOpen, showCloseButton]); return Display; }; diff --git a/packages/pharos-site/src/components/layout.module.css b/packages/pharos-site/src/components/layout.module.css index 9924a88f..7e83794f 100644 --- a/packages/pharos-site/src/components/layout.module.css +++ b/packages/pharos-site/src/components/layout.module.css @@ -27,6 +27,7 @@ a { @media screen and (width <= 1055px) { .container { grid-template-areas: + 'sidenav' 'main' 'footer'; grid-template-columns: auto; diff --git a/packages/pharos-site/src/components/layout.tsx b/packages/pharos-site/src/components/layout.tsx index 7fbe612c..70403021 100644 --- a/packages/pharos-site/src/components/layout.tsx +++ b/packages/pharos-site/src/components/layout.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import type { FC, ReactElement } from 'react'; import type { WindowLocation } from '@reach/router'; import Sidenav from './Sidenav'; @@ -36,35 +36,70 @@ const Layout: FC = ({ children, location, fill }) => { typeof window !== `undefined` ? require('@ithaka/pharos/lib/react-components') : null; const [MainContent, setMainContent] = useState(null); + const mobileBreakpoint = 1055; + const [isMobile, setIsMobile] = useState( + typeof window !== `undefined` ? window.innerWidth < mobileBreakpoint : false + ); + const [isSidenavDisplayed, setIsSidenavDisplayed] = useState(!isMobile); + const containerRef = useRef(null); useEffect(() => { - const { PharosLink, PharosLayout } = Pharos; + const { PharosLink, PharosLayout, PharosButton } = Pharos; + const currentRef = containerRef.current; + const resizeObserver = new ResizeObserver(() => { + const windowWidth = window.innerWidth; + setIsSidenavDisplayed(windowWidth >= mobileBreakpoint); + setIsMobile(windowWidth < mobileBreakpoint); + }); const body = ( -
-
- - Skip to main navigation - -
- - {fill ? children :
{children}
} -
-
+ <> + +
+
+ + Skip to main navigation + +
+ + + + + {fill ? children :
{children}
} +
+
+ ); setMainContent(body); - }, [Pharos, children, fill]); + + if (currentRef) { + resizeObserver.observe(currentRef); + } + return () => { + if (currentRef) { + resizeObserver.unobserve(currentRef); + } + }; + }, [Pharos, children, fill, isMobile, isSidenavDisplayed]); return ( -
+
- {MainContent}
diff --git a/packages/pharos-site/src/components/variables.module.css b/packages/pharos-site/src/components/variables.module.css index 8a400fc1..e3987326 100644 --- a/packages/pharos-site/src/components/variables.module.css +++ b/packages/pharos-site/src/components/variables.module.css @@ -16,3 +16,9 @@ --sidebar-spacing-padding: var(--pharos-spacing-one-and-a-half-x); --main-spacing-padding: var(--pharos-spacing-7-x); } + +@media screen and (width <= 1055px) { + :root { + --main-spacing-padding: var(--pharos-spacing-2-x); + } +}