feat: use hover to open dropdown-menu

This commit is contained in:
Ou 2024-10-21 15:23:51 +08:00
parent 0e2d5bc25b
commit d285f37eeb

View File

@ -1,8 +1,8 @@
import { Homepage } from "@shared/consts" import { Homepage } from "@shared/consts"
import clsx from "clsx" import clsx from "clsx"
import { motion } from "framer-motion" import { motion } from "framer-motion"
import { useRef, useState } from "react" import { useEffect, useRef, useState } from "react"
import { useClickAway } from "react-use" import { useHoverDirty } from "react-use"
import { useDark } from "~/hooks/useDark" import { useDark } from "~/hooks/useDark"
import { useLogin } from "~/hooks/useLogin" import { useLogin } from "~/hooks/useLogin"
@ -21,13 +21,14 @@ function ThemeToggle() {
export function Menu() { export function Menu() {
const { loggedIn, login, logout, enabledLogin, userInfo } = useLogin() const { loggedIn, login, logout, enabledLogin, userInfo } = useLogin()
const [shown, show] = useState(false) const [shown, show] = useState(false)
const ref = useRef<HTMLDivElement>(null) const ref = useRef<HTMLElement>(null)
useClickAway(ref, () => { const isHover = useHoverDirty(ref)
show(false) useEffect(() => {
}) show(isHover)
}, [shown, isHover])
return ( return (
<span ref={ref} className="relative"> <span ref={ref} className="relative">
<span className="flex items-center scale-90" onClick={() => show(!shown)}> <span className="flex items-center scale-90">
{ {
enabledLogin && loggedIn && userInfo.avatar enabledLogin && loggedIn && userInfo.avatar
? ( ? (
@ -46,10 +47,11 @@ export function Menu() {
} }
</span> </span>
{shown && ( {shown && (
<div className="absolute right-0 z-99 bg-transparent pt-8 top-0">
<motion.div <motion.div
id="dropdown-menu" id="dropdown-menu"
className={clsx([ className={clsx([
"absolute top-2rem right-0 z-99 w-200px", "w-200px",
"bg-primary p-1px backdrop-blur-5 bg-op-70! rounded-lg", "bg-primary p-1px backdrop-blur-5 bg-op-70! rounded-lg",
])} ])}
initial={{ initial={{
@ -79,6 +81,7 @@ export function Menu() {
</li> </li>
</ol> </ol>
</motion.div> </motion.div>
</div>
)} )}
</span> </span>
) )