| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import { useStorefront } from './context';
- import { storefrontText } from './text';
- import { formatPrice } from './theme';
- import type { StoreProduct } from './types';
- import { ImagePlaceholder, SfLink } from './ui';
-
- /** A product photo, or the themed placeholder when the product has none. */
- export function ProductImage({
- src,
- alt,
- seed = 0,
- className,
- }: {
- src: string | null;
- alt: string;
- seed?: number;
- className?: string;
- }) {
- if (!src) {
- return (
- <ImagePlaceholder seed={seed} label={alt} className={className} />
- );
- }
-
- return (
- <img
- src={src}
- alt={alt}
- loading="lazy"
- className={`rounded-(--sf-radius-card) border border-(--sf-border) object-cover ${className ?? ''}`}
- />
- );
- }
-
- export function Price({ product }: { product: StoreProduct }) {
- const { language } = useStorefront();
- const t = storefrontText(language);
-
- return (
- <p className="text-sm tabular-nums">
- <span className="text-(--sf-muted)">
- {product.price_varies
- ? t.from(formatPrice(product.price))
- : formatPrice(product.price)}
- </span>
- {!product.price_varies && product.compare_at_price !== null && (
- <s className="ml-2 text-(--sf-muted) opacity-70">
- {formatPrice(product.compare_at_price)}
- </s>
- )}
- </p>
- );
- }
-
- /** One real product in a grid. The whole card links to the product's page. */
- export function ProductCard({
- product,
- seed = 0,
- centered = false,
- }: {
- product: StoreProduct;
- seed?: number;
- centered?: boolean;
- }) {
- const { language } = useStorefront();
- const t = storefrontText(language);
- // A second photo, shown when the shopper points at the card.
- const second = product.images[1] ?? null;
-
- return (
- <SfLink href={`/products/${product.slug}`} className="group block">
- <div className="relative overflow-hidden rounded-(--sf-radius-card)">
- <ProductImage
- src={product.image}
- alt={product.name}
- seed={seed}
- className="aspect-[4/5] w-full transition-transform duration-500 motion-safe:group-hover:scale-[1.03]"
- />
- {second && (
- <img
- src={second}
- alt=""
- loading="lazy"
- data-test="second-photo"
- 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"
- />
- )}
- {!product.in_stock && (
- <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)">
- {t.soldOut}
- </span>
- )}
- </div>
- <div
- className={
- centered
- ? 'mt-4 text-center'
- : 'mt-4 flex flex-col gap-1 sm:flex-row sm:items-baseline sm:justify-between sm:gap-3'
- }
- >
- <h3 className="font-medium group-hover:underline">
- {product.name}
- </h3>
- <Price product={product} />
- </div>
- </SfLink>
- );
- }
|