-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from buildheadless/pdp
Add AddToCartForm and Cart route
- Loading branch information
Showing
14 changed files
with
640 additions
and
532 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import {cx} from 'class-variance-authority'; | ||
import {twMerge} from 'tailwind-merge'; | ||
|
||
export type IconProps = JSX.IntrinsicElements['svg'] & { | ||
direction?: 'down' | 'left' | 'right' | 'up'; | ||
}; | ||
|
||
export function Icon({ | ||
children, | ||
className, | ||
fill = 'currentColor', | ||
stroke, | ||
...props | ||
}: IconProps) { | ||
return ( | ||
<svg | ||
viewBox="0 0 20 20" | ||
xmlns="http://www.w3.org/2000/svg" | ||
{...props} | ||
className={twMerge(cx('h-5 w-5', className))} | ||
fill={fill} | ||
stroke={stroke} | ||
> | ||
{children} | ||
</svg> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type {IconProps} from './Icon'; | ||
|
||
import {Icon} from './Icon'; | ||
|
||
export function IconBag(props: IconProps) { | ||
return ( | ||
<Icon {...props}> | ||
<title>Bag</title> | ||
<path | ||
d="M8.125 5a1.875 1.875 0 0 1 3.75 0v.375h-3.75V5Zm-1.25.375V5a3.125 3.125 0 1 1 6.25 0v.375h3.5V15A2.625 2.625 0 0 1 14 17.625H6A2.625 2.625 0 0 1 3.375 15V5.375h3.5ZM4.625 15V6.625h10.75V15c0 .76-.616 1.375-1.375 1.375H6c-.76 0-1.375-.616-1.375-1.375Z" | ||
fillRule="evenodd" | ||
/> | ||
</Icon> | ||
); | ||
} |
58 changes: 58 additions & 0 deletions
58
templates/hydrogen-theme/app/components/layout/CartCount.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {Await, Link} from '@remix-run/react'; | ||
import {cx} from 'class-variance-authority'; | ||
import {Suspense, useMemo} from 'react'; | ||
|
||
import {useIsHydrated} from '~/hooks/useIsHydrated'; | ||
import {useLocalePath} from '~/hooks/useLocalePath'; | ||
import {useRootLoaderData} from '~/hooks/useRootLoaderData'; | ||
|
||
import {IconBag} from '../icons/IconBag'; | ||
|
||
export function CartCount() { | ||
const rootData = useRootLoaderData(); | ||
|
||
return ( | ||
<Suspense fallback={<Badge count={0} />}> | ||
<Await resolve={rootData?.cart}> | ||
{(cart) => <Badge count={cart?.totalQuantity || 0} />} | ||
</Await> | ||
</Suspense> | ||
); | ||
} | ||
|
||
function Badge(props: {count: number}) { | ||
const {count} = props; | ||
const isHydrated = useIsHydrated(); | ||
const path = useLocalePath({path: '/cart'}); | ||
|
||
const BadgeCounter = useMemo( | ||
() => ( | ||
<> | ||
<IconBag className="h-6 w-6" /> | ||
{isHydrated && count > 0 && ( | ||
<div | ||
className={cx([ | ||
'absolute right-[-8px] top-0 flex items-center justify-center', | ||
'inverted-color-scheme', | ||
'aspect-square h-auto min-w-[1.35rem] rounded-full p-1', | ||
'text-center text-[.7rem] leading-[0] subpixel-antialiased', | ||
])} | ||
> | ||
<span>{count}</span> | ||
</div> | ||
)} | ||
</> | ||
), | ||
[count, isHydrated], | ||
); | ||
|
||
return ( | ||
<Link | ||
className="focus:ring-primary/5 relative flex h-8 w-8 items-center justify-center" | ||
prefetch="intent" | ||
to={path} | ||
> | ||
{BadgeCounter} | ||
</Link> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {useEffect, useState} from 'react'; | ||
|
||
export function useIsHydrated() { | ||
const [isHydrated, setHydrated] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
setHydrated(true); | ||
}, []); | ||
|
||
return isHydrated; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {useLocale} from './useLocale'; | ||
|
||
export function useLocalePath(props: {path: string}) { | ||
const locale = useLocale(); | ||
const {path} = props; | ||
const pathPrefix = locale?.pathPrefix; | ||
|
||
if (pathPrefix) { | ||
return `${pathPrefix}${path}`; | ||
} | ||
|
||
return path; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.