mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Setup Mobile Menu Sidebar
This commit is contained in:
@@ -13,6 +13,7 @@ import { useAcceptCookies } from '@lib/hooks/useAcceptCookies'
|
|||||||
import { Sidebar, Button, Modal, LoadingDots } from '@components/ui'
|
import { Sidebar, Button, Modal, LoadingDots } from '@components/ui'
|
||||||
import PaymentMethodView from '@components/checkout/PaymentMethodView'
|
import PaymentMethodView from '@components/checkout/PaymentMethodView'
|
||||||
import CheckoutSidebarView from '@components/checkout/CheckoutSidebarView'
|
import CheckoutSidebarView from '@components/checkout/CheckoutSidebarView'
|
||||||
|
import MenuSidebarView from '../UserNav/MenuSidebarView'
|
||||||
|
|
||||||
import LoginView from '@components/auth/LoginView'
|
import LoginView from '@components/auth/LoginView'
|
||||||
import s from './Layout.module.css'
|
import s from './Layout.module.css'
|
||||||
@@ -69,12 +70,14 @@ const ModalUI: FC = () => {
|
|||||||
) : null
|
) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
const SidebarView: FC<{ sidebarView: string; closeSidebar(): any }> = ({
|
const SidebarView: FC<{
|
||||||
sidebarView,
|
sidebarView: string
|
||||||
closeSidebar,
|
closeSidebar(): any
|
||||||
}) => {
|
links: any
|
||||||
|
}> = ({ sidebarView, closeSidebar, links }) => {
|
||||||
return (
|
return (
|
||||||
<Sidebar onClose={closeSidebar}>
|
<Sidebar onClose={closeSidebar}>
|
||||||
|
{sidebarView === 'MOBILEMENU_VIEW' && <MenuSidebarView links={links} />}
|
||||||
{sidebarView === 'CART_VIEW' && <CartSidebarView />}
|
{sidebarView === 'CART_VIEW' && <CartSidebarView />}
|
||||||
{sidebarView === 'CHECKOUT_VIEW' && <CheckoutSidebarView />}
|
{sidebarView === 'CHECKOUT_VIEW' && <CheckoutSidebarView />}
|
||||||
{sidebarView === 'PAYMENT_VIEW' && <PaymentMethodView />}
|
{sidebarView === 'PAYMENT_VIEW' && <PaymentMethodView />}
|
||||||
@@ -83,10 +86,14 @@ const SidebarView: FC<{ sidebarView: string; closeSidebar(): any }> = ({
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
const SidebarUI: FC = () => {
|
const SidebarUI: FC<{ links: any }> = ({ links }) => {
|
||||||
const { displaySidebar, closeSidebar, sidebarView } = useUI()
|
const { displaySidebar, closeSidebar, sidebarView } = useUI()
|
||||||
return displaySidebar ? (
|
return displaySidebar ? (
|
||||||
<SidebarView sidebarView={sidebarView} closeSidebar={closeSidebar} />
|
<SidebarView
|
||||||
|
sidebarView={sidebarView}
|
||||||
|
closeSidebar={closeSidebar}
|
||||||
|
links={links}
|
||||||
|
/>
|
||||||
) : null
|
) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +115,7 @@ const Layout: FC<Props> = ({
|
|||||||
<main className="fit">{children}</main>
|
<main className="fit">{children}</main>
|
||||||
<Footer pages={pageProps.pages} />
|
<Footer pages={pageProps.pages} />
|
||||||
<ModalUI />
|
<ModalUI />
|
||||||
<SidebarUI />
|
<SidebarUI links={navBarlinks} />
|
||||||
<FeatureBar
|
<FeatureBar
|
||||||
title="This site uses cookies to improve your experience. By clicking, you agree to our Privacy Policy."
|
title="This site uses cookies to improve your experience. By clicking, you agree to our Privacy Policy."
|
||||||
hide={acceptedCookies}
|
hide={acceptedCookies}
|
||||||
|
@@ -9,6 +9,7 @@ interface Link {
|
|||||||
href: string
|
href: string
|
||||||
label: string
|
label: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface NavbarProps {
|
interface NavbarProps {
|
||||||
links?: Link[]
|
links?: Link[]
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,38 @@
|
|||||||
|
import cn from 'classnames'
|
||||||
|
import Link from 'next/link'
|
||||||
|
import { FC } from 'react'
|
||||||
|
import { useUI } from '@components/ui/context'
|
||||||
|
import SidebarLayout from '@components/common/SidebarLayout'
|
||||||
|
|
||||||
|
interface Link {
|
||||||
|
href: string
|
||||||
|
label: string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MenuProps {
|
||||||
|
links?: Link[]
|
||||||
|
}
|
||||||
|
|
||||||
|
const MenuSidebarView: FC<MenuProps> = (props) => {
|
||||||
|
const { closeSidebar } = useUI()
|
||||||
|
const handleClose = () => closeSidebar()
|
||||||
|
|
||||||
|
console.log(props.links)
|
||||||
|
return (
|
||||||
|
<SidebarLayout handleClose={handleClose}>
|
||||||
|
<div className="px-4 sm:px-6 flex-1">
|
||||||
|
<ul>
|
||||||
|
{props.links?.map((l: any) => (
|
||||||
|
<li key={l.href}>
|
||||||
|
<Link href={l.href}>
|
||||||
|
<a>{l.label}</a>
|
||||||
|
</Link>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</SidebarLayout>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MenuSidebarView
|
1
components/common/UserNav/MenuSidebarView/index.ts
Normal file
1
components/common/UserNav/MenuSidebarView/index.ts
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export { default } from './MenuSidebarView'
|
@@ -10,6 +10,7 @@ import { useUI } from '@components/ui/context'
|
|||||||
import Button from '@components/ui/Button'
|
import Button from '@components/ui/Button'
|
||||||
import DropdownMenu from './DropdownMenu'
|
import DropdownMenu from './DropdownMenu'
|
||||||
import s from './UserNav.module.css'
|
import s from './UserNav.module.css'
|
||||||
|
import Menu from '@components/icons/Menu'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
className?: string
|
className?: string
|
||||||
@@ -20,7 +21,8 @@ const countItem = (count: number, item: LineItem) => count + item.quantity
|
|||||||
const UserNav: FC<Props> = ({ className }) => {
|
const UserNav: FC<Props> = ({ className }) => {
|
||||||
const { data } = useCart()
|
const { data } = useCart()
|
||||||
const { data: customer } = useCustomer()
|
const { data: customer } = useCustomer()
|
||||||
const { toggleSidebar, closeSidebarIfPresent, openModal } = useUI()
|
const { toggleSidebar, closeSidebarIfPresent, openModal, setSidebarView } =
|
||||||
|
useUI()
|
||||||
const itemsCount = data?.lineItems.reduce(countItem, 0) ?? 0
|
const itemsCount = data?.lineItems.reduce(countItem, 0) ?? 0
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -28,9 +30,16 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
<ul className={s.list}>
|
<ul className={s.list}>
|
||||||
{process.env.COMMERCE_CART_ENABLED && (
|
{process.env.COMMERCE_CART_ENABLED && (
|
||||||
<li className={s.item}>
|
<li className={s.item}>
|
||||||
<Button className={s.item} variant="naked" onClick={toggleSidebar} aria-label="Cart">
|
<Button
|
||||||
|
className={s.item}
|
||||||
|
variant="naked"
|
||||||
|
onClick={toggleSidebar}
|
||||||
|
aria-label="Cart"
|
||||||
|
>
|
||||||
<Bag />
|
<Bag />
|
||||||
{itemsCount > 0 && <span className={s.bagCount}>{itemsCount}</span>}
|
{itemsCount > 0 && (
|
||||||
|
<span className={s.bagCount}>{itemsCount}</span>
|
||||||
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
</li>
|
</li>
|
||||||
)}
|
)}
|
||||||
@@ -58,6 +67,19 @@ const UserNav: FC<Props> = ({ className }) => {
|
|||||||
)}
|
)}
|
||||||
</li>
|
</li>
|
||||||
)}
|
)}
|
||||||
|
<li className="block lg:hidden">
|
||||||
|
<Button
|
||||||
|
className={s.item}
|
||||||
|
variant="naked"
|
||||||
|
onClick={() => {
|
||||||
|
setSidebarView('MOBILEMENU_VIEW')
|
||||||
|
toggleSidebar()
|
||||||
|
}}
|
||||||
|
aria-label="Menu"
|
||||||
|
>
|
||||||
|
<Menu />
|
||||||
|
</Button>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
)
|
)
|
||||||
|
21
components/icons/Menu.tsx
Normal file
21
components/icons/Menu.tsx
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
const Menu = ({ ...props }) => {
|
||||||
|
return (
|
||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
className="h-6 w-6"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
stroke="currentColor"
|
||||||
|
{...props}
|
||||||
|
>
|
||||||
|
<path
|
||||||
|
strokeLinecap="round"
|
||||||
|
strokeLinejoin="round"
|
||||||
|
strokeWidth={2}
|
||||||
|
d="M4 6h16M4 12h16m-7 6h7"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Menu
|
Reference in New Issue
Block a user