Skip to content

Commit

Permalink
refactor: detect touch screens for header navigation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
r41ph committed Jan 22, 2025
1 parent 45e118f commit 447306c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import React, { useRef } from 'react';
import {
MenuList,
Paper,
Popover,
useMediaQuery,
useTheme,
} from '@mui/material';
import { MenuList, Paper, Popover, useMediaQuery } from '@mui/material';
import cx from 'clsx';

import DropdownIcon from '../../../icons/DropdownIcon';
Expand Down Expand Up @@ -38,8 +32,8 @@ const HeaderMenuItemHover = ({
children,
}: Props): JSX.Element => {
const { classes: styles } = useMenuHoverStyles();
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down('sm'));
// eslint-disable-next-line lingui/no-unlocalized-strings
const isTouchScreen = useMediaQuery('(pointer: coarse)');
const popoverAnchor = useRef(null);
const [anchorEl, setAnchorEl] = React.useState<HTMLElement | null>(null);

Expand All @@ -63,9 +57,9 @@ const HeaderMenuItemHover = ({
ref={popoverAnchor}
aria-owns={open ? 'mouse-over-popover' : undefined}
aria-haspopup="true"
onMouseEnter={isMobile ? undefined : handlePopoverOpen}
onMouseEnter={isTouchScreen ? undefined : handlePopoverOpen}
onMouseLeave={handlePopoverClose}
onClick={isMobile ? handlePopoverToggle : undefined}
onClick={isTouchScreen ? handlePopoverToggle : undefined}
>
{title && (
<span className={classes?.title}>
Expand Down
6 changes: 4 additions & 2 deletions web-components/src/components/mobile-menu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from 'react';
import { useTheme } from '@mui/material';
import { useMediaQuery, useTheme } from '@mui/material';
import Box from '@mui/material/Box';
import Drawer from '@mui/material/Drawer';
import MenuItem from '@mui/material/MenuItem';
Expand Down Expand Up @@ -34,6 +34,8 @@ const MobileMenu: React.FC<React.PropsWithChildren<Props>> = ({
}) => {
const { classes: styles, cx } = useMobileMenuStyles();
const theme = useTheme();
// eslint-disable-next-line lingui/no-unlocalized-strings
const isTouchScreen = useMediaQuery('(pointer: coarse)');
const [open, setOpen] = useState(false);

const handleToggle = () => {
Expand All @@ -53,7 +55,7 @@ const MobileMenu: React.FC<React.PropsWithChildren<Props>> = ({
<HamburgerIcon
className={cx(styles.hamburger, styles.icon)}
onClick={handleToggle}
onMouseLeave={handleClose}
onMouseLeave={isTouchScreen ? handleClose : undefined}
width="29px"
height="22px"
sx={{ ml: { xs: isUserLoggedIn ? 0 : 2, sm: 4 } }}
Expand Down

0 comments on commit 447306c

Please sign in to comment.