Skip to content

Commit

Permalink
Fix open cart drawer on header
Browse files Browse the repository at this point in the history
  • Loading branch information
hta218 committed Dec 13, 2024
1 parent 6a67894 commit de1e3c6
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 72 deletions.
16 changes: 3 additions & 13 deletions app/components/header/cart-count.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,22 @@ import { useIsHydrated } from "~/hooks/use-is-hydrated";
import type { RootLoader } from "~/root";

export function CartCount({
isHome,
openCart,
isTransparent,
}: {
isHome: boolean;
openCart: () => void;
isTransparent: boolean;
}) {
let rootData = useRouteLoaderData<RootLoader>("root");
return (
<Suspense
fallback={
<Badge
count={0}
dark={isHome}
openCart={openCart}
isTransparent={isTransparent}
/>
<Badge count={0} openCart={openCart} isTransparent={isTransparent} />
}
>
<Await resolve={rootData?.cart}>
{(cart) => (
<Badge
dark={isHome}
openCart={openCart}
count={cart?.totalQuantity || 0}
cart={cart}
Expand All @@ -44,13 +36,11 @@ export function CartCount({

function Badge({
openCart,
dark,
count,
cart,
isTransparent,
}: {
count: number;
dark: boolean;
openCart: () => void;
cart?: any;
isTransparent: boolean;
Expand All @@ -63,11 +53,11 @@ function Badge({
{count > 0 && (
<div
className={clsx(
"text-[12px] leading-none text-center font-medium subpixel-antialiased",
"text-sm leading-none text-center font-medium subpixel-antialiased",
"flex items-center justify-center min-w-4 rounded-full p-0.5",
"absolute top-0 -right-1",
"transition-colors duration-300",
"group-hover/header:bg-[var(--color-header-text)] group-hover/header:text-[--color-header-bg]",
"group-hover/header:bg-[--color-header-text] group-hover/header:text-[--color-header-bg]",
isTransparent
? "text-[--color-header-text] bg-[--color-transparent-header-text]"
: "bg-[--color-header-text] text-[--color-header-bg]",
Expand Down
8 changes: 2 additions & 6 deletions app/components/header/desktop-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { Logo } from "~/components/logo";
import { cn } from "~/lib/cn";
import { useIsHomePath } from "~/lib/utils";
import type { RootLoader } from "~/root";
import { CartDrawer } from "./cart-drawer";
import { DesktopMenu } from "./menu/desktop-menu";
import { PredictiveSearchButton } from "./predictive-search";
import { CartCount } from "./cart-count";

let variants = cva("", {
variants: {
Expand Down Expand Up @@ -72,11 +72,7 @@ export function DesktopHeader() {
<div className="flex items-center gap-1 z-1">
<PredictiveSearchButton />
<AccountLink className="relative flex items-center justify-center w-8 h-8" />
<CartCount
isHome={isHome}
openCart={() => {}}
isTransparent={isTransparent}
/>
<CartDrawer isTransparent={isTransparent} />
</div>
</div>
</header>
Expand Down
47 changes: 5 additions & 42 deletions app/components/header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { Await, useRouteLoaderData } from "@remix-run/react";
import { CartForm, type CartReturn } from "@shopify/hydrogen";
import { Suspense, useEffect } from "react";
import { CartForm } from "@shopify/hydrogen";
import { useEffect } from "react";
import { useCartFetchers } from "~/hooks/use-cart-fetchers";
import { Cart } from "~/modules/cart";
import type { RootLoader } from "~/root";
import { CartLoading } from "../../modules/cart-loading";
import { Drawer, useDrawer } from "../../modules/drawer";
import { useDrawer } from "../../modules/drawer";
import { DesktopHeader } from "./desktop-header";
import { MobileHeader } from "./mobile-header";
import { ScrollingAnnouncement } from "./scrolling-announcement";

export function Header() {
let {
isOpen: isCartOpen,
openDrawer: openCart,
closeDrawer: closeCart,
} = useDrawer();
let { isOpen: isCartOpen, openDrawer: openCart } = useDrawer();

let addToCartFetchers = useCartFetchers(CartForm.ACTIONS.LinesAdd);
// toggle cart drawer when adding to cart
Expand All @@ -26,38 +18,9 @@ export function Header() {

return (
<>
{/* <CartDrawer isOpen={isCartOpen} onClose={closeCart} /> */}
<ScrollingAnnouncement />
<DesktopHeader />
<MobileHeader openCart={openCart} />
<MobileHeader />
</>
);
}

function CartDrawer({
isOpen,
onClose,
}: {
isOpen: boolean;
onClose: () => void;
}) {
const rootData = useRouteLoaderData<RootLoader>("root");

return (
<Drawer open={isOpen} onClose={onClose} heading="Cart" openFrom="right">
<div className="grid">
<Suspense fallback={<CartLoading />}>
<Await resolve={rootData?.cart}>
{(cart) => (
<Cart
layout="drawer"
onClose={onClose}
cart={cart as CartReturn}
/>
)}
</Await>
</Suspense>
</div>
</Drawer>
);
}
14 changes: 3 additions & 11 deletions app/components/header/mobile-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ import { Logo } from "~/components/logo";
import { cn } from "~/lib/cn";
import { useIsHomePath } from "~/lib/utils";
import type { RootLoader } from "~/root";
import { CartCount } from "./cart-count";
import { CartDrawer } from "./cart-drawer";
import { MobileMenu } from "./menu/mobile-menu";

export function MobileHeader({
openCart,
}: {
openCart: () => void;
}) {
export function MobileHeader() {
let isHome = useIsHomePath();
let { enableTransparentHeader } = useThemeSettings();
let { y } = useWindowScroll();
Expand Down Expand Up @@ -65,11 +61,7 @@ export function MobileHeader({
<Logo isTransparent={isTransparent} />
<div className="flex items-center justify-end w-full">
<AccountLink className="relative flex items-center justify-center w-8 h-8" />
<CartCount
isHome={isHome}
openCart={openCart}
isTransparent={isTransparent}
/>
<CartDrawer isTransparent={isTransparent} />
</div>
</header>
);
Expand Down

0 comments on commit de1e3c6

Please sign in to comment.