| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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 }) {
- return (
- <p className="text-sm tabular-nums">
- <span className="text-(--sf-muted)">
- {formatPrice(product.price)}
- </span>
- {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);
-
- return (
- <SfLink href={`/products/${product.slug}`} className="group block">
- <div className="relative">
- <ProductImage
- src={product.image}
- alt={product.name}
- seed={seed}
- className="aspect-[4/5] w-full transition-opacity group-hover:opacity-90"
- />
- {!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>
- );
- }
|