-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
48ddc20
commit 0bc4e6f
Showing
2 changed files
with
96 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,73 @@ | ||
import { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { UserMenu, LogoutLink } from '../UserMenu/UserMenu'; | ||
import { MenuWrapper } from './MobileMenu.styled'; | ||
import { MenuWrapper, Overlay, CloseButton } from './MobileMenu.styled'; | ||
|
||
export const MobileMenu = ({ isOpen, handleClick }) => { | ||
const [menuIsOpen, setMenuIsOpen] = useState(isOpen); | ||
|
||
const openMenu = () => { | ||
setMenuIsOpen(true); | ||
}; | ||
|
||
const closeMenu = () => { | ||
setMenuIsOpen(false); | ||
}; | ||
|
||
const handleEscKey = event => { | ||
if (event.key === 'Escape') { | ||
closeMenu(); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
if (menuIsOpen) { | ||
window.addEventListener('keydown', handleEscKey); | ||
} | ||
|
||
return () => { | ||
window.removeEventListener('keydown', handleEscKey); | ||
}; | ||
}, [menuIsOpen]); | ||
|
||
const handleBackdropClick = event => { | ||
if (event.target === event.currentTarget) { | ||
closeMenu(); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
setMenuIsOpen(isOpen); | ||
}, [isOpen]); | ||
|
||
export const MobileMenu = ({ isShown, handleClick }) => { | ||
return ( | ||
<MenuWrapper className={isShown ? 'shown' : 'hidden'}> | ||
<UserMenu handleClick={handleClick}></UserMenu> | ||
<LogoutLink to="/"> | ||
<span>Logout</span> | ||
<svg width="20" height="20"> | ||
<use href="../../../src/images/sprite.svg#icon-log-out"></use> | ||
</svg> | ||
</LogoutLink> | ||
</MenuWrapper> | ||
<> | ||
{menuIsOpen && <Overlay onClick={closeMenu}></Overlay>} | ||
<MenuWrapper | ||
className={menuIsOpen ? 'shown' : 'hidden'} | ||
onClick={handleBackdropClick} | ||
> | ||
<CloseButton onClick={closeMenu}> | ||
<svg width="20" height="20"> | ||
<use href="../../../src/images/sprite.svg#icon-x"></use> | ||
</svg> | ||
</CloseButton> | ||
|
||
<UserMenu handleClick={handleClick}></UserMenu> | ||
<LogoutLink to="/"> | ||
<span>Logout</span> | ||
<svg width="20" height="20"> | ||
<use href="../../../src/images/sprite.svg#icon-log-out"></use> | ||
</svg> | ||
</LogoutLink> | ||
</MenuWrapper> | ||
</> | ||
); | ||
}; | ||
|
||
MobileMenu.propTypes = { | ||
isShown: PropTypes.bool.isRequired, | ||
isOpen: PropTypes.bool.isRequired, | ||
handleClick: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default MobileMenu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters