From 9678306b2365a25feebf68ccbd5eaa5b65a0137d Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Wed, 21 Jun 2023 15:13:10 -0500 Subject: [PATCH] Fixes cart closing and reopening with first interaction (#1053) * Works * Adds animation back --- components/cart/button.tsx | 64 ------------------- components/cart/index.tsx | 4 +- components/cart/modal.tsx | 122 +++++++++++++++++++++---------------- 3 files changed, 73 insertions(+), 117 deletions(-) delete mode 100644 components/cart/button.tsx diff --git a/components/cart/button.tsx b/components/cart/button.tsx deleted file mode 100644 index aed87ee7a..000000000 --- a/components/cart/button.tsx +++ /dev/null @@ -1,64 +0,0 @@ -'use client'; - -import { useEffect, useRef, useState } from 'react'; -import { useCookies } from 'react-cookie'; - -import CartIcon from 'components/icons/cart'; -import CartModal from './modal'; - -import type { Cart } from 'lib/shopify/types'; - -export default function CartButton({ - cart, - cartIdUpdated -}: { - cart: Cart; - cartIdUpdated: boolean; -}) { - const [, setCookie] = useCookies(['cartId']); - const [cartIsOpen, setCartIsOpen] = useState(false); - const quantityRef = useRef(cart.totalQuantity); - - // Temporary hack to update the `cartId` cookie when it changes since we cannot update it - // on the server-side (yet). - useEffect(() => { - if (cartIdUpdated) { - setCookie('cartId', cart.id, { - path: '/', - sameSite: 'strict', - secure: process.env.NODE_ENV === 'production' - }); - } - return; - }, [setCookie, cartIdUpdated, cart.id]); - - useEffect(() => { - // Open cart modal when when quantity changes. - if (cart.totalQuantity !== quantityRef.current) { - // But only if it's not already open (quantity also changes when editing items in cart). - if (!cartIsOpen) { - setCartIsOpen(true); - } - - // Always update the quantity reference - quantityRef.current = cart.totalQuantity; - } - }, [cartIsOpen, cart.totalQuantity, quantityRef]); - - return ( - <> - setCartIsOpen(false)} cart={cart} /> - - - - ); -} diff --git a/components/cart/index.tsx b/components/cart/index.tsx index 0e3ffcdc1..75b6978fe 100644 --- a/components/cart/index.tsx +++ b/components/cart/index.tsx @@ -1,6 +1,6 @@ import { createCart, getCart } from 'lib/shopify'; import { cookies } from 'next/headers'; -import CartButton from './button'; +import CartModal from './modal'; export default async function Cart() { const cartId = cookies().get('cartId')?.value; @@ -19,5 +19,5 @@ export default async function Cart() { cartIdUpdated = true; } - return ; + return ; } diff --git a/components/cart/modal.tsx b/components/cart/modal.tsx index 4220cfe94..82c2a310a 100644 --- a/components/cart/modal.tsx +++ b/components/cart/modal.tsx @@ -1,14 +1,18 @@ -import { Dialog } from '@headlessui/react'; -import { AnimatePresence, motion } from 'framer-motion'; +'use client'; + +import { Dialog, Transition } from '@headlessui/react'; import Image from 'next/image'; import Link from 'next/link'; +import CartIcon from 'components/icons/cart'; import CloseIcon from 'components/icons/close'; import ShoppingBagIcon from 'components/icons/shopping-bag'; import Price from 'components/price'; import { DEFAULT_OPTION } from 'lib/constants'; import type { Cart } from 'lib/shopify/types'; import { createUrl } from 'lib/utils'; +import { Fragment, useEffect, useRef, useState } from 'react'; +import { useCookies } from 'react-cookie'; import DeleteItemButton from './delete-item-button'; import EditItemQuantityButton from './edit-item-quantity-button'; @@ -16,53 +20,70 @@ type MerchandiseSearchParams = { [key: string]: string; }; -export default function CartModal({ - isOpen, - onClose, - cart -}: { - isOpen: boolean; - onClose: () => void; - cart: Cart; -}) { - return ( - - {isOpen && ( - -