4
0
forked from crowetic/commerce

Merge branch 'master' into jb/product-improvements

This commit is contained in:
Julián Benegas 2020-10-22 11:01:38 -03:00
commit b3fec00939
9 changed files with 219 additions and 141 deletions

View File

@ -0,0 +1,29 @@
.dropdownMenu {
@apply fixed top-0 right-0 z-20 w-full h-full;
@screen lg {
@apply absolute right-0 w-screen;
max-width: 200px;
}
& .dropdownMenuContainer {
@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 {
@apply flex space-x-2 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 items-center;
text-transform: capitalize;
& .icons svg {
@apply w-6 h-6;
}
}
&.off {
@apply hidden;
}
}

View File

@ -0,0 +1,75 @@
import { useTheme } from 'next-themes'
import s from './DropdownMenu.module.css'
import { FC } from 'react'
import { FocusScope } from '@react-aria/focus'
import {
useOverlay,
DismissButton,
usePreventScroll,
} from '@react-aria/overlays'
import Link from 'next/link'
import cn from 'classnames'
import { Moon, Sun } from '@components/icon'
interface DropdownMenuProps {
onClose: () => void
innerRef: React.MutableRefObject<HTMLInputElement>
}
const DropdownMenu: FC<DropdownMenuProps> = ({
onClose,
children,
innerRef,
...props
}) => {
const { theme, setTheme } = useTheme()
let { overlayProps } = useOverlay(
{
isDismissable: true,
onClose: onClose,
isOpen: true,
},
innerRef
)
usePreventScroll()
return (
<FocusScope restoreFocus>
<div className={cn(s.dropdownMenu)} ref={innerRef} {...overlayProps}>
{/* Needed placeholder for User Interation*/}
<div className="flex justify-end">
<span onClick={onClose} className="bg-transparent h-12 w-12" />
</div>
<nav className={s.dropdownMenuContainer}>
<Link href="#">
<a className={s.link}>My Purchases</a>
</Link>
<Link href="#">
<a className={s.link}>My Account</a>
</Link>
<a
className={s.link}
onClick={() =>
theme === 'dark' ? setTheme('light') : setTheme('dark')
}
>
<span>
Theme: <strong>{theme}</strong>{' '}
</span>
<span className={s.icons}>
{theme === 'dark' ? <Moon /> : <Sun />}
</span>
</a>
<Link href="#">
<a className={cn(s.link, 'border-t border-accents-2 mt-4')}>
Logout
</a>
</Link>
</nav>
</div>
</FocusScope>
)
}
export default DropdownMenu

View File

@ -12,35 +12,14 @@
.item {
@apply mr-6 cursor-pointer relative transition ease-in-out duration-150 text-base flex items-center;
&:hover {
@apply text-accents-8;
@apply text-accents-8 transition ease-in-out duration-100 transform scale-110;
}
&.heart:hover svg {
fill: var(--accents-9);
}
&:last-child {
@apply mr-0;
}
}
.dropdownMenu {
@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;
}
& .dropdownMenuContainer {
@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 {
@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;
}
}

View File

@ -1,18 +1,13 @@
import { FC, useEffect, useState, useRef } from 'react'
import Link from 'next/link'
import cn from 'classnames'
import s from './UserNav.module.css'
import { useTheme } from 'next-themes'
import { FC, useRef } from 'react'
import { Avatar } from '@components/core'
import { Heart, Bag } from '@components/icon'
import { useUI } from '@components/ui/context'
import { FocusScope } from '@react-aria/focus'
import DropdownMenu from './DropdownMenu'
import {
useOverlay,
DismissButton,
usePreventScroll,
} from '@react-aria/overlays'
import useCart from '@lib/bigcommerce/cart/use-cart'
interface Props {
@ -25,36 +20,18 @@ const countItems = (count: number, items: any[]) =>
const UserNav: FC<Props> = ({ className, children, ...props }) => {
const { data } = useCart()
const { openSidebar, closeSidebar, displaySidebar } = useUI()
const [displayDropdown, setDisplayDropdown] = useState(false)
const {
openSidebar,
closeSidebar,
displaySidebar,
displayDropdown,
openDropdown,
closeDropdown,
} = useUI()
const itemsCount = Object.values(data?.line_items ?? {}).reduce(countItems, 0)
let ref = useRef() as React.MutableRefObject<HTMLInputElement>
useEffect(() => {
function handleClick(e: any) {
const isInside = e?.target?.closest(`#user-dropdown`) !== null
if (isInside) return
setDisplayDropdown(false)
document.removeEventListener('click', handleClick)
}
function handleKeyPress(e: KeyboardEvent) {
if (e.key === 'Escape') {
setDisplayDropdown(false)
document.removeEventListener('keydown', handleKeyPress)
}
}
if (displayDropdown) {
document.addEventListener('click', handleClick)
document.addEventListener('keydown', handleKeyPress)
return () => {
document.removeEventListener('click', handleClick)
document.removeEventListener('keydown', handleKeyPress)
}
}
}, [displayDropdown])
const toggleDropdown = () => setDisplayDropdown((v) => !v)
return (
<nav className={cn(s.root, className)}>
<div className={s.mainContainer}>
@ -71,75 +48,24 @@ const UserNav: FC<Props> = ({ className, children, ...props }) => {
)}
</li>
<Link href="/wishlist">
<li className={s.item}>
<li className={cn(s.item, s.heart)}>
<Heart />
</li>
</Link>
<li className={s.item} onClick={toggleDropdown}>
<li
className={s.item}
onClick={() => (displayDropdown ? closeDropdown() : openDropdown())}
>
<Avatar />
</li>
</ul>
</div>
<DismissButton onDismiss={() => setDisplayDropdown(false)} />
{displayDropdown && (
<DropdownMenu
onClose={() => setDisplayDropdown(false)}
innerRef={ref}
/>
<DropdownMenu onClose={closeDropdown} 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}>
<Link href="#">
<a className={s.link}>My Purchases</a>
</Link>
<Link href="#">
<a className={s.link}>My Account</a>
</Link>
<a
className={s.link}
onClick={() =>
theme === 'dark' ? setTheme('light') : setTheme('dark')
}
>
Theme: <strong>{theme}</strong>
</a>
<Link href="#">
<a className={cn(s.link, 'border-t border-accents-2 mt-4')}>
Logout
</a>
</Link>
</nav>
</div>
</FocusScope>
)
}
export default UserNav

19
components/icon/Moon.tsx Normal file
View File

@ -0,0 +1,19 @@
const Moon = ({ ...props }) => {
return (
<svg
viewBox="0 0 24 24"
width="24"
height="24"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
fill="none"
shape-rendering="geometricPrecision"
>
<path d="M21 12.79A9 9 0 1111.21 3 7 7 0 0021 12.79z" />
</svg>
)
}
export default Moon

27
components/icon/Sun.tsx Normal file
View File

@ -0,0 +1,27 @@
const Sun = ({ ...props }) => {
return (
<svg
viewBox="0 0 24 24"
width="24"
height="24"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
fill="none"
shape-rendering="geometricPrecision"
>
<circle cx="12" cy="12" r="5" />
<path d="M12 1v2" />
<path d="M12 21v2" />
<path d="M4.22 4.22l1.42 1.42" />
<path d="M18.36 18.36l1.42 1.42" />
<path d="M1 12h2" />
<path d="M21 12h2" />
<path d="M4.22 19.78l1.42-1.42" />
<path d="M18.36 5.64l1.42-1.42" />
</svg>
)
}
export default Sun

View File

@ -6,3 +6,5 @@ export { default as ArrowLeft } from './ArrowLeft'
export { default as Plus } from './Plus'
export { default as Minus } from './Minus'
export { default as Check } from './Check'
export { default as Sun } from './Sun'
export { default as Moon } from './Moon'

View File

@ -34,7 +34,7 @@
}
.nameBox {
@apply absolute top-6 left-0 z-30;
@apply absolute top-6 left-0 z-10;
& .name {
@apply px-6 py-2 bg-primary text-primary font-bold;

View File

@ -4,10 +4,12 @@ import { SSRProvider, OverlayProvider } from 'react-aria'
export interface State {
displaySidebar: boolean
displayDropdown: boolean
}
const initialState = {
displaySidebar: false,
displayDropdown: false,
}
type Action =
@ -17,21 +19,61 @@ type Action =
| {
type: 'CLOSE_SIDEBAR'
}
| {
type: 'OPEN_DROPDOWN'
}
| {
type: 'CLOSE_DROPDOWN'
}
export const UIContext = React.createContext<State | any>(initialState)
UIContext.displayName = 'UIContext'
function uiReducer(state: State, action: Action) {
switch (action.type) {
case 'OPEN_SIDEBAR': {
return {
...state,
displaySidebar: true,
}
}
case 'CLOSE_SIDEBAR': {
return {
...state,
displaySidebar: false,
}
}
case 'OPEN_DROPDOWN': {
return {
...state,
displayDropdown: true,
}
}
case 'CLOSE_DROPDOWN': {
return {
...state,
displayDropdown: false,
}
}
}
}
export const UIProvider: FC = (props) => {
const [state, dispatch] = React.useReducer(uiReducer, initialState)
const openSidebar = () => dispatch({ type: 'OPEN_SIDEBAR' })
const closeSidebar = () => dispatch({ type: 'CLOSE_SIDEBAR' })
const openDropdown = () => dispatch({ type: 'OPEN_DROPDOWN' })
const closeDropdown = () => dispatch({ type: 'CLOSE_DROPDOWN' })
const value = {
...state,
openSidebar,
closeSidebar,
openDropdown,
closeDropdown,
}
return <UIContext.Provider value={value} {...props} />
@ -45,27 +87,6 @@ export const useUI = () => {
return context
}
function uiReducer(state: State, action: Action) {
switch (action.type) {
case 'OPEN_SIDEBAR': {
return !state.displaySidebar
? {
...state,
displaySidebar: true,
}
: state
}
case 'CLOSE_SIDEBAR': {
return state.displaySidebar
? {
...state,
displaySidebar: false,
}
: state
}
}
}
export const ManagedUIContext: FC = ({ children }) => (
<UIProvider>
<ThemeProvider>