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 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. return (
  63. <SfLink href={`/products/${product.slug}`} className="group block">
  64. <div className="relative">
  65. <ProductImage
  66. src={product.image}
  67. alt={product.name}
  68. seed={seed}
  69. className="aspect-[4/5] w-full transition-opacity group-hover:opacity-90"
  70. />
  71. {!product.in_stock && (
  72. <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)">
  73. {t.soldOut}
  74. </span>
  75. )}
  76. </div>
  77. <div
  78. className={
  79. centered
  80. ? 'mt-4 text-center'
  81. : 'mt-4 flex flex-col gap-1 sm:flex-row sm:items-baseline sm:justify-between sm:gap-3'
  82. }
  83. >
  84. <h3 className="font-medium group-hover:underline">
  85. {product.name}
  86. </h3>
  87. <Price product={product} />
  88. </div>
  89. </SfLink>
  90. );
  91. }