|
| 1 | +import { ArrowLeft, ArrowRight, VideoCamera, X } from "@phosphor-icons/react"; |
| 2 | +import * as Dialog from "@radix-ui/react-dialog"; |
| 3 | +import * as VisuallyHidden from "@radix-ui/react-visually-hidden"; |
| 4 | +import clsx from "clsx"; |
| 5 | +import type { |
| 6 | + MediaFragment, |
| 7 | + Media_MediaImage_Fragment, |
| 8 | + Media_Video_Fragment, |
| 9 | +} from "storefront-api.generated"; |
| 10 | +import { Button } from "~/components/button"; |
| 11 | +import { Image } from "~/components/image"; |
| 12 | +import { ScrollArea } from "~/components/scroll-area"; |
| 13 | +import { cn } from "~/utils/cn"; |
| 14 | +import { getImageAspectRatio } from "~/utils/image"; |
| 15 | + |
| 16 | +export function ZoomModal({ |
| 17 | + media, |
| 18 | + zoomMediaId, |
| 19 | + setZoomMediaId, |
| 20 | + open, |
| 21 | + onOpenChange, |
| 22 | +}: { |
| 23 | + media: MediaFragment[]; |
| 24 | + zoomMediaId: string; |
| 25 | + setZoomMediaId: (id: string) => void; |
| 26 | + open: boolean; |
| 27 | + onOpenChange: (open: boolean) => void; |
| 28 | +}) { |
| 29 | + let zoomMedia = media.find((med) => med.id === zoomMediaId); |
| 30 | + let zoomMediaIndex = media.findIndex((med) => med.id === zoomMediaId); |
| 31 | + let nextMedia = media[zoomMediaIndex + 1] ?? media[0]; |
| 32 | + let prevMedia = media[zoomMediaIndex - 1] ?? media[media.length - 1]; |
| 33 | + |
| 34 | + return ( |
| 35 | + <Dialog.Root open={open} onOpenChange={onOpenChange}> |
| 36 | + <Dialog.Portal> |
| 37 | + <Dialog.Overlay |
| 38 | + className="fixed inset-0 bg-white data-[state=open]:animate-fade-in z-10" |
| 39 | + style={{ "--fade-in-duration": "100ms" } as React.CSSProperties} |
| 40 | + /> |
| 41 | + <Dialog.Content |
| 42 | + className={clsx([ |
| 43 | + "fixed inset-0 w-screen z-10", |
| 44 | + "data-[state=open]:animate-slide-up", |
| 45 | + ])} |
| 46 | + style={ |
| 47 | + { |
| 48 | + "--slide-up-from": "20px", |
| 49 | + "--slide-up-duration": "300ms", |
| 50 | + } as React.CSSProperties |
| 51 | + } |
| 52 | + aria-describedby={undefined} |
| 53 | + > |
| 54 | + <div className="w-full h-full flex items-center justify-center bg-[--color-background] relative"> |
| 55 | + <VisuallyHidden.Root asChild> |
| 56 | + <Dialog.Title>Product media zoom</Dialog.Title> |
| 57 | + </VisuallyHidden.Root> |
| 58 | + <div className="hidden md:block absolute top-10 left-8"> |
| 59 | + <ScrollArea className="max-h-[700px]" size="sm"> |
| 60 | + <div className="w-24 pr-2 space-y-2"> |
| 61 | + {media.map(({ id, previewImage, alt, mediaContentType }) => { |
| 62 | + return ( |
| 63 | + <div |
| 64 | + key={id} |
| 65 | + className={cn( |
| 66 | + "relative", |
| 67 | + "p-1 border rounded-md transition-colors cursor-pointer border-transparent !h-auto", |
| 68 | + zoomMediaId === id && "border-line", |
| 69 | + )} |
| 70 | + onClick={() => setZoomMediaId(id)} |
| 71 | + > |
| 72 | + <Image |
| 73 | + data={{ |
| 74 | + ...previewImage, |
| 75 | + altText: alt || "Product image zoom", |
| 76 | + }} |
| 77 | + loading="lazy" |
| 78 | + width={200} |
| 79 | + aspectRatio="1/1" |
| 80 | + className="object-cover w-full h-auto rounded" |
| 81 | + sizes="auto" |
| 82 | + /> |
| 83 | + {mediaContentType === "VIDEO" && ( |
| 84 | + <div className="absolute bottom-2 right-2 bg-gray-900 text-white p-0.5"> |
| 85 | + <VideoCamera className="w-4 h-4" /> |
| 86 | + </div> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + ); |
| 90 | + })} |
| 91 | + </div> |
| 92 | + </ScrollArea> |
| 93 | + </div> |
| 94 | + <ZoomMedia media={zoomMedia} /> |
| 95 | + <Dialog.Close className="absolute top-4 right-4 z-1"> |
| 96 | + <X className="w-6 h-6" /> |
| 97 | + </Dialog.Close> |
| 98 | + <div className="flex items-center gap-2 justify-center absolute bottom-10 left-10 md:left-auto right-10"> |
| 99 | + <Button |
| 100 | + variant="secondary" |
| 101 | + className="rounded border-line-subtle" |
| 102 | + onClick={() => setZoomMediaId(prevMedia.id)} |
| 103 | + > |
| 104 | + <ArrowLeft className="w-4.5 h-4.5" /> |
| 105 | + </Button> |
| 106 | + <Button |
| 107 | + variant="secondary" |
| 108 | + className="rounded border-line-subtle" |
| 109 | + onClick={() => setZoomMediaId(nextMedia.id)} |
| 110 | + > |
| 111 | + <ArrowRight className="w-4.5 h-4.5" /> |
| 112 | + </Button> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + </Dialog.Content> |
| 116 | + </Dialog.Portal> |
| 117 | + </Dialog.Root> |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +function ZoomMedia({ media }: { media: MediaFragment }) { |
| 122 | + if (!media) return null; |
| 123 | + if (media.mediaContentType === "IMAGE") { |
| 124 | + let { image, alt } = media as Media_MediaImage_Fragment; |
| 125 | + return ( |
| 126 | + <Image |
| 127 | + data={{ ...image, altText: alt || "Product image zoom" }} |
| 128 | + loading="lazy" |
| 129 | + className="object-cover max-w-[95vw] w-auto h-auto md:h-full max-h-screen rounded" |
| 130 | + width={4096} |
| 131 | + aspectRatio={getImageAspectRatio(image, "adapt")} |
| 132 | + sizes="auto" |
| 133 | + /> |
| 134 | + ); |
| 135 | + } |
| 136 | + if (media.mediaContentType === "VIDEO") { |
| 137 | + let mediaVideo = media as Media_Video_Fragment; |
| 138 | + return ( |
| 139 | + <video controls className="h-auto md:h-full object-cover rounded"> |
| 140 | + <track kind="captions" /> |
| 141 | + <source src={mediaVideo.sources[0].url} type="video/mp4" /> |
| 142 | + </video> |
| 143 | + ); |
| 144 | + } |
| 145 | + return null; |
| 146 | +} |
0 commit comments