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.

bento.tsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { ArrowUpRight } from 'lucide-react';
  2. import { cn } from '@/lib/utils';
  3. import { useStorefront } from '../context';
  4. import type { BentoSection, BentoTile } from '../types';
  5. import { Container, SectionHeading, SfLink } from '../ui';
  6. /**
  7. * Tile sizes for the mosaic layout on wide screens (4 columns), by how many tiles there are.
  8. * The first tile is always the big one. On phones the big and wide tiles take the full width.
  9. */
  10. const mosaic: Record<number, string[]> = {
  11. 3: ['big', 'wide', 'wide'],
  12. 4: ['big', 'wide', 'small', 'small'],
  13. 5: ['big', 'small', 'small', 'small', 'small'],
  14. 6: ['big', 'small', 'small', 'small', 'small', 'full'],
  15. };
  16. const sizeClass: Record<string, string> = {
  17. big: 'col-span-2 row-span-2',
  18. wide: 'col-span-2',
  19. small: 'col-span-1',
  20. full: 'col-span-2 md:col-span-4',
  21. };
  22. const toneClass: Record<string, string> = {
  23. primary: 'bg-(--sf-primary) text-(--sf-primary-fg)',
  24. accent: 'bg-(--sf-accent) text-(--sf-fg)',
  25. dark: 'bg-(--sf-fg) text-(--sf-bg)',
  26. surface: 'bg-(--sf-surface) text-(--sf-fg)',
  27. };
  28. /** Tiles of different sizes for products, collections, offers and stories, like a bento box. */
  29. export function Bento({ section }: { section: BentoSection }) {
  30. const { heading, tiles } = section.content;
  31. const even = section.variant === 'even';
  32. const sizes = mosaic[tiles.length] ?? [];
  33. return (
  34. <section
  35. id={section.id}
  36. className="sf-section scroll-mt-20 py-16 md:py-20"
  37. data-layout={section.variant}
  38. >
  39. <Container>
  40. {heading && <SectionHeading heading={heading} />}
  41. <ul
  42. className={cn(
  43. 'grid gap-4',
  44. heading && 'mt-10',
  45. even
  46. ? 'grid-cols-1 sm:grid-cols-2 lg:grid-cols-3'
  47. : 'auto-rows-[180px] grid-cols-2 md:auto-rows-[220px] md:grid-cols-4',
  48. )}
  49. >
  50. {tiles.map((tile, index) => (
  51. <li
  52. key={tile.title + index}
  53. className={cn(
  54. !even && sizeClass[sizes[index] ?? 'small'],
  55. even && 'aspect-[4/3]',
  56. )}
  57. >
  58. <Tile tile={tile} big={!even && index === 0} />
  59. </li>
  60. ))}
  61. </ul>
  62. </Container>
  63. </section>
  64. );
  65. }
  66. function Tile({ tile, big }: { tile: BentoTile; big: boolean }) {
  67. const { heroImages } = useStorefront();
  68. const photo = tile.image ? heroImages.refs[tile.image] : undefined;
  69. return (
  70. <SfLink
  71. href={tile.href}
  72. className={cn(
  73. 'sf-card group relative flex h-full flex-col justify-end overflow-hidden rounded-(--sf-radius-card) border border-(--sf-border) p-5',
  74. photo
  75. ? 'text-white'
  76. : (toneClass[tile.tone ?? 'surface'] ?? toneClass.surface),
  77. )}
  78. >
  79. {photo && (
  80. <>
  81. <img
  82. src={photo}
  83. alt=""
  84. loading="lazy"
  85. className="absolute inset-0 -z-0 h-full w-full object-cover transition-transform duration-500 motion-safe:group-hover:scale-[1.04]"
  86. />
  87. <span className="absolute inset-0 bg-linear-to-t from-black/70 via-black/20 to-transparent" />
  88. </>
  89. )}
  90. <ArrowUpRight
  91. className="absolute top-4 right-4 size-5 opacity-70 transition-transform group-hover:translate-x-0.5 group-hover:-translate-y-0.5"
  92. aria-hidden="true"
  93. />
  94. <span className="relative">
  95. <span
  96. className={cn(
  97. 'sf-heading block font-semibold text-balance',
  98. big ? 'text-2xl md:text-3xl' : 'text-lg',
  99. )}
  100. >
  101. {tile.title}
  102. </span>
  103. {tile.text && (
  104. <span className="mt-1 block text-sm opacity-85">
  105. {tile.text}
  106. </span>
  107. )}
  108. </span>
  109. </SfLink>
  110. );
  111. }