| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- import { useStorefront } from '../context';
- import { ProductCard } from '../product-card';
- import { storefrontText } from '../text';
- import { formatPrice } from '../theme';
- import type { ProductGridSection } from '../types';
- import { Container, ImagePlaceholder, SectionHeading, SfButton } from '../ui';
-
- /** How many real products the home page shows before "View all". */
- const maxShown = 8;
-
- /**
- * Shows the merchant's real products when there are any.
- * Until then it shows the example items the AI wrote, so a new store never looks empty.
- */
- export function ProductGrid({ section }: { section: ProductGridSection }) {
- const { heading, subheading, items } = section.content;
- const minimal = section.variant === 'minimal';
- const { products, language } = useStorefront();
- const t = storefrontText(language);
- const hasProducts = products.length > 0;
-
- return (
- <section
- id={section.id}
- className="sf-section scroll-mt-20 py-16 md:py-20"
- >
- <Container>
- <div
- className={
- minimal
- ? ''
- : 'flex flex-wrap items-end justify-between gap-4'
- }
- >
- <SectionHeading
- heading={heading}
- subheading={subheading}
- align={minimal ? 'center' : 'left'}
- />
- {hasProducts && !minimal && (
- <SfButton href="/shop" variant="outline">
- {t.viewAll}
- </SfButton>
- )}
- </div>
-
- <ul className="mt-10 grid grid-cols-2 gap-x-5 gap-y-9 lg:grid-cols-4">
- {hasProducts
- ? products.slice(0, maxShown).map((product, index) => (
- <li key={product.id}>
- <ProductCard
- product={product}
- seed={index}
- centered={minimal}
- />
- </li>
- ))
- : items.map((item, index) => (
- <li key={item.name + index}>
- <div className="relative">
- <ImagePlaceholder
- seed={index}
- label={item.name}
- className="aspect-[4/5] w-full"
- />
- {item.badge && (
- <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)">
- {item.badge}
- </span>
- )}
- </div>
- <div
- className={
- minimal
- ? '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">
- {item.name}
- </h3>
- <p className="text-sm text-(--sf-muted) tabular-nums">
- {formatPrice(item.price)}
- </p>
- </div>
- </li>
- ))}
- </ul>
-
- {hasProducts && minimal && (
- <div className="mt-10 text-center">
- <SfButton href="/shop" variant="outline">
- {t.viewAll}
- </SfButton>
- </div>
- )}
- </Container>
- </section>
- );
- }
|