mirror of
https://github.com/vercel/commerce.git
synced 2025-03-31 17:25:53 +00:00
Dropdown Menu
This commit is contained in:
parent
df6c9070e5
commit
1b0777a487
@ -30,10 +30,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
& .dropdownMenuContainer {
|
& .dropdownMenuContainer {
|
||||||
@apply flex-col py-6 bg-primary h-full justify-around h-auto;
|
@apply flex-col py-6 bg-primary h-full justify-around;
|
||||||
|
|
||||||
@screen lg {
|
@screen lg {
|
||||||
@apply border border-accents-1 shadow-lg py-2;
|
@apply border border-accents-1 shadow-lg py-2 h-auto;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,31 +1,36 @@
|
|||||||
import { FC, useState } from 'react'
|
import Link from 'next/link'
|
||||||
import cn from 'classnames'
|
import cn from 'classnames'
|
||||||
import useCart from '@lib/bigcommerce/cart/use-cart'
|
import s from './UserNav.module.css'
|
||||||
|
import { FC, useState, useRef, useCallback } from 'react'
|
||||||
|
import { useTheme } from 'next-themes'
|
||||||
import { Avatar } from '@components/core'
|
import { Avatar } from '@components/core'
|
||||||
import { Heart, Bag } from '@components/icon'
|
import { Heart, Bag } from '@components/icon'
|
||||||
import { useUI } from '@components/ui/context'
|
import { useUI } from '@components/ui/context'
|
||||||
import s from './UserNav.module.css'
|
import { FocusScope } from '@react-aria/focus'
|
||||||
import Link from 'next/link'
|
|
||||||
import { Logo } from '@components/ui'
|
import {
|
||||||
import { Toggle } from '@components/core'
|
useOverlay,
|
||||||
import { useTheme } from 'next-themes'
|
DismissButton,
|
||||||
|
usePreventScroll,
|
||||||
|
} from '@react-aria/overlays'
|
||||||
|
import useCart from '@lib/bigcommerce/cart/use-cart'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string
|
className?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const countItem = (count: number, item: any) => count + item.quantity
|
const countItem = (count: number, item: any) => count + item.quantity
|
||||||
|
|
||||||
const countItems = (count: number, items: any[]) =>
|
const countItems = (count: number, items: any[]) =>
|
||||||
items.reduce(countItem, count)
|
items.reduce(countItem, count)
|
||||||
|
|
||||||
const UserNav: FC<Props> = ({ className }) => {
|
const UserNav: FC<Props> = ({ className, children, ...props }) => {
|
||||||
const { data } = useCart()
|
const { data } = useCart()
|
||||||
const [displayDropdown, setDisplayDropdown] = useState(true)
|
|
||||||
const { openSidebar, closeSidebar, displaySidebar } = useUI()
|
const { openSidebar, closeSidebar, displaySidebar } = useUI()
|
||||||
|
const [displayDropdown, setDisplayDropdown] = useState(false)
|
||||||
const itemsCount = Object.values(data?.line_items ?? {}).reduce(countItems, 0)
|
const itemsCount = Object.values(data?.line_items ?? {}).reduce(countItems, 0)
|
||||||
const { theme, setTheme } = useTheme()
|
let ref = useRef() as React.MutableRefObject<HTMLInputElement>
|
||||||
|
|
||||||
|
const toggleDropdown = () => setDisplayDropdown((v) => !v)
|
||||||
return (
|
return (
|
||||||
<nav className={cn(s.root, className)}>
|
<nav className={cn(s.root, className)}>
|
||||||
<div className={s.mainContainer}>
|
<div className={s.mainContainer}>
|
||||||
@ -46,18 +51,47 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
<Heart />
|
<Heart />
|
||||||
</li>
|
</li>
|
||||||
</Link>
|
</Link>
|
||||||
<li
|
<li className={s.item} onClick={() => toggleDropdown()}>
|
||||||
className={s.item}
|
|
||||||
onClick={() => {
|
|
||||||
setDisplayDropdown((i) => !i)
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Avatar />
|
<Avatar />
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
<DismissButton onDismiss={() => setDisplayDropdown(false)} />
|
||||||
|
{displayDropdown && (
|
||||||
|
<DropdownMenu
|
||||||
|
onClose={() => setDisplayDropdown(false)}
|
||||||
|
innerRef={ref}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</nav>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
<div className={cn(s.dropdownMenu, { [s.off]: displayDropdown })}>
|
interface DropdownMenuProps {
|
||||||
|
onClose: () => void
|
||||||
|
innerRef: React.MutableRefObject<HTMLInputElement>
|
||||||
|
}
|
||||||
|
|
||||||
|
const DropdownMenu: FC<DropdownMenuProps> = ({
|
||||||
|
onClose,
|
||||||
|
children,
|
||||||
|
innerRef,
|
||||||
|
...props
|
||||||
|
}) => {
|
||||||
|
const { theme, setTheme } = useTheme()
|
||||||
|
|
||||||
|
let { overlayProps } = useOverlay(
|
||||||
|
{
|
||||||
|
onClose: onClose,
|
||||||
|
isOpen: true,
|
||||||
|
},
|
||||||
|
innerRef
|
||||||
|
)
|
||||||
|
|
||||||
|
usePreventScroll()
|
||||||
|
return (
|
||||||
|
<FocusScope contain restoreFocus autoFocus>
|
||||||
|
<div className={cn(s.dropdownMenu)} ref={innerRef} {...overlayProps}>
|
||||||
<nav className={s.dropdownMenuContainer}>
|
<nav className={s.dropdownMenuContainer}>
|
||||||
<Link href="#">
|
<Link href="#">
|
||||||
<a className={s.link}>My Purchases</a>
|
<a className={s.link}>My Purchases</a>
|
||||||
@ -80,7 +114,7 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
</Link>
|
</Link>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</FocusScope>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user