Skip to content

Commit

Permalink
Fixed the profile menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Pdzly committed Dec 17, 2023
1 parent b6f0ca9 commit 83679e6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/components/profile-menu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ import {
Button,
Menu, MenuHandler, MenuList
} from '@material-tailwind/react';
import React, { useEffect } from 'react';
import React, { useEffect, useState } from 'react';
import { UserCircleIcon } from '@heroicons/react/24/outline';
import { useTheme } from '@/utils/theme';
import Link from 'next/link';
import { useClickOutside } from '@/utils/clickAway';
import Icon, { ICON_SIZE } from '../icon';
import ThemeSwitch from '../theme-switch';
import { LinkText } from '../text';

const ProfileMenu = () => {
const [theme, setTheme] = useTheme();
const [open, setOpen] = useState<boolean>(false);

const ref = useClickOutside<HTMLUListElement>(() => open && setOpen(false));

useEffect(() => {
document.documentElement.classList.remove('dark');
Expand All @@ -27,13 +31,13 @@ const ProfileMenu = () => {
}[] = [
{
item:
(
<Link href="/user/todo" className="justify-center flex">
<LinkText className="text-center w-full">
Profile
</LinkText>
</Link>
)
(
<Link href="/user/todo" className="justify-center flex">
<LinkText className="text-center w-full">
Profile
</LinkText>
</Link>
)
},
{
item: <ThemeSwitch key="theme-switcher" mode={theme as 'dark' | 'light'} onSwitch={setTheme} />
Expand All @@ -44,13 +48,18 @@ const ProfileMenu = () => {
<div className="flex justify-center items-center">
<Menu
placement="bottom"
open={open}
handler={setOpen}
>
<MenuHandler>
<Button aria-label="ProfileButton" className="bg-transparent">
<Button
aria-label="ProfileButton"
className="bg-transparent"
>
<Icon IconType={UserCircleIcon} size={ICON_SIZE.MEDIUM} title="User icon" isInteractable />
</Button>
</MenuHandler>
<MenuList className="w-full relative md:w-80 flex flex-col bg-primary dark:bg-primary-dark dark:border-gray-800 rounded-b-none rounded-t-md md:rounded-b-md md:rounded-t-none" aria-label="ProfileMenu">
<MenuList ref={ref} className="w-full relative md:w-80 flex flex-col bg-primary dark:bg-primary-dark dark:border-gray-800 rounded-b-none rounded-t-md md:rounded-b-md md:rounded-t-none" aria-label="ProfileMenu">
{items.map(({ item }) => (
<div className="w-full mt-0 top-0 pt-0 bg-none border-t first:border-t-0 border-gray-500 z-50" key={item.key}>
{item}
Expand Down
24 changes: 24 additions & 0 deletions src/utils/clickAway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useEffect, useRef } from 'react';

export function useClickOutside<T extends HTMLElement>(callback: () => void) {
const ref = useRef<T | null>(null);

useEffect(() => {
function handleClick(event: MouseEvent) {
const el = ref?.current;
// Do nothing if clicking ref's element or descendent elements
if (!el || el.contains(event.target as Node)) {
return;
}

callback();
}

window?.addEventListener('mousedown', handleClick, { capture: true });
return () => {
window?.removeEventListener('mousedown', handleClick, { capture: true });
};
}, [callback, ref]);

return ref;
}

0 comments on commit 83679e6

Please sign in to comment.