Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 26x 26x 26x 26x 26x 28x 28x 28x 26x 26x 26x 26x 26x 26x 26x 26x 28x 28x 28x 26x 26x 26x 28x 28x 28x 28x 28x 28x 28x 28x 28x 27x 27x 27x 27x 27x 27x 27x 27x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 3x 3x 4x 4x 4x 4x 4x 4x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x 1x 1x 1x 1x 2x 2x 2x | 'use client';
import { useState } from 'react';
import Image from 'next/image';
import Link from 'next/link';
import { Product } from '@/types/product';
import { IconButton, StarRating, PriceDisplay, Center, HStack, Button } from '@/components/ui';
import { useProductActions } from '@/hooks/useProductActions';
import { WishlistButton } from '@/components/features/product/WishlistButton';
import { cn } from '@/lib/core';
/**
* ProductCard Component Props
*/
export interface ProductCardProps {
/** Product data */
product: Product;
/** Card variant/layout */
variant?: 'grid' | 'list' | 'featured';
/** Show quick view button */
showQuickView?: boolean;
/** Show add to cart button */
showAddToCart?: boolean;
/** Show wishlist button */
showWishlist?: boolean;
/** Show rating */
showRating?: boolean;
/** Additional CSS classes */
className?: string;
}
/**
* ProductCard - Unified product card component
*
* Replaces:
* - ProductItem (grid)
* - SingleGridItem (grid)
* - SingleListItem (list)
* - BestSeller/SingleItem (featured)
*
* Features:
* - Three layout variants (grid, list, featured)
* - Uses new reusable components (IconButton, StarRating, PriceDisplay)
* - Uses useProductActions hook for consistent behavior
* - Built-in image error handling
* - Hover effects for action buttons
* - Responsive design
*
* @example
* ```tsx
* // Grid variant (default)
* <ProductCard product={product} />
*
* // List variant
* <ProductCard product={product} variant="list" />
*
* // Featured variant
* <ProductCard product={product} variant="featured" />
*
* // Custom action buttons
* <ProductCard
* product={product}
* showQuickView={true}
* showAddToCart={true}
* showWishlist={false}
* />
* ```
*/
export function ProductCard({
product,
variant = 'grid',
showQuickView = true,
showAddToCart = true,
showWishlist = true,
showRating = true,
className}: ProductCardProps) {
const [imageError, setImageError] = useState(false);
const { addToCart, showQuickView: openQuickView } = useProductActions(product);
const imageSrc = imageError
? '/images/placeholder.png'
: product.imgs?.previews[0] || '/images/placeholder.png';
// Grid variant (default)
if (variant === 'grid' || variant === 'featured') {
const productId = `product-${product.id}`;
const productUrl = `/product/${product.id}`;
return (
<article
className={cn('group product-card', className)}
aria-labelledby={`${productId}-title`}
>
{/* Image Container - Clickable link to product page */}
<Link href={productUrl} className="block">
<Center
className={cn(
'relative overflow-hidden rounded-lg aspect-square mb-4 cursor-pointer',
variant === 'grid' ? 'bg-white dark:bg-gray-800 shadow-1' : 'bg-[#F6F7FB] dark:bg-gray-800'
)}
>
<Image
src={imageSrc}
alt="" // Decorative - title is in heading
width={250}
height={250}
className="object-contain"
onError={() => setImageError(true)}
/>
{/* Action Buttons - Slide up on hover */}
<HStack
align="center"
justify="center"
gap={2.5}
className="absolute left-0 bottom-0 translate-y-full w-full pb-5 ease-linear duration-200 group-hover:translate-y-0"
onClick={(e) => e.preventDefault()}
>
{showQuickView && (
<IconButton
icon="eye"
onClick={(e) => { e.preventDefault(); e.stopPropagation(); openQuickView(); }}
aria-label={`Quick view ${product.title}`}
/>
)}
{showAddToCart && (
<Button
onClick={(e) => { e.preventDefault(); e.stopPropagation(); addToCart(); }}
variant="primary"
size="sm"
aria-label={`Add ${product.title} to cart`}
>
Add to cart
</Button>
)}
{showWishlist && (
<div onClick={(e) => { e.preventDefault(); e.stopPropagation(); }}>
<WishlistButton product={product} variant="icon" />
</div>
)}
</HStack>
</Center>
</Link>
{/* Product Info */}
<div>
{/* Rating */}
{showRating && (
<StarRating
rating={product.rating ?? 0}
reviewCount={product.reviews}
showCount={true}
size="sm"
productId={product.id}
className="mb-2"
/>
)}
{/* Title */}
<h3
id={`${productId}-title`}
className="font-medium text-dark dark:text-gray-100 ease-out duration-200 hover:text-blue mb-1.5"
>
<Link href={productUrl}>
{product.title}
</Link>
</h3>
{/* Price */}
<PriceDisplay
price={product.price}
discountedPrice={product.discountedPrice}
size="md"
/>
</div>
</article>
);
}
// List variant
if (variant === 'list') {
const productId = `product-list-${product.id}`;
const productUrl = `/product/${product.id}`;
return (
<article
className={cn('group rounded-lg bg-white dark:bg-gray-800 shadow-1 product-card', className)}
aria-labelledby={`${productId}-title`}
>
<HStack>
{/* Image Container - Clickable link to product page */}
<Link href={productUrl} className="block">
<Center className="shadow-list relative overflow-hidden max-w-[270px] w-full aspect-square p-4 cursor-pointer">
<Image
src={imageSrc}
alt="" // Decorative - title is in heading
width={250}
height={250}
className="object-contain"
onError={() => setImageError(true)}
/>
{/* Action Buttons - Slide up on hover */}
<HStack
align="center"
justify="center"
gap={2.5}
className="absolute left-0 bottom-0 translate-y-full w-full pb-5 ease-linear duration-200 group-hover:translate-y-0"
onClick={(e) => e.preventDefault()}
>
{showQuickView && (
<IconButton
icon="eye"
onClick={(e) => { e.preventDefault(); e.stopPropagation(); openQuickView(); }}
aria-label={`Quick view ${product.title}`}
/>
)}
{showAddToCart && (
<Button
onClick={(e) => { e.preventDefault(); e.stopPropagation(); addToCart(); }}
variant="primary"
size="sm"
className="text-custom-sm py-[7px] px-5 rounded-[5px]"
aria-label={`Add ${product.title} to cart`}
>
Add to cart
</Button>
)}
{showWishlist && (
<div onClick={(e) => { e.preventDefault(); e.stopPropagation(); }}>
<WishlistButton product={product} variant="icon" />
</div>
)}
</HStack>
</Center>
</Link>
{/* Product Info */}
<div className="w-full flex flex-col gap-5 sm:flex-row sm:items-center justify-center sm:justify-between py-5 px-4 sm:px-7.5 lg:pl-11 lg:pr-12">
<div>
{/* Title */}
<h3
id={`${productId}-title`}
className="font-medium text-dark dark:text-gray-100 ease-out duration-200 hover:text-blue mb-1.5"
>
<Link href={productUrl}>
{product.title}
</Link>
</h3>
{/* Price */}
<PriceDisplay
price={product.price}
discountedPrice={product.discountedPrice}
size="md"
/>
</div>
{/* Rating */}
{showRating && (
<StarRating
rating={product.rating ?? 0}
reviewCount={product.reviews}
showCount={true}
size="sm"
productId={product.id}
className="mb-2"
/>
)}
</div>
</HStack>
</article>
);
}
return null;
}
/**
* ProductCardGrid - Grid variant shorthand
*/
export function ProductCardGrid(props: Omit<ProductCardProps, 'variant'>) {
return <ProductCard {...props} variant="grid" />;
}
/**
* ProductCardList - List variant shorthand
*/
export function ProductCardList(props: Omit<ProductCardProps, 'variant'>) {
return <ProductCard {...props} variant="list" />;
}
/**
* ProductCardFeatured - Featured variant shorthand
*/
export function ProductCardFeatured(props: Omit<ProductCardProps, 'variant'>) {
return <ProductCard {...props} variant="featured" />;
}
|