| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- import { ChevronLeft, ChevronRight } from 'lucide-react';
- import type { ReactNode } from 'react';
- import { useRef } from 'react';
- import { cn } from '@/lib/utils';
- 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;
-
- /** The large layout shows fewer, bigger products. */
- const maxLarge = 4;
-
- /**
- * 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.
- *
- * Layouts: grid (4 across), minimal (centred), carousel (one row that scrolls sideways)
- * and large (2 big cards across, like a magazine).
- */
- export function ProductGrid({ section }: { section: ProductGridSection }) {
- const { heading, subheading } = section.content;
- const variant = section.variant;
- const centered = variant === 'minimal';
- const { products, language } = useStorefront();
- const t = storefrontText(language);
- const hasProducts = products.length > 0;
- const cards = useCards(section, centered);
- const shown = variant === 'large' ? cards.slice(0, maxLarge) : cards;
-
- const viewAll = hasProducts && (
- <SfButton href="/shop" variant="outline">
- {t.viewAll}
- </SfButton>
- );
-
- if (variant === 'carousel') {
- return <Carousel section={section} cards={shown} viewAll={viewAll} />;
- }
-
- return (
- <section
- id={section.id}
- className="sf-section scroll-mt-20 py-16 md:py-20"
- >
- <Container>
- <div
- className={
- centered
- ? ''
- : 'flex flex-wrap items-end justify-between gap-4'
- }
- >
- <SectionHeading
- heading={heading}
- subheading={subheading}
- align={centered ? 'center' : 'left'}
- />
- {!centered && viewAll}
- </div>
-
- <ul
- className={cn(
- 'mt-10 grid',
- variant === 'large'
- ? 'grid-cols-1 gap-x-8 gap-y-12 sm:grid-cols-2'
- : 'grid-cols-2 gap-x-5 gap-y-9 lg:grid-cols-4',
- )}
- >
- {shown.map((card) => (
- <li key={card.key}>{card.node}</li>
- ))}
- </ul>
-
- {centered && viewAll && (
- <div className="mt-10 text-center">{viewAll}</div>
- )}
- </Container>
- </section>
- );
- }
-
- /** One row of products that scrolls sideways, with arrows on wide screens. */
- function Carousel({
- section,
- cards,
- viewAll,
- }: {
- section: ProductGridSection;
- cards: { key: string; node: ReactNode }[];
- viewAll: ReactNode;
- }) {
- const { language } = useStorefront();
- const t = storefrontText(language);
- const row = useRef<HTMLUListElement>(null);
-
- function scroll(direction: 1 | -1) {
- const list = row.current;
-
- if (list) {
- list.scrollBy({
- left: direction * list.clientWidth * 0.8,
- behavior: window.matchMedia('(prefers-reduced-motion: reduce)')
- .matches
- ? 'auto'
- : 'smooth',
- });
- }
- }
-
- const arrow =
- 'hidden size-10 items-center justify-center rounded-full border border-(--sf-border) text-(--sf-fg) transition-colors hover:bg-(--sf-surface) md:inline-flex';
-
- return (
- <section
- id={section.id}
- className="sf-section scroll-mt-20 py-16 md:py-20"
- data-layout="carousel"
- >
- <Container>
- <div className="flex flex-wrap items-end justify-between gap-4">
- <SectionHeading
- heading={section.content.heading}
- subheading={section.content.subheading}
- />
- <div className="flex items-center gap-2">
- {viewAll}
- <button
- type="button"
- onClick={() => scroll(-1)}
- aria-label={t.previousProducts}
- className={arrow}
- >
- <ChevronLeft
- className="size-5"
- aria-hidden="true"
- />
- </button>
- <button
- type="button"
- onClick={() => scroll(1)}
- aria-label={t.nextProducts}
- className={arrow}
- >
- <ChevronRight
- className="size-5"
- aria-hidden="true"
- />
- </button>
- </div>
- </div>
-
- <ul
- ref={row}
- className="-mx-5 mt-10 flex snap-x snap-mandatory scroll-px-5 gap-5 overflow-x-auto px-5 pb-4 sm:-mx-8 sm:scroll-px-8 sm:px-8 [&::-webkit-scrollbar]:hidden"
- style={{ scrollbarWidth: 'none' }}
- >
- {cards.map((card) => (
- <li
- key={card.key}
- className="w-[70%] shrink-0 snap-start sm:w-[40%] lg:w-[calc(25%-15px)]"
- >
- {card.node}
- </li>
- ))}
- </ul>
- </Container>
- </section>
- );
- }
-
- /** The real products as cards, or the AI's example items until the merchant adds products. */
- function useCards(
- section: ProductGridSection,
- centered: boolean,
- ): { key: string; node: ReactNode }[] {
- const { products } = useStorefront();
-
- if (products.length > 0) {
- return products.slice(0, maxShown).map((product, index) => ({
- key: `p${product.id}`,
- node: (
- <ProductCard
- product={product}
- seed={index}
- centered={centered}
- />
- ),
- }));
- }
-
- return section.content.items.map((item, index) => ({
- key: `i${index}${item.name}`,
- node: (
- <>
- <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={
- 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">{item.name}</h3>
- <p className="text-sm text-(--sf-muted) tabular-nums">
- {formatPrice(item.price)}
- </p>
- </div>
- </>
- ),
- }));
- }
|