feat: use floating-ui menu
This commit is contained in:
@@ -23,6 +23,7 @@ import {
|
||||
useNotifications,
|
||||
} from "@crossbell/notification"
|
||||
import { useAccountBalance } from "~/hooks/use-account-balance"
|
||||
import { Menu } from "~/components/ui/Menu"
|
||||
|
||||
export const ConnectButton: React.FC<{
|
||||
left?: boolean
|
||||
@@ -127,85 +128,92 @@ export const ConnectButton: React.FC<{
|
||||
}
|
||||
return (
|
||||
<div
|
||||
className="relative group"
|
||||
className="relative"
|
||||
style={{ gap: 12, height: avatarSize + "px" }}
|
||||
>
|
||||
{userSites.isSuccess ? (
|
||||
<>
|
||||
<button
|
||||
className="flex items-center w-full"
|
||||
type="button"
|
||||
aria-label="connector"
|
||||
>
|
||||
<Avatar
|
||||
className="align-middle mr-2"
|
||||
images={userSites.data?.[0]?.avatars || []}
|
||||
name={userSites.data?.[0]?.name}
|
||||
size={avatarSize}
|
||||
/>
|
||||
<div className="flex-1 flex flex-col min-w-0">
|
||||
<span
|
||||
className={`text-left leading-none font-medium truncate ${
|
||||
InsufficientBalance ? "text-red-600" : "text-gray-600"
|
||||
} ${size === "base" ? "text-base" : "text-sm"}`}
|
||||
style={{ marginBottom: "0.15rem" }}
|
||||
<Menu
|
||||
placement="bottom-end"
|
||||
target={
|
||||
<button
|
||||
className="flex items-center w-full"
|
||||
type="button"
|
||||
aria-label="connector"
|
||||
>
|
||||
{userSites.data?.[0]?.name ||
|
||||
getAccountDisplayName(account)}
|
||||
</span>
|
||||
{userSites.data?.[0]?.username && (
|
||||
<span
|
||||
className={`text-left leading-none ${
|
||||
sizeDecrease === "sm" ? "text-sm" : "text-xs"
|
||||
} truncate ${
|
||||
InsufficientBalance ? "text-red-400" : "text-gray-400"
|
||||
}`}
|
||||
>
|
||||
{"@" + userSites.data?.[0]?.username ||
|
||||
getAccountDisplayName(account)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
<div
|
||||
className={`absolute hidden ${
|
||||
left ? "left" : "right"
|
||||
}-0 pt-2 group-hover:block top-full z-10 text-gray-600`}
|
||||
>
|
||||
<div
|
||||
className={`bg-white rounded-lg ring-1 ring-zinc-100 min-w-[140px] shadow-md py-2 ${
|
||||
size === "base" ? "text-base" : "text-sm"
|
||||
}`}
|
||||
>
|
||||
{InsufficientBalance && (
|
||||
<UniLink
|
||||
href="https://faucet.crossbell.io/"
|
||||
className="text-red-600 px-4 h-8 flex items-center w-full whitespace-nowrap hover:bg-zinc-100"
|
||||
>
|
||||
<span className="mr-2 fill-red-600 i-bxs:bell"></span>
|
||||
Insufficient $CSB balance ({balance?.formatted})
|
||||
</UniLink>
|
||||
)}
|
||||
{dropdownLinks.map((link, i) => {
|
||||
return (
|
||||
<UniLink
|
||||
key={i}
|
||||
href={link.url}
|
||||
onClick={link.onClick}
|
||||
className={`${
|
||||
size === "base" ? "pl-5 pr-6 h-11" : "pl-4 pr-5 h-9"
|
||||
} flex items-center w-full whitespace-nowrap hover:bg-zinc-100`}
|
||||
aria-label={link.label}
|
||||
<Avatar
|
||||
className="align-middle mr-2"
|
||||
images={userSites.data?.[0]?.avatars || []}
|
||||
name={userSites.data?.[0]?.name}
|
||||
size={avatarSize}
|
||||
/>
|
||||
<div className="flex-1 flex flex-col min-w-0">
|
||||
<span
|
||||
className={`text-left leading-none font-medium truncate ${
|
||||
InsufficientBalance
|
||||
? "text-red-600"
|
||||
: "text-gray-600"
|
||||
} ${size === "base" ? "text-base" : "text-sm"}`}
|
||||
style={{ marginBottom: "0.15rem" }}
|
||||
>
|
||||
<span className="mr-2 flex justify-center">
|
||||
{link.icon}
|
||||
{userSites.data?.[0]?.name ||
|
||||
getAccountDisplayName(account)}
|
||||
</span>
|
||||
{userSites.data?.[0]?.username && (
|
||||
<span
|
||||
className={`text-left leading-none ${
|
||||
sizeDecrease === "sm" ? "text-sm" : "text-xs"
|
||||
} truncate ${
|
||||
InsufficientBalance
|
||||
? "text-red-400"
|
||||
: "text-gray-400"
|
||||
}`}
|
||||
>
|
||||
{"@" + userSites.data?.[0]?.username ||
|
||||
getAccountDisplayName(account)}
|
||||
</span>
|
||||
{link.label}
|
||||
)}
|
||||
</div>
|
||||
</button>
|
||||
}
|
||||
dropdown={
|
||||
<div
|
||||
className={`text-gray-600 bg-white rounded-lg ring-1 ring-zinc-100 min-w-[140px] shadow-md py-2 ${
|
||||
size === "base" ? "text-base" : "text-sm"
|
||||
} mt-1`}
|
||||
>
|
||||
{InsufficientBalance && (
|
||||
<UniLink
|
||||
href="https://faucet.crossbell.io/"
|
||||
className="text-red-600 px-4 h-8 flex items-center w-full whitespace-nowrap hover:bg-zinc-100"
|
||||
>
|
||||
<span className="mr-2 fill-red-600 i-bxs:bell"></span>
|
||||
Insufficient $CSB balance ({balance?.formatted})
|
||||
</UniLink>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
{dropdownLinks.map((link, i) => {
|
||||
return (
|
||||
<UniLink
|
||||
key={i}
|
||||
href={link.url}
|
||||
onClick={link.onClick}
|
||||
className={`${
|
||||
size === "base"
|
||||
? "pl-5 pr-6 h-11"
|
||||
: "pl-4 pr-5 h-9"
|
||||
} flex items-center w-full whitespace-nowrap hover:bg-zinc-100`}
|
||||
aria-label={link.label}
|
||||
>
|
||||
<span className="mr-2 flex justify-center">
|
||||
{link.icon}
|
||||
</span>
|
||||
{link.label}
|
||||
</UniLink>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
</>
|
||||
) : (
|
||||
""
|
||||
|
||||
@@ -19,6 +19,7 @@ import { XFeedIcon } from "~/components/icons/XFeedIcon"
|
||||
import { FastAverageColor } from "fast-average-color"
|
||||
import { useState, useRef, useEffect, RefObject } from "react"
|
||||
import chroma from "chroma-js"
|
||||
import { Menu } from "~/components/ui/Menu"
|
||||
|
||||
export type HeaderLinkType = {
|
||||
icon?: React.ReactNode
|
||||
@@ -183,37 +184,35 @@ export const SiteHeader: React.FC<{
|
||||
<FollowingCount siteId={site?.username} />
|
||||
<FollowingButton
|
||||
site={site}
|
||||
className="text-accent mr-5 min-[438px]:mx-5"
|
||||
className="text-accent mr-1 min-[438px]:ml-5 min-[438px]:mr-3"
|
||||
variant="text"
|
||||
/>
|
||||
<div className="relative inline-block align-middle h-7 group">
|
||||
<Button
|
||||
variant="text"
|
||||
onClick={(e) => {
|
||||
e.stopPropagation()
|
||||
}}
|
||||
aria-label="more"
|
||||
>
|
||||
<EllipsisHorizontalIcon className="w-5 h-5" />
|
||||
</Button>
|
||||
<div className="absolute hidden right-0 sm:left-0 pt-2 group-hover:block top-full z-10 text-gray-600 w-52">
|
||||
<div className="bg-white rounded-lg ring-1 ring-zinc-100 shadow-md py-2 text-sm">
|
||||
{moreMenuItems.map((item) => {
|
||||
return (
|
||||
<UniLink
|
||||
key={item.text}
|
||||
href={item.url}
|
||||
className="h-10 flex w-full space-x-2 items-center px-3 hover:bg-gray-100"
|
||||
>
|
||||
<span className="fill-gray-500 flex">
|
||||
{item.icon}
|
||||
</span>
|
||||
<span>{item.text}</span>
|
||||
</UniLink>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
<div className="relative inline-block align-middle">
|
||||
<Menu
|
||||
target={
|
||||
<Button variant="text" aria-label="more">
|
||||
<EllipsisHorizontalIcon className="w-5 h-5 mx-2" />
|
||||
</Button>
|
||||
}
|
||||
dropdown={
|
||||
<div className="text-gray-600 bg-white rounded-lg ring-1 ring-zinc-100 shadow-md py-2 text-sm">
|
||||
{moreMenuItems.map((item) => {
|
||||
return (
|
||||
<UniLink
|
||||
key={item.text}
|
||||
href={item.url}
|
||||
className="h-10 flex w-full space-x-2 items-center px-3 hover:bg-gray-100"
|
||||
>
|
||||
<span className="fill-gray-500 flex">
|
||||
{item.icon}
|
||||
</span>
|
||||
<span>{item.text}</span>
|
||||
</UniLink>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
import { useState } from "react"
|
||||
import {
|
||||
offset,
|
||||
flip,
|
||||
shift,
|
||||
autoUpdate,
|
||||
useFloating,
|
||||
useInteractions,
|
||||
useClick,
|
||||
useRole,
|
||||
useDismiss,
|
||||
useTransitionStyles,
|
||||
Placement,
|
||||
} from "@floating-ui/react"
|
||||
|
||||
export const Menu: React.FC<{
|
||||
target: JSX.Element
|
||||
dropdown: JSX.Element
|
||||
placement?: Placement
|
||||
}> = ({ target, dropdown, placement }) => {
|
||||
const [isOpen, setIsOpen] = useState(false)
|
||||
|
||||
const { x, y, strategy, refs, context } = useFloating({
|
||||
placement: placement || "bottom-start",
|
||||
open: isOpen,
|
||||
onOpenChange: setIsOpen,
|
||||
middleware: [offset(5), flip(), shift({ padding: 8 })],
|
||||
whileElementsMounted: autoUpdate,
|
||||
})
|
||||
|
||||
const click = useClick(context)
|
||||
const { getReferenceProps, getFloatingProps } = useInteractions([
|
||||
click,
|
||||
useRole(context, { role: "tooltip" }),
|
||||
useDismiss(context),
|
||||
])
|
||||
|
||||
const { isMounted, styles } = useTransitionStyles(context, {
|
||||
duration: 100,
|
||||
})
|
||||
|
||||
return (
|
||||
<>
|
||||
<div ref={refs.setReference} {...getReferenceProps()}>
|
||||
{target}
|
||||
</div>
|
||||
{isMounted && (
|
||||
<div
|
||||
ref={refs.setFloating}
|
||||
className="z-10 w-max"
|
||||
style={{
|
||||
position: strategy,
|
||||
top: y ?? "0",
|
||||
left: x ?? "0",
|
||||
...styles,
|
||||
}}
|
||||
{...getFloatingProps()}
|
||||
>
|
||||
{dropdown}
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user