AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

product-card.tsx 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { useStorefront } from './context';
  2. import { storefrontText } from './text';
  3. import { formatPrice } from './theme';
  4. import type { StoreProduct } from './types';
  5. import { ImagePlaceholder, SfLink } from './ui';
  6. /** A product photo, or the themed placeholder when the product has none. */
  7. export function ProductImage({
  8. src,
  9. alt,
  10. seed = 0,
  11. className,
  12. }: {
  13. src: string | null;
  14. alt: string;
  15. seed?: number;
  16. className?: string;
  17. }) {
  18. if (!src) {
  19. return (
  20. <ImagePlaceholder seed={seed} label={alt} className={className} />
  21. );
  22. }
  23. return (
  24. <img
  25. src={src}
  26. alt={alt}
  27. loading="lazy"
  28. className={`rounded-(--sf-radius-card) border border-(--sf-border) object-cover ${className ?? ''}`}
  29. />
  30. );
  31. }
  32. export function Price({ product }: { product: StoreProduct }) {
  33. const { language } = useStorefront();
  34. const t = storefrontText(language);
  35. return (
  36. <p className="text-sm tabular-nums">
  37. <span className="text-(--sf-muted)">
  38. {product.price_varies
  39. ? t.from(formatPrice(product.price))
  40. : formatPrice(product.price)}
  41. </span>
  42. {!product.price_varies && product.compare_at_price !== null && (
  43. <s className="ml-2 text-(--sf-muted) opacity-70">
  44. {formatPrice(product.compare_at_price)}
  45. </s>
  46. )}
  47. </p>
  48. );
  49. }
  50. /** One real product in a grid. The whole card links to the product's page. */
  51. export function ProductCard({
  52. product,
  53. seed = 0,
  54. centered = false,
  55. }: {
  56. product: StoreProduct;
  57. seed?: number;
  58. centered?: boolean;
  59. }) {
  60. const { language } = useStorefront();
  61. const t = storefrontText(language);
  62. // A second photo, shown when the shopper points at the card.
  63. const second = product.images[1] ?? null;
  64. return (
  65. <SfLink href={`/products/${product.slug}`} className="group block">
  66. <div className="relative overflow-hidden rounded-(--sf-radius-card)">
  67. <ProductImage
  68. src={product.image}
  69. alt={product.name}
  70. seed={seed}
  71. className="aspect-[4/5] w-full transition-transform duration-500 motion-safe:group-hover:scale-[1.03]"
  72. />
  73. {second && (
  74. <img
  75. src={second}
  76. alt=""
  77. loading="lazy"
  78. data-test="second-photo"
  79. className="absolute inset-0 h-full w-full rounded-(--sf-radius-card) object-cover opacity-0 transition-opacity duration-300 group-hover:opacity-100 group-focus-visible:opacity-100"
  80. />
  81. )}
  82. {!product.in_stock && (
  83. <span className="absolute top-3 left-3 rounded-(--sf-radius) bg-(--sf-fg) px-2.5 py-1 text-xs font-semibold text-(--sf-bg)">
  84. {t.soldOut}
  85. </span>
  86. )}
  87. </div>
  88. <div
  89. className={
  90. centered
  91. ? 'mt-4 text-center'
  92. : 'mt-4 flex flex-col gap-1 sm:flex-row sm:items-baseline sm:justify-between sm:gap-3'
  93. }
  94. >
  95. <h3 className="font-medium group-hover:underline">
  96. {product.name}
  97. </h3>
  98. <Price product={product} />
  99. </div>
  100. </SfLink>
  101. );
  102. }