| 1 |
import {useState, useEffect, useRef} from 'react'; |
| 2 |
import {useLocation} from 'react-router-dom'; |
| 3 |
import {useSelector} from 'react-redux'; |
| 4 |
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome'; |
| 5 |
|
| 6 |
import './style.scss'; |
| 7 |
|
| 8 |
const MobileMenu = ({children}) => { |
| 9 |
const [isOpen, setIsOpen] = useState(false); |
| 10 |
|
| 11 |
const toggleRef = useRef(null); |
| 12 |
|
| 13 |
useEffect(() => { |
| 14 |
const handleClick = (evt) => { |
| 15 |
if (toggleRef.current && !toggleRef.current.contains(evt.target)) { |
| 16 |
setIsOpen(false); |
| 17 |
} |
| 18 |
}; |
| 19 |
|
| 20 |
if (isOpen) { |
| 21 |
document.addEventListener('click', handleClick); |
| 22 |
} |
| 23 |
|
| 24 |
return function cleanup() { |
| 25 |
if (isOpen) { |
| 26 |
document.removeEventListener('click', handleClick); |
| 27 |
} |
| 28 |
}; |
| 29 |
}, [isOpen, toggleRef]); |
| 30 |
|
| 31 |
const location = useLocation(); |
| 32 |
const tabsSelector = useSelector((state) => state.tabs); |
| 33 |
|
| 34 |
const slug = location.pathname.length > 2 ? location.pathname.split('/')[1] : 'dashboard'; |
| 35 |
const label = tabsSelector[slug] ? tabsSelector[slug].label : null; |
| 36 |
|
| 37 |
return ( |
| 38 |
<div className="give-donor-dashboard-mobile-menu"> |
| 39 |
<div className="give-donor-dashboard-mobile-menu__header"> |
| 40 |
<div className="give-donor-dashboard-mobile-menu__label">{label}</div> |
| 41 |
<div |
| 42 |
ref={toggleRef} |
| 43 |
className={`give-donor-dashboard-mobile-menu__toggle ${ |
| 44 |
isOpen ? 'give-donor-dashboard-mobile-menu__toggle--toggled' : '' |
| 45 |
}`} |
| 46 |
onClick={() => { |
| 47 |
setIsOpen(!isOpen); |
| 48 |
}} |
| 49 |
> |
| 50 |
<FontAwesomeIcon icon="bars" /> |
| 51 |
</div> |
| 52 |
</div> |
| 53 |
{isOpen && ( |
| 54 |
<div className="give-donor-dashboard-mobile-menu__content"> |
| 55 |
{children} |
| 56 |
</div> |
| 57 |
)} |
| 58 |
</div> |
| 59 |
); |
| 60 |
}; |
| 61 |
|
| 62 |
export default MobileMenu; |
| 63 |
|