forked from crowetic/commerce
Merge branch 'master' into jb/product-improvements
This commit is contained in:
commit
9aace115b6
@ -3,14 +3,14 @@ import { FC } from 'react'
|
|||||||
import Link from 'next/link'
|
import Link from 'next/link'
|
||||||
import { useTheme } from 'next-themes'
|
import { useTheme } from 'next-themes'
|
||||||
import { Logo } from '@components/ui'
|
import { Logo } from '@components/ui'
|
||||||
import { Searchbar, Toggle, UserNav } from '@components/core'
|
import { Searchbar, UserNav } from '@components/core'
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string
|
className?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const Navbar: FC<Props> = ({ className }) => {
|
const Navbar: FC<Props> = ({ className }) => {
|
||||||
const rootClassName = className
|
const rootClassName = className
|
||||||
const { theme, setTheme } = useTheme()
|
|
||||||
return (
|
return (
|
||||||
<div className={rootClassName}>
|
<div className={rootClassName}>
|
||||||
<div className="flex justify-between align-center flex-row py-4 md:py-6 relative">
|
<div className="flex justify-between align-center flex-row py-4 md:py-6 relative">
|
||||||
@ -38,12 +38,6 @@ const Navbar: FC<Props> = ({ className }) => {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex flex-1 justify-end space-x-8">
|
<div className="flex flex-1 justify-end space-x-8">
|
||||||
<Toggle
|
|
||||||
checked={theme === 'dark'}
|
|
||||||
onChange={() =>
|
|
||||||
theme === 'dark' ? setTheme('light') : setTheme('dark')
|
|
||||||
}
|
|
||||||
/>
|
|
||||||
<UserNav />
|
<UserNav />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -2,8 +2,11 @@
|
|||||||
@apply relative;
|
@apply relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.mainContainer {
|
||||||
|
}
|
||||||
|
|
||||||
.list {
|
.list {
|
||||||
@apply flex flex-row items-center h-full;
|
@apply flex flex-row items-center justify-items-end h-full;
|
||||||
}
|
}
|
||||||
|
|
||||||
.item {
|
.item {
|
||||||
@ -18,14 +21,26 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.dropdownMenu {
|
.dropdownMenu {
|
||||||
@apply absolute right-0 mt-2 w-screen max-w-xs sm:px-0 z-50 border border-accents-1 bg-primary;
|
@apply bg-primary fixed right-0 z-50 w-full h-full;
|
||||||
|
|
||||||
|
@screen lg {
|
||||||
|
@apply absolute mt-3 right-0 w-screen;
|
||||||
max-width: 160px;
|
max-width: 160px;
|
||||||
|
}
|
||||||
|
|
||||||
& .dropdownMenuContainer {
|
& .dropdownMenuContainer {
|
||||||
@apply shadow-lg overflow-hidden relative grid py-2;
|
@apply flex-col py-6 bg-primary h-full justify-around;
|
||||||
|
|
||||||
|
@screen lg {
|
||||||
|
@apply border border-accents-1 shadow-lg py-2 h-auto;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
& .link {
|
& .link {
|
||||||
@apply px-6 py-3 block space-y-1 hover:bg-accents-1 transition ease-in-out duration-150 text-base leading-6 font-medium text-gray-900;
|
@apply cursor-pointer px-6 py-3 block space-y-1 hover:bg-accents-1 transition ease-in-out duration-150 text-base leading-6 font-medium text-gray-900;
|
||||||
|
}
|
||||||
|
|
||||||
|
&.off {
|
||||||
|
@apply hidden;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,25 +1,34 @@
|
|||||||
import { FC, useEffect, useState } from 'react'
|
import { FC, useEffect, useState, useRef } 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 { 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 {
|
||||||
|
useOverlay,
|
||||||
|
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(false)
|
|
||||||
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)
|
||||||
|
let ref = useRef() as React.MutableRefObject<HTMLInputElement>
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function handleClick(e: any) {
|
function handleClick(e: any) {
|
||||||
@ -45,8 +54,10 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
}
|
}
|
||||||
}, [displayDropdown])
|
}, [displayDropdown])
|
||||||
|
|
||||||
|
const toggleDropdown = () => setDisplayDropdown((v) => !v)
|
||||||
return (
|
return (
|
||||||
<nav className={cn(s.root, className)}>
|
<nav className={cn(s.root, className)}>
|
||||||
|
<div className={s.mainContainer}>
|
||||||
<ul className={s.list}>
|
<ul className={s.list}>
|
||||||
<li
|
<li
|
||||||
className={s.item}
|
className={s.item}
|
||||||
@ -64,21 +75,47 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
<Heart />
|
<Heart />
|
||||||
</li>
|
</li>
|
||||||
</Link>
|
</Link>
|
||||||
<button
|
<li className={s.item} onClick={toggleDropdown}>
|
||||||
className={cn(
|
|
||||||
s.item,
|
|
||||||
'rounded-full focus:shadow-outline-blue focus:outline-none'
|
|
||||||
)}
|
|
||||||
onClick={() => {
|
|
||||||
setDisplayDropdown((i) => !i)
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
<Avatar />
|
<Avatar />
|
||||||
</button>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
</div>
|
||||||
|
<DismissButton onDismiss={() => setDisplayDropdown(false)} />
|
||||||
{displayDropdown && (
|
{displayDropdown && (
|
||||||
<div className={cn(s.dropdownMenu, 'shadow-lg')} id="user-dropdown">
|
<DropdownMenu
|
||||||
|
onClose={() => setDisplayDropdown(false)}
|
||||||
|
innerRef={ref}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</nav>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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>
|
||||||
@ -86,14 +123,22 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
<Link href="#">
|
<Link href="#">
|
||||||
<a className={s.link}>My Account</a>
|
<a className={s.link}>My Account</a>
|
||||||
</Link>
|
</Link>
|
||||||
<div className="my-1 h-px w-full bg-accents-2" />
|
<a
|
||||||
|
className={s.link}
|
||||||
|
onClick={() =>
|
||||||
|
theme === 'dark' ? setTheme('light') : setTheme('dark')
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Theme: <strong>{theme}</strong>
|
||||||
|
</a>
|
||||||
<Link href="#">
|
<Link href="#">
|
||||||
<a className={cn(s.link)}>Logout</a>
|
<a className={cn(s.link, 'border-t border-accents-2 mt-4')}>
|
||||||
|
Logout
|
||||||
|
</a>
|
||||||
</Link>
|
</Link>
|
||||||
</nav>
|
</nav>
|
||||||
</div>
|
</div>
|
||||||
)}
|
</FocusScope>
|
||||||
</nav>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ interface Props {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const Container: FC<Props> = ({ children, className, el = 'div' }) => {
|
const Container: FC<Props> = ({ children, className, el = 'div' }) => {
|
||||||
const rootClassName = cn('mx-auto max-w-7xl px-3 md:px-6', className)
|
const rootClassName = cn('mx-auto max-w-7xl px-6', className)
|
||||||
|
|
||||||
let Component: React.ComponentType<React.HTMLAttributes<
|
let Component: React.ComponentType<React.HTMLAttributes<
|
||||||
HTMLDivElement
|
HTMLDivElement
|
||||||
|
Loading…
x
Reference in New Issue
Block a user