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-grid.tsx 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { ChevronLeft, ChevronRight } from 'lucide-react';
  2. import type { ReactNode } from 'react';
  3. import { useRef } from 'react';
  4. import { cn } from '@/lib/utils';
  5. import { useStorefront } from '../context';
  6. import { ProductCard } from '../product-card';
  7. import { storefrontText } from '../text';
  8. import { formatPrice } from '../theme';
  9. import type { ProductGridSection } from '../types';
  10. import { Container, ImagePlaceholder, SectionHeading, SfButton } from '../ui';
  11. /** How many real products the home page shows before "View all". */
  12. const maxShown = 8;
  13. /** The large layout shows fewer, bigger products. */
  14. const maxLarge = 4;
  15. /**
  16. * Shows the merchant's real products when there are any.
  17. * Until then it shows the example items the AI wrote, so a new store never looks empty.
  18. *
  19. * Layouts: grid (4 across), minimal (centred), carousel (one row that scrolls sideways)
  20. * and large (2 big cards across, like a magazine).
  21. */
  22. export function ProductGrid({ section }: { section: ProductGridSection }) {
  23. const { heading, subheading } = section.content;
  24. const variant = section.variant;
  25. const centered = variant === 'minimal';
  26. const { products, language } = useStorefront();
  27. const t = storefrontText(language);
  28. const hasProducts = products.length > 0;
  29. const cards = useCards(section, centered);
  30. const shown = variant === 'large' ? cards.slice(0, maxLarge) : cards;
  31. const viewAll = hasProducts && (
  32. <SfButton href="/shop" variant="outline">
  33. {t.viewAll}
  34. </SfButton>
  35. );
  36. if (variant === 'carousel') {
  37. return <Carousel section={section} cards={shown} viewAll={viewAll} />;
  38. }
  39. return (
  40. <section
  41. id={section.id}
  42. className="sf-section scroll-mt-20 py-16 md:py-20"
  43. >
  44. <Container>
  45. <div
  46. className={
  47. centered
  48. ? ''
  49. : 'flex flex-wrap items-end justify-between gap-4'
  50. }
  51. >
  52. <SectionHeading
  53. heading={heading}
  54. subheading={subheading}
  55. align={centered ? 'center' : 'left'}
  56. />
  57. {!centered && viewAll}
  58. </div>
  59. <ul
  60. className={cn(
  61. 'mt-10 grid',
  62. variant === 'large'
  63. ? 'grid-cols-1 gap-x-8 gap-y-12 sm:grid-cols-2'
  64. : 'grid-cols-2 gap-x-5 gap-y-9 lg:grid-cols-4',
  65. )}
  66. >
  67. {shown.map((card) => (
  68. <li key={card.key}>{card.node}</li>
  69. ))}
  70. </ul>
  71. {centered && viewAll && (
  72. <div className="mt-10 text-center">{viewAll}</div>
  73. )}
  74. </Container>
  75. </section>
  76. );
  77. }
  78. /** One row of products that scrolls sideways, with arrows on wide screens. */
  79. function Carousel({
  80. section,
  81. cards,
  82. viewAll,
  83. }: {
  84. section: ProductGridSection;
  85. cards: { key: string; node: ReactNode }[];
  86. viewAll: ReactNode;
  87. }) {
  88. const { language } = useStorefront();
  89. const t = storefrontText(language);
  90. const row = useRef<HTMLUListElement>(null);
  91. function scroll(direction: 1 | -1) {
  92. const list = row.current;
  93. if (list) {
  94. list.scrollBy({
  95. left: direction * list.clientWidth * 0.8,
  96. behavior: window.matchMedia('(prefers-reduced-motion: reduce)')
  97. .matches
  98. ? 'auto'
  99. : 'smooth',
  100. });
  101. }
  102. }
  103. const arrow =
  104. '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';
  105. return (
  106. <section
  107. id={section.id}
  108. className="sf-section scroll-mt-20 py-16 md:py-20"
  109. data-layout="carousel"
  110. >
  111. <Container>
  112. <div className="flex flex-wrap items-end justify-between gap-4">
  113. <SectionHeading
  114. heading={section.content.heading}
  115. subheading={section.content.subheading}
  116. />
  117. <div className="flex items-center gap-2">
  118. {viewAll}
  119. <button
  120. type="button"
  121. onClick={() => scroll(-1)}
  122. aria-label={t.previousProducts}
  123. className={arrow}
  124. >
  125. <ChevronLeft
  126. className="size-5"
  127. aria-hidden="true"
  128. />
  129. </button>
  130. <button
  131. type="button"
  132. onClick={() => scroll(1)}
  133. aria-label={t.nextProducts}
  134. className={arrow}
  135. >
  136. <ChevronRight
  137. className="size-5"
  138. aria-hidden="true"
  139. />
  140. </button>
  141. </div>
  142. </div>
  143. <ul
  144. ref={row}
  145. 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"
  146. style={{ scrollbarWidth: 'none' }}
  147. >
  148. {cards.map((card) => (
  149. <li
  150. key={card.key}
  151. className="w-[70%] shrink-0 snap-start sm:w-[40%] lg:w-[calc(25%-15px)]"
  152. >
  153. {card.node}
  154. </li>
  155. ))}
  156. </ul>
  157. </Container>
  158. </section>
  159. );
  160. }
  161. /** The real products as cards, or the AI's example items until the merchant adds products. */
  162. function useCards(
  163. section: ProductGridSection,
  164. centered: boolean,
  165. ): { key: string; node: ReactNode }[] {
  166. const { products } = useStorefront();
  167. if (products.length > 0) {
  168. return products.slice(0, maxShown).map((product, index) => ({
  169. key: `p${product.id}`,
  170. node: (
  171. <ProductCard
  172. product={product}
  173. seed={index}
  174. centered={centered}
  175. />
  176. ),
  177. }));
  178. }
  179. return section.content.items.map((item, index) => ({
  180. key: `i${index}${item.name}`,
  181. node: (
  182. <>
  183. <div className="relative">
  184. <ImagePlaceholder
  185. seed={index}
  186. label={item.name}
  187. className="aspect-[4/5] w-full"
  188. />
  189. {item.badge && (
  190. <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)">
  191. {item.badge}
  192. </span>
  193. )}
  194. </div>
  195. <div
  196. className={
  197. centered
  198. ? 'mt-4 text-center'
  199. : 'mt-4 flex flex-col gap-1 sm:flex-row sm:items-baseline sm:justify-between sm:gap-3'
  200. }
  201. >
  202. <h3 className="font-medium">{item.name}</h3>
  203. <p className="text-sm text-(--sf-muted) tabular-nums">
  204. {formatPrice(item.price)}
  205. </p>
  206. </div>
  207. </>
  208. ),
  209. }));
  210. }