-
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 #67 from buildheadless/cart
Add cart drawer
- Loading branch information
Showing
9 changed files
with
237 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import {Dialog, Transition} from '@headlessui/react'; | ||
import {cx} from 'class-variance-authority'; | ||
import {Fragment, useState} from 'react'; | ||
|
||
import {IconClose} from './icons/IconClose'; | ||
|
||
/** | ||
* Drawer component that opens on user click. | ||
* @param heading - string. Shown at the top of the drawer. | ||
* @param open - boolean state. if true opens the drawer. | ||
* @param onClose - function should set the open state. | ||
* @param openFrom - right, left | ||
* @param children - react children node. | ||
*/ | ||
export function Drawer({ | ||
children, | ||
heading, | ||
onClose, | ||
open, | ||
openFrom = 'right', | ||
}: { | ||
children: React.ReactNode; | ||
heading?: string; | ||
onClose: () => void; | ||
open: boolean; | ||
openFrom: 'left' | 'right'; | ||
}) { | ||
const offScreen = { | ||
left: '-translate-x-full', | ||
right: 'translate-x-full', | ||
}; | ||
|
||
return ( | ||
<Transition appear as={Fragment} show={open}> | ||
<Dialog as="div" className="relative z-50" onClose={onClose}> | ||
<Transition.Child | ||
as={Fragment} | ||
enter="ease-out duration-300" | ||
enterFrom="opacity-0 left-0" | ||
enterTo="opacity-100" | ||
leave="ease-in duration-200" | ||
leaveFrom="opacity-100" | ||
leaveTo="opacity-0" | ||
> | ||
<div className="fixed inset-0 bg-slate-400/50 backdrop-blur-sm transition-opacity" /> | ||
</Transition.Child> | ||
|
||
<div className="fixed inset-0 overflow-hidden"> | ||
<div className="absolute inset-0 overflow-hidden"> | ||
<div | ||
className={cx([ | ||
'fixed inset-y-0 flex max-w-full', | ||
openFrom === 'right' ? 'right-0' : '', | ||
])} | ||
> | ||
<Transition.Child | ||
as={Fragment} | ||
enter="transform transition ease-in-out duration-300" | ||
enterFrom={offScreen[openFrom]} | ||
enterTo="translate-x-0" | ||
leave="transform transition ease-in-out duration-300" | ||
leaveFrom="translate-x-0" | ||
leaveTo={offScreen[openFrom]} | ||
> | ||
<div className="max-h-screen w-screen max-w-lg bg-white"> | ||
<Dialog.Panel className="flex max-h-screen min-h-full flex-col"> | ||
<header className="flex items-start justify-between p-5 shadow-sm"> | ||
{heading !== null && ( | ||
<Dialog.Title className="text-lg"> | ||
{heading} | ||
</Dialog.Title> | ||
)} | ||
<button | ||
className="-m-4 p-4 text-primary transition hover:text-primary/50" | ||
data-test="close-cart" | ||
onClick={onClose} | ||
type="button" | ||
> | ||
<IconClose aria-label="Close panel" /> | ||
</button> | ||
</header> | ||
{children} | ||
</Dialog.Panel> | ||
</div> | ||
</Transition.Child> | ||
</div> | ||
</div> | ||
</div> | ||
</Dialog> | ||
</Transition> | ||
); | ||
} | ||
|
||
/* Use for associating arialabelledby with the title*/ | ||
Drawer.Title = Dialog.Title; | ||
|
||
export function useDrawer(openDefault = false) { | ||
const [isOpen, setIsOpen] = useState(openDefault); | ||
|
||
function openDrawer() { | ||
setIsOpen(true); | ||
} | ||
|
||
function closeDrawer() { | ||
setIsOpen(false); | ||
} | ||
|
||
return { | ||
closeDrawer, | ||
isOpen, | ||
openDrawer, | ||
}; | ||
} |
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
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
25 changes: 25 additions & 0 deletions
25
templates/hydrogen-theme/app/components/icons/IconClose.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,25 @@ | ||
import type {IconProps} from './Icon'; | ||
|
||
import {Icon} from './Icon'; | ||
|
||
export function IconClose(props: IconProps) { | ||
return ( | ||
<Icon {...props} stroke={props.stroke || 'currentColor'}> | ||
<title>Close</title> | ||
<line | ||
strokeWidth="1.25" | ||
x1="4.44194" | ||
x2="15.7556" | ||
y1="4.30806" | ||
y2="15.6218" | ||
/> | ||
<line | ||
strokeWidth="1.25" | ||
transform="matrix(-0.707107 0.707107 0.707107 0.707107 16 4.75)" | ||
x2="16" | ||
y1="-0.625" | ||
y2="-0.625" | ||
/> | ||
</Icon> | ||
); | ||
} |
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,17 @@ | ||
import {useFetchers} from '@remix-run/react'; | ||
import {CartForm} from '@shopify/hydrogen'; | ||
|
||
export function useCartFetchers(actionName: string) { | ||
const fetchers = useFetchers(); | ||
const cartFetchers = []; | ||
|
||
for (const fetcher of fetchers) { | ||
if (fetcher.formData) { | ||
const formInputs = CartForm.getFormInput(fetcher.formData); | ||
if (formInputs.action === actionName) { | ||
cartFetchers.push(fetcher); | ||
} | ||
} | ||
} | ||
return cartFetchers; | ||
} |