Removing Portal, since it's not needed. We might add it later I'd rather not to.

This commit is contained in:
Bel Curcio 2021-06-06 17:20:34 -03:00
parent 00d1d2ac02
commit 4891ffed7f
5 changed files with 59 additions and 78 deletions

View File

@ -76,18 +76,22 @@ const Layout: FC<Props> = ({
<main className="fit">{children}</main>
<Footer pages={pageProps.pages} />
<Modal open={displayModal} onClose={closeModal}>
{modalView === 'LOGIN_VIEW' && <LoginView />}
{modalView === 'SIGNUP_VIEW' && <SignUpView />}
{modalView === 'FORGOT_VIEW' && <ForgotPassword />}
</Modal>
{displayModal && (
<Modal onClose={closeModal}>
{modalView === 'LOGIN_VIEW' && <LoginView />}
{modalView === 'SIGNUP_VIEW' && <SignUpView />}
{modalView === 'FORGOT_VIEW' && <ForgotPassword />}
</Modal>
)}
<Sidebar open={displaySidebar} onClose={closeSidebar}>
{sidebarView === 'CART_VIEW' && <CartSidebarView />}
{sidebarView === 'CHECKOUT_VIEW' && <CheckoutSidebarView />}
{sidebarView === 'PAYMENT_VIEW' && <PaymentMethodView />}
{sidebarView === 'SHIPPING_VIEW' && <ShippingView />}
</Sidebar>
{displaySidebar && (
<Sidebar onClose={closeSidebar}>
{sidebarView === 'CART_VIEW' && <CartSidebarView />}
{sidebarView === 'CHECKOUT_VIEW' && <CheckoutSidebarView />}
{sidebarView === 'PAYMENT_VIEW' && <PaymentMethodView />}
{sidebarView === 'SHIPPING_VIEW' && <ShippingView />}
</Sidebar>
)}
<FeatureBar
title="This site uses cookies to improve your experience. By clicking, you agree to our Privacy Policy."

View File

@ -1,22 +1,16 @@
import { FC, useRef, useEffect, useCallback } from 'react'
import Portal from '@reach/portal'
import s from './Modal.module.css'
import { Cross } from '@components/icons'
import {
disableBodyScroll,
enableBodyScroll,
clearAllBodyScrollLocks,
} from 'body-scroll-lock'
import FocusTrap from '@lib/focus-trap'
import { Cross } from '@components/icons'
import { disableBodyScroll, clearAllBodyScrollLocks } from 'body-scroll-lock'
interface ModalProps {
className?: string
children?: any
open?: boolean
onClose: () => void
onEnter?: () => void | null
}
const Modal: FC<ModalProps> = ({ children, open, onClose, onEnter = null }) => {
const Modal: FC<ModalProps> = ({ children, onClose }) => {
const ref = useRef() as React.MutableRefObject<HTMLDivElement>
const handleKey = useCallback(
@ -30,12 +24,8 @@ const Modal: FC<ModalProps> = ({ children, open, onClose, onEnter = null }) => {
useEffect(() => {
if (ref.current) {
if (open) {
disableBodyScroll(ref.current)
window.addEventListener('keydown', handleKey)
} else {
enableBodyScroll(ref.current)
}
disableBodyScroll(ref.current)
window.addEventListener('keydown', handleKey)
}
return () => {
window.removeEventListener('keydown', handleKey)
@ -44,22 +34,18 @@ const Modal: FC<ModalProps> = ({ children, open, onClose, onEnter = null }) => {
}, [open, handleKey])
return (
<Portal>
{open ? (
<div className={s.root}>
<div className={s.modal} role="dialog" ref={ref}>
<button
onClick={() => onClose()}
aria-label="Close panel"
className="hover:text-gray-500 transition ease-in-out duration-150 focus:outline-none absolute right-0 top-0 m-6"
>
<Cross className="h-6 w-6" />
</button>
<FocusTrap focusFirst>{children}</FocusTrap>
</div>
</div>
) : null}
</Portal>
<div className={s.root}>
<div className={s.modal} role="dialog" ref={ref}>
<button
onClick={() => onClose()}
aria-label="Close panel"
className="hover:text-gray-500 transition ease-in-out duration-150 focus:outline-none absolute right-0 top-0 m-6"
>
<Cross className="h-6 w-6" />
</button>
<FocusTrap focusFirst>{children}</FocusTrap>
</div>
</div>
)
}

View File

@ -6,3 +6,9 @@
@apply h-full flex flex-col text-base bg-accent-0 shadow-xl overflow-y-auto;
min-width: 335px;
}
.backdrop {
@apply absolute inset-0 bg-black bg-opacity-40 transition transition-opacity duration-100 ease-linear;
backdrop-filter: blur(0.8px);
-webkit-backdrop-filter: blur(0.8px);
}

View File

@ -1,6 +1,6 @@
import s from './Sidebar.module.css'
import Portal from '@reach/portal'
import { FC, useEffect, useRef } from 'react'
import s from './Sidebar.module.css'
import cn from 'classnames'
import {
disableBodyScroll,
enableBodyScroll,
@ -9,47 +9,33 @@ import {
interface SidebarProps {
children: any
open: boolean
onClose: () => void
}
const Sidebar: FC<SidebarProps> = ({ children, open = false, onClose }) => {
const Sidebar: FC<SidebarProps> = ({ children, onClose }) => {
const ref = useRef() as React.MutableRefObject<HTMLDivElement>
// useEffect(() => {
// // setTimeout(() => {
// // if (ref.current && open) {
// // window.document.body.style.overflow = 'hidden'
// // disableBodyScroll(ref.current)
// // } else {
// // window.document.body.style.overflow &&
// // setTimeout(() => (window.document.body.style.overflow = 'unset'), 30)
// // !!ref.current && enableBodyScroll(ref.current)
// // }
// // }, 30)
// return () => {
// clearAllBodyScrollLocks()
// }
// }, [open])
useEffect(() => {
console.log('Sidebar', ref)
if (ref.current) {
disableBodyScroll(ref.current)
}
return () => {
clearAllBodyScrollLocks()
}
}, [])
return (
<Portal>
{open && (
<div className={s.root} ref={ref}>
<div className="absolute inset-0 overflow-hidden">
<div
className="absolute inset-0 bg-black bg-opacity-50 transition-opacity"
onClick={onClose}
/>
<section className="absolute inset-y-0 right-0 pl-10 max-w-full flex sm:pl-16 outline-none">
<div className="h-full md:w-screen md:max-w-md">
<div className={s.sidebar}>{children}</div>
</div>
</section>
<div className={cn(s.root)} ref={ref}>
<div className="absolute inset-0 overflow-hidden">
<div className={s.backdrop} onClick={onClose} />
<section className="absolute inset-y-0 right-0 pl-10 max-w-full flex sm:pl-16 outline-none">
<div className="h-full md:w-screen md:max-w-md">
<div className={s.sidebar}>{children}</div>
</div>
</div>
)}
</Portal>
</section>
</div>
</div>
)
}

View File

@ -19,7 +19,6 @@
"node": "14.x"
},
"dependencies": {
"@reach/portal": "^0.15.0",
"@react-spring/web": "^9.2.1",
"@vercel/fetch": "^6.1.0",
"autoprefixer": "^10.2.6",