diff --git a/src/components/HamburgerMenu/index.tsx b/src/components/HamburgerMenu/index.tsx index 9a2a76d850..3557cb15a8 100644 --- a/src/components/HamburgerMenu/index.tsx +++ b/src/components/HamburgerMenu/index.tsx @@ -1,33 +1,46 @@ import cn from 'classnames' -import React, { useCallback, useState, useEffect } from 'react' +import React, { + useEffect, + useState, + useCallback, + MouseEvent, + KeyboardEvent +} from 'react' import HamburgerIcon from '../HamburgerIcon' import Link from '../Link' - import { logEvent } from '../../utils/front/ga' + import { getFirstPage } from '../../utils/shared/sidebar' import { ReactComponent as LogoSVG } from '../../../static/img/logo-white.svg' import { ReactComponent as TwitterIcon } from '../SocialIcon/twitter.svg' import { ReactComponent as GithubIcon } from '../SocialIcon/github.svg' -import { useHeaderIsScrolled } from '../../utils/front/scroll' import styles from './styles.module.css' const docsPage = getFirstPage() -const HamburgerMenu: React.FC = () => { - const [isOpened, setOpened] = useState(false) - const collapsed = useHeaderIsScrolled() +export type HamburgerHelpers = { + opened: boolean + setOpened: (newState: boolean) => void + handleToggle: () => void + handleKeyDown: (e: KeyboardEvent) => void + handleClose: () => void + handleItemClick: (name: string) => (e: MouseEvent) => void +} + +export const useHamburgerMenu: () => HamburgerHelpers = () => { + const [opened, setOpened] = useState(false) - const toggleMobileMenu = useCallback(() => setOpened(!isOpened), [isOpened]) - const openOnEnterKey = useCallback(e => { + const handleToggle = useCallback(() => setOpened(!opened), [opened]) + const handleKeyDown = useCallback(e => { if (e.which === 13) { - toggleMobileMenu() + handleToggle() } }, []) - const close = useCallback(() => setOpened(false), [isOpened]) - const itemClick = useCallback( + const handleClose = useCallback(() => setOpened(false), [opened]) + const handleItemClick = useCallback( item => (): void => { close() logEvent('hamburger', item) @@ -36,197 +49,217 @@ const HamburgerMenu: React.FC = () => { ) useEffect(() => { - const method = isOpened ? 'add' : 'remove' - + const method = opened ? 'add' : 'remove' document.body.classList[method](styles.hiddenScrollbar) - }, [isOpened]) + }, [opened]) - return ( -