Skip to content
This repository has been archived by the owner on Apr 25, 2024. It is now read-only.

Commit

Permalink
window Dimension hook & mobile improvements to HeaderNavigation
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiggr committed Nov 5, 2021
1 parent 8fb5dc4 commit 7b2f299
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/components/theme/Header/HeaderNavigation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
import React from 'react';
import { useHistory } from 'react-router-dom';
import { Link } from 'react-router-dom';
import useWindowSize from '../../../helpers/useWindowSize';
import downIcon from '@plone/volto/icons/down-key.svg';
import closeIcon from '@plone/volto/icons/clear.svg';
import { Icon } from '@plone/volto/components';

const MobileNav = ({ items, activeItem }) => {
const [expanded, setExpanded] = React.useState(false);

React.useEffect(() => {
setExpanded(false);
}, [activeItem]);

return (
<div className="lead-mobile-nav">
{expanded ? (
Expand All @@ -21,6 +29,14 @@ const MobileNav = ({ items, activeItem }) => {
}`}
>
{item.title}
{item.url === activeItem.url && (
<Icon
className="lead-nav-icon"
name={closeIcon}
size="35px"
onClick={() => setExpanded(false)}
/>
)}
</p>
</Link>
))}
Expand All @@ -32,6 +48,12 @@ const MobileNav = ({ items, activeItem }) => {
className="lead-nav-item active-mobile-nav"
>
{activeItem.title}
<Icon
className="lead-nav-icon"
name={downIcon}
size="35px"
onClick={() => setExpanded(true)}
/>
</p>
</div>
)}
Expand All @@ -43,23 +65,24 @@ const HeaderNavigation = ({ items }) => {
const [activeItem, setActiveItem] = React.useState('');
const [isMobile, setIsMobile] = React.useState(false);
const history = useHistory();
const size = useWindowSize();

React.useEffect(() => {
const { width } = size;
const activeRouteDetected = items.filter(
(item) => item.url === history.location.pathname,
);
if (activeRouteDetected && activeRouteDetected.length > 0) {
setActiveItem(activeRouteDetected[0]);
}
const width = window && window.innerWidth ? window.innerWidth : '';
if (width && width <= 600) {
if (width && width <= 768) {
setIsMobile(true);
}
if (width && width > 600) {
if (width && width > 768) {
setIsMobile(false);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [items]);
}, [items, size]);

return (
<React.Fragment>
Expand Down
35 changes: 35 additions & 0 deletions src/helpers/useWindowSize.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { useState, useEffect } from 'react';

export default function useWindowSize() {
// Initialize state with undefined width/height so server and client renders match
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
const [windowSize, setWindowSize] = useState({
width: undefined,
height: undefined,
});
useEffect(() => {
// Handler to call on window resize
function handleResize() {
// Set window width/height to state
if (typeof window !== 'undefined') {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
}
// Add event listener
if (typeof window !== 'undefined') {
window.addEventListener('resize', handleResize);
handleResize();
}
// Call handler right away so state gets updated with initial window size
// Remove event listener on cleanup
return () => {
if (typeof window !== 'undefined') {
window.removeEventListener('resize', handleResize);
}
};
}, []); // Empty array ensures that effect is only run on mount
return windowSize;
}
14 changes: 14 additions & 0 deletions theme/site/globals/site.overrides
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ body.has-toolbar {
height: 600px !important;
}



.lead-mobile-nav {
position: absolute;
z-index: 1;
Expand All @@ -142,6 +144,7 @@ body.has-toolbar {
}

.lead-nav-item {
position: relative;
padding: 12px 20px;
margin: 2px 0;
background-color: #f7f7f5;
Expand All @@ -153,6 +156,17 @@ body.has-toolbar {
font-weight: bold;
text-align: center;
text-decoration: none;

.lead-nav-icon {
fill: #554535 !important;
position: absolute;
right: 10px;
top: 6px;
}

.lead-nav-icon::hover{
fill: #cd4200 !important;
}
}

.lead-nav-item:hover {
Expand Down

0 comments on commit 7b2f299

Please sign in to comment.