Skip to content

Commit

Permalink
perf: menu
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Jun 24, 2024
1 parent b94c72e commit c2253b2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/web/components/common/MyMenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import MyIcon from '../Icon';
import MyDivider from '../MyDivider';
import type { IconNameType } from '../Icon/type';
import { useSystem } from '../../../hooks/useSystem';

export type MenuItemType = 'primary' | 'danger';

Expand Down Expand Up @@ -77,10 +78,14 @@ const MyMenu = ({
alignItems: 'center',
fontSize: 'sm'
};

const { isPc } = useSystem();
const ref = useRef<HTMLDivElement>(null);
const closeTimer = useRef<any>();
const [isOpen, setIsOpen] = useState(false);

const formatTrigger = !isPc ? 'click' : trigger;

useOutsideClick({
ref: ref,
handler: () => {
Expand All @@ -103,17 +108,18 @@ const MyMenu = ({
isLazy
lazyBehavior={'keepMounted'}
placement="bottom-start"
computePositionOnMount
>
<Box
ref={ref}
onMouseEnter={() => {
if (trigger === 'hover') {
if (formatTrigger === 'hover') {
setIsOpen(true);
}
clearTimeout(closeTimer.current);
}}
onMouseLeave={() => {
if (trigger === 'hover') {
if (formatTrigger === 'hover') {
closeTimer.current = setTimeout(() => {
setIsOpen(false);
}, 100);
Expand All @@ -124,7 +130,7 @@ const MyMenu = ({
position={'relative'}
onClickCapture={(e) => {
e.stopPropagation();
if (trigger === 'click') {
if (formatTrigger === 'click') {
setIsOpen(!isOpen);
}
}}
Expand Down
7 changes: 7 additions & 0 deletions packages/web/hooks/useSystem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useMediaQuery } from '@chakra-ui/react';

export const useSystem = () => {
const [isPc] = useMediaQuery('(min-width: 900px)');

return { isPc };
};

0 comments on commit c2253b2

Please sign in to comment.