{"slug":"infinite-masonry","name":"Infinite Masonry","description":"Responsive virtualized masonry that measures variable-height cards and loads more data as the user nears the end.","category":"blocks","source_url":"https://beui.dev/r/infinite-masonry/raw","detail_url":"https://beui.dev/r/infinite-masonry","raw_url":"https://beui.dev/r/infinite-masonry/raw","page_url":"https://beui.dev/components/blocks/infinite-masonry","markdown_url":"https://beui.dev/components/blocks/infinite-masonry.md","published_at":"2026-07-15","updated_at":"2026-11-10","dependencies":["@tanstack/react-virtual","clsx","lucide-react","motion","react","tailwind-merge"],"internal":["@/components/motion/infinite-masonry","@/lib/ease","@/lib/i18n","@/lib/utils"],"install_slugs":["infinite-masonry"],"requires_theme":false,"props":[{"displayName":"InfiniteMasonry","props":[{"name":"items","type":"readonly T[]","required":true,"defaultValue":null,"description":"The items loaded so far, in feed order."},{"name":"getItemKey","type":"(item: T, index: number) => InfiniteMasonryKey","required":true,"defaultValue":null,"description":"Returns a stable unique key for an item."},{"name":"renderItem","type":"(item: T, index: number) => ReactNode","required":true,"defaultValue":null,"description":"Renders one item's card."},{"name":"onLoadMore","type":"() => void | Promise<void>","required":true,"defaultValue":null,"description":"Called when the scroll nears the end and more items should load."},{"name":"hasMore","type":"boolean","required":true,"defaultValue":null,"description":"Whether more items can load. False shows `endState` after the last item."},{"name":"loading","type":"boolean","required":false,"defaultValue":"false","description":"Shows loading placeholders at the end while more items load. Default: false"},{"name":"error","type":"ReactNode","required":false,"defaultValue":null,"description":"Error shown in a card at the end. Pauses loading while set."},{"name":"onRetry","type":"(() => void)","required":false,"defaultValue":null,"description":"Called from the error card's retry button. The button shows only when set."},{"name":"estimateSize","type":"((item: T, index: number) => number)","required":false,"defaultValue":"() => 240","description":"Estimated item height in px before it is measured. Default: 240"},{"name":"renderLoadingItem","type":"((index: number) => ReactNode)","required":false,"defaultValue":"(index) => <DefaultLoadingItem index={index} />","description":"Renders a loading placeholder card while more items load."},{"name":"emptyState","type":"ReactNode","required":false,"defaultValue":null,"description":"Shown when there are no items and nothing more to load."},{"name":"endState","type":"ReactNode","required":false,"defaultValue":null,"description":"Shown below the last item once `hasMore` is false."},{"name":"minColumnWidth","type":"number","required":false,"defaultValue":"208","description":"Smallest column width in px before a column is dropped. Default: 208"},{"name":"maxColumns","type":"number","required":false,"defaultValue":"4","description":"Most columns to show. Default: 4"},{"name":"gap","type":"number","required":false,"defaultValue":"12","description":"Space between items in px. Default: 12"},{"name":"overscan","type":"number","required":false,"defaultValue":"4","description":"Rows to render past the viewport, per column. Default: 4"},{"name":"prefetch","type":"number","required":false,"defaultValue":"3","description":"Starts loading when this many items from the end are visible. Default: 3"},{"name":"animateItems","type":"boolean","required":false,"defaultValue":"true","description":"Fades and slides in items added after the first render. Default: true"},{"name":"ariaLabel","type":"string","required":false,"defaultValue":null,"description":"Accessible name for the feed. Default: \"Infinite masonry feed\""},{"name":"className","type":"string","required":false,"defaultValue":null,"description":"Classes merged onto the scroll container."},{"name":"contentClassName","type":"string","required":false,"defaultValue":null,"description":"Classes merged onto the inner content element that holds the items."},{"name":"itemClassName","type":"string","required":false,"defaultValue":null,"description":"Classes merged onto each item wrapper, not loading or error cards."},{"name":"labels","type":"Partial<{ label: string; emptyTitle: string; emptyDescription: string; loadError: string; retry: string; loadingMore: string; }>","required":false,"defaultValue":null,"description":"Override any built-in string."}]}],"types":[],"files":[{"path":"components/motion/infinite-masonry.tsx","type":"component","content":"\"use client\";\n// beui.dev/components/blocks/infinite-masonry\n\nimport { useVirtualizer } from \"@tanstack/react-virtual\";\nimport { AlertCircle, Inbox } from \"lucide-react\";\nimport { motion, useReducedMotion } from \"motion/react\";\nimport {\n  useEffect,\n  useRef,\n  useState,\n  type MutableRefObject,\n  type ReactNode,\n} from \"react\";\nimport { EASE_OUT, SPRING_PANEL } from \"@/lib/ease\";\nimport { useMessages } from \"@/lib/i18n\";\nimport { cn } from \"@/lib/utils\";\n\n/** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */\nexport const infiniteMasonryMessages = {\n  label: \"Infinite masonry feed\",\n  emptyTitle: \"No items yet\",\n  emptyDescription: \"New items will appear here when they become available.\",\n  loadError: \"Couldn't load more\",\n  retry: \"Try again\",\n  loadingMore: \"Loading more items\",\n};\n\nexport type InfiniteMasonryMessages = typeof infiniteMasonryMessages;\n\nexport type InfiniteMasonryKey = string | number | bigint;\n\nexport interface InfiniteMasonryProps<T> {\n  /** The items loaded so far, in feed order. */\n  items: readonly T[];\n  /** Returns a stable unique key for an item. */\n  getItemKey: (item: T, index: number) => InfiniteMasonryKey;\n  /** Renders one item's card. */\n  renderItem: (item: T, index: number) => ReactNode;\n  /** Called when the scroll nears the end and more items should load. */\n  onLoadMore: () => void | Promise<void>;\n  /** Whether more items can load. False shows `endState` after the last item. */\n  hasMore: boolean;\n  /** Shows loading placeholders at the end while more items load. Default: false */\n  loading?: boolean;\n  /** Error shown in a card at the end. Pauses loading while set. */\n  error?: ReactNode;\n  /** Called from the error card's retry button. The button shows only when set. */\n  onRetry?: () => void;\n  /** Estimated item height in px before it is measured. Default: 240 */\n  estimateSize?: (item: T, index: number) => number;\n  /** Renders a loading placeholder card while more items load. */\n  renderLoadingItem?: (index: number) => ReactNode;\n  /** Shown when there are no items and nothing more to load. */\n  emptyState?: ReactNode;\n  /** Shown below the last item once `hasMore` is false. */\n  endState?: ReactNode;\n  /** Smallest column width in px before a column is dropped. Default: 208 */\n  minColumnWidth?: number;\n  /** Most columns to show. Default: 4 */\n  maxColumns?: number;\n  /** Space between items in px. Default: 12 */\n  gap?: number;\n  /** Rows to render past the viewport, per column. Default: 4 */\n  overscan?: number;\n  /** Starts loading when this many items from the end are visible. Default: 3 */\n  prefetch?: number;\n  /** Fades and slides in items added after the first render. Default: true */\n  animateItems?: boolean;\n  /** Accessible name for the feed. Default: \"Infinite masonry feed\" */\n  ariaLabel?: string;\n  /** Classes merged onto the scroll container. */\n  className?: string;\n  /** Classes merged onto the inner content element that holds the items. */\n  contentClassName?: string;\n  /** Classes merged onto each item wrapper, not loading or error cards. */\n  itemClassName?: string;\n  /** Override any built-in string. */\n  labels?: Partial<InfiniteMasonryMessages>;\n}\n\ntype MasonryMetrics = {\n  columns: number;\n  width: number;\n};\n\nfunction useMasonryMetrics({\n  elementRef,\n  gap,\n  maxColumns,\n  minColumnWidth,\n}: {\n  elementRef: React.RefObject<HTMLDivElement | null>;\n  gap: number;\n  maxColumns: number;\n  minColumnWidth: number;\n}) {\n  const [metrics, setMetrics] = useState<MasonryMetrics>({\n    columns: 1,\n    width: 0,\n  });\n\n  useEffect(() => {\n    const element = elementRef.current;\n    if (!element) return;\n\n    const update = (width: number) => {\n      const columns = Math.min(\n        maxColumns,\n        Math.max(1, Math.floor((width + gap) / (minColumnWidth + gap))),\n      );\n\n      setMetrics((current) =>\n        current.columns === columns && current.width === width\n          ? current\n          : { columns, width },\n      );\n    };\n\n    update(element.getBoundingClientRect().width);\n\n    const observer = new ResizeObserver(([entry]) => {\n      update(entry.contentRect.width);\n    });\n    observer.observe(element);\n\n    return () => observer.disconnect();\n  }, [elementRef, gap, maxColumns, minColumnWidth]);\n\n  return metrics;\n}\n\nfunction DefaultLoadingItem({ index }: { index: number }) {\n  return (\n    <div\n      aria-hidden=\"true\"\n      className=\"animate-pulse rounded-2xl border border-border bg-card p-3\"\n      style={{ minHeight: 144 + (index % 3) * 36 }}\n    >\n      <div className=\"h-3 w-2/3 rounded-full bg-muted\" />\n      <div className=\"mt-3 h-2 w-full rounded-full bg-muted\" />\n      <div className=\"mt-2 h-2 w-4/5 rounded-full bg-muted\" />\n    </div>\n  );\n}\n\nfunction DefaultEmptyState({\n  title,\n  description,\n}: {\n  title: string;\n  description: string;\n}) {\n  return (\n    <div className=\"flex h-full min-h-64 flex-col items-center justify-center px-6 text-center\">\n      <Inbox className=\"size-8 text-muted-foreground\" aria-hidden=\"true\" />\n      <p className=\"mt-3 text-sm font-medium text-foreground\">{title}</p>\n      <p className=\"mt-1 max-w-xs text-xs leading-5 text-muted-foreground\">\n        {description}\n      </p>\n    </div>\n  );\n}\n\nfunction MasonryItemReveal({\n  itemKey,\n  lane,\n  revealedKeys,\n  animate,\n  children,\n}: {\n  itemKey: InfiniteMasonryKey;\n  lane: number;\n  revealedKeys: MutableRefObject<Set<InfiniteMasonryKey>>;\n  animate: boolean;\n  children: ReactNode;\n}) {\n  const [shouldReveal] = useState(\n    () => animate && !revealedKeys.current.has(itemKey),\n  );\n\n  useEffect(() => {\n    revealedKeys.current.add(itemKey);\n  }, [itemKey, revealedKeys]);\n\n  const delay = Math.min(lane, 3) * 0.04;\n\n  return (\n    <motion.div\n      initial={shouldReveal ? { opacity: 0, y: 12 } : false}\n      animate={{ opacity: 1, y: 0 }}\n      transition={{\n        y: { ...SPRING_PANEL, delay },\n        opacity: { duration: 0.2, ease: EASE_OUT, delay },\n      }}\n    >\n      {children}\n    </motion.div>\n  );\n}\n\nexport function InfiniteMasonry<T>({\n  items,\n  getItemKey,\n  renderItem,\n  onLoadMore,\n  hasMore,\n  loading = false,\n  error,\n  onRetry,\n  estimateSize = () => 240,\n  renderLoadingItem = (index) => <DefaultLoadingItem index={index} />,\n  emptyState,\n  endState,\n  minColumnWidth = 208,\n  maxColumns = 4,\n  gap = 12,\n  overscan = 4,\n  prefetch = 3,\n  animateItems = true,\n  ariaLabel,\n  className,\n  contentClassName,\n  itemClassName,\n  labels,\n}: InfiniteMasonryProps<T>) {\n  const t = useMessages(\"infiniteMasonry\", infiniteMasonryMessages, labels);\n  const label = ariaLabel ?? t(\"label\");\n  const reduceMotion = useReducedMotion();\n  const scrollRef = useRef<HTMLDivElement>(null);\n  const contentRef = useRef<HTMLDivElement>(null);\n  const loadMoreRef = useRef(onLoadMore);\n  const loadPendingRef = useRef(false);\n  const initialItemCountRef = useRef(items.length);\n  const revealedKeysRef = useRef(new Set<InfiniteMasonryKey>());\n  const { columns, width } = useMasonryMetrics({\n    elementRef: contentRef,\n    gap,\n    maxColumns,\n    minColumnWidth,\n  });\n\n  useEffect(() => {\n    loadMoreRef.current = onLoadMore;\n  }, [onLoadMore]);\n\n  useEffect(() => {\n    if (!loading) loadPendingRef.current = false;\n  }, [loading]);\n\n  const hasError = error !== undefined && error !== null;\n  const tailCount = hasError ? 1 : loading ? columns : 0;\n  const virtualizer = useVirtualizer({\n    count: items.length + tailCount,\n    getScrollElement: () => scrollRef.current,\n    getItemKey: (index) =>\n      index < items.length\n        ? getItemKey(items[index], index)\n        : `masonry-tail-${index - items.length}`,\n    estimateSize: (index) =>\n      index < items.length\n        ? estimateSize(items[index], index)\n        : 144 + ((index - items.length) % 3) * 36,\n    gap,\n    lanes: columns,\n    overscan: overscan * columns,\n  });\n\n  const virtualItems = virtualizer.getVirtualItems();\n  const viewportEnd =\n    (virtualizer.scrollOffset ?? 0) + (virtualizer.scrollRect?.height ?? 0);\n  const lastVisibleIndex = virtualItems.reduce(\n    (lastIndex, item) =>\n      item.start < viewportEnd ? Math.max(lastIndex, item.index) : lastIndex,\n    -1,\n  );\n\n  useEffect(() => {\n    if (\n      hasError ||\n      loading ||\n      !hasMore ||\n      loadPendingRef.current ||\n      lastVisibleIndex < Math.max(0, items.length - prefetch)\n    ) {\n      return;\n    }\n\n    loadPendingRef.current = true;\n    void Promise.resolve(loadMoreRef.current()).finally(() => {\n      loadPendingRef.current = false;\n    });\n  }, [hasError, hasMore, items.length, lastVisibleIndex, loading, prefetch]);\n\n  if (items.length === 0 && !hasMore && !loading && !hasError) {\n    return (\n      <section\n        aria-label={label}\n        className={cn(\n          \"w-full overflow-hidden rounded-3xl border border-border bg-background\",\n          className,\n        )}\n      >\n        {emptyState ?? (\n          <DefaultEmptyState\n            title={t(\"emptyTitle\")}\n            description={t(\"emptyDescription\")}\n          />\n        )}\n      </section>\n    );\n  }\n\n  const columnWidth =\n    columns > 0 ? Math.max(0, (width - gap * (columns - 1)) / columns) : 0;\n\n  return (\n    <section\n      ref={scrollRef}\n      aria-label={label}\n      aria-busy={loading}\n      className={cn(\n        \"w-full contain-[layout_paint] overflow-y-auto overscroll-none rounded-3xl border border-border bg-background p-3 [overflow-anchor:none] [scrollbar-gutter:stable]\",\n        className,\n      )}\n    >\n      <div\n        ref={contentRef}\n        className={cn(\"relative w-full\", contentClassName)}\n        style={{ height: virtualizer.getTotalSize() }}\n      >\n        {virtualItems.map((virtualItem) => {\n          const isTail = virtualItem.index >= items.length;\n          const tailIndex = virtualItem.index - items.length;\n\n          return (\n            <div\n              key={virtualItem.key}\n              ref={virtualizer.measureElement}\n              data-index={virtualItem.index}\n              className={cn(\n                \"absolute left-0 top-0 will-change-transform\",\n                !isTail && itemClassName,\n              )}\n              style={{\n                width: columnWidth,\n                transform: `translate3d(${virtualItem.lane * (columnWidth + gap)}px, ${virtualItem.start}px, 0)`,\n              }}\n            >\n              {isTail ? (\n                hasError ? (\n                  <div className=\"flex min-h-36 flex-col items-start justify-center rounded-2xl border border-destructive/20 bg-destructive/5 p-4\">\n                    <div className=\"flex items-center gap-2 text-destructive\">\n                      <AlertCircle className=\"size-4\" aria-hidden=\"true\" />\n                      <p className=\"text-sm font-medium\">{t(\"loadError\")}</p>\n                    </div>\n                    <div className=\"mt-2 text-xs leading-5 text-muted-foreground\">\n                      {error}\n                    </div>\n                    {onRetry ? (\n                      <button\n                        type=\"button\"\n                        onClick={onRetry}\n                        className=\"mt-3 min-h-10 rounded-full border border-border bg-background px-4 text-xs font-medium text-foreground outline-none transition-colors hover:bg-muted focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2\"\n                      >\n                        {t(\"retry\")}\n                      </button>\n                    ) : null}\n                  </div>\n                ) : (\n                  renderLoadingItem(tailIndex)\n                )\n              ) : (\n                <MasonryItemReveal\n                  itemKey={virtualItem.key}\n                  lane={virtualItem.lane}\n                  revealedKeys={revealedKeysRef}\n                  animate={\n                    animateItems &&\n                    !reduceMotion &&\n                    virtualItem.index >= initialItemCountRef.current\n                  }\n                >\n                  {renderItem(items[virtualItem.index], virtualItem.index)}\n                </MasonryItemReveal>\n              )}\n            </div>\n          );\n        })}\n      </div>\n\n      {!hasMore && items.length > 0 && endState ? (\n        <div className=\"py-4 text-center text-xs text-muted-foreground\">\n          {endState}\n        </div>\n      ) : null}\n      <span className=\"sr-only\" aria-live=\"polite\">\n        {loading ? t(\"loadingMore\") : null}\n      </span>\n    </section>\n  );\n}\n"},{"path":"lib/ease.ts","type":"util","content":"// Shared motion tokens. Easing curves mirror the CSS custom properties in\n// globals.css; springs are the canonical physics used across components.\n// Strong custom variants - defaults like `ease-in`/`ease-out` feel weak.\n\nexport const EASE_OUT = [0.16, 1, 0.3, 1] as const;\n/** Accelerating curve for things that fall (a dropped ball, a toppled block). Not for UI entrances. */\nexport const EASE_IN = [0.55, 0, 1, 0.45] as const;\nexport const EASE_IN_OUT = [0.77, 0, 0.175, 1] as const;\nexport const EASE_DRAWER = [0.32, 0.72, 0, 1] as const;\n\n/** CSS string form of EASE_OUT for inline style transitions. */\nexport const EASE_OUT_CSS = \"cubic-bezier(0.16, 1, 0.3, 1)\";\n\n/** Press feedback on buttons and other tappable surfaces. */\nexport const SPRING_PRESS = {\n  type: \"spring\",\n  stiffness: 500,\n  damping: 30,\n  mass: 0.6,\n} as const;\n\n/** Content swaps - label/icon slots trading places inside a control. */\nexport const SPRING_SWAP = {\n  type: \"spring\",\n  stiffness: 460,\n  damping: 30,\n  mass: 0.55,\n} as const;\n\n/** Overlay panel entrances - modals and sheets summoned by pointer. */\nexport const SPRING_PANEL = {\n  type: \"spring\",\n  stiffness: 420,\n  damping: 40,\n  mass: 0.5,\n} as const;\n\n/** Shared-layout glides - pills, indicators and panels morphing between positions. */\nexport const SPRING_LAYOUT = {\n  type: \"spring\",\n  stiffness: 360,\n  damping: 32,\n  mass: 0.6,\n} as const;\n\n/** Cursor-follow physics for decorative mouse tracking (magnetic, tilt, dock). */\nexport const SPRING_MOUSE = {\n  stiffness: 200,\n  damping: 15,\n  mass: 0.3,\n} as const;\n\n/** Dragged handles and fills (sliders) - critically damped `useSpring` config,\n * so the value follows the pointer butterily and never rebounds off an end. */\nexport const SPRING_GLIDE = {\n  stiffness: 700,\n  damping: 50,\n  mass: 0.5,\n} as const;\n\n/** Shared-surface morphs: a trigger growing into its panel and back on one\n * weighted spring, so opening and closing travel the same path. */\nexport const SPRING_MORPH = {\n  type: \"spring\",\n  duration: 0.5,\n  bounce: 0.22,\n} as const;\n"},{"path":"lib/i18n.tsx","type":"util","content":"\"use client\";\n\nimport {\n  createContext,\n  type ReactNode,\n  useCallback,\n  useContext,\n  useMemo,\n} from \"react\";\n\n/**\n * A component's messages. Plain strings with `{name}` placeholders, so a\n * dictionary stays JSON-serializable and can be passed from a server layout.\n * There are no plural forms: write messages that read correctly for any count.\n */\nexport type Messages = Record<string, string>;\n\n/** A language file: component namespace → any subset of that component's messages. */\nexport type LocaleMessages = Record<string, Partial<Messages>>;\n\ntype LocaleContextValue = {\n  locale?: string;\n  messages: LocaleMessages;\n};\n\nconst NO_MESSAGES: LocaleMessages = {};\n\nconst LocaleContext = createContext<LocaleContextValue>({ messages: NO_MESSAGES });\n\n/** Without a locale, digits stay plain so server and client render the same text. */\nexport function formatNumber(value: number, locale?: string) {\n  return locale ? new Intl.NumberFormat(locale).format(value) : String(value);\n}\n\nexport interface LocaleProviderProps {\n  /** BCP 47 tag, e.g. `de`, `pt-BR`. Drives `Intl` number formatting. */\n  locale: string;\n  /**\n   * Component namespace → translated strings. Missing keys fall back to English.\n   * Pass a stable object (a module constant, imported JSON or a memoized value):\n   * a new object each render gives every translated component a new `t`.\n   */\n  messages?: LocaleMessages;\n  children: ReactNode;\n}\n\nexport function LocaleProvider({ locale, messages = NO_MESSAGES, children }: LocaleProviderProps) {\n  const value = useMemo(() => ({ locale, messages }), [locale, messages]);\n  return <LocaleContext.Provider value={value}>{children}</LocaleContext.Provider>;\n}\n\nexport function useLocale(): { locale?: string } {\n  return { locale: useContext(LocaleContext).locale };\n}\n\n/**\n * Resolves a component's strings: `labels` prop, then the provider's\n * messages for `namespace`, then the component's English `defaults`.\n * Numbers passed as values are formatted for the provider's locale.\n */\nexport function useMessages<T extends Messages>(\n  namespace: string,\n  defaults: T,\n  labels?: Partial<T>,\n) {\n  const { locale, messages } = useContext(LocaleContext);\n  const provided = messages[namespace];\n  // Inline `labels={{ … }}` is a new object every render. Keying it by content\n  // keeps `t` one identity until a label changes, so effects and memos that\n  // depend on `t` don't re-run per render, and render-time reads never lag.\n  const labelsKey = labels ? JSON.stringify(labels) : \"\";\n  const stableLabels = useMemo(\n    () => (labelsKey ? (JSON.parse(labelsKey) as Partial<T>) : undefined),\n    [labelsKey],\n  );\n\n  return useCallback(\n    (key: keyof T & string, values?: Record<string, string | number>) => {\n      const template = stableLabels?.[key] ?? provided?.[key] ?? defaults[key];\n      if (!values) return template;\n      return template.replace(/\\{(\\w+)\\}/g, (match, name: string) => {\n        const value = values[name];\n        if (value === undefined) return match;\n        return typeof value === \"number\" ? formatNumber(value, locale) : value;\n      });\n    },\n    [defaults, stableLabels, locale, provided],\n  );\n}\n"},{"path":"lib/utils.ts","type":"util","content":"import { clsx, type ClassValue } from \"clsx\"\nimport { twMerge } from \"tailwind-merge\"\n\nexport function cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs))\n}\n"},{"path":"components/previews/blocks/infinite-masonry.preview.tsx","type":"preview","content":"\"use client\";\n\nimport { useCallback, useState } from \"react\";\nimport { InfiniteMasonry } from \"@/components/motion/infinite-masonry\";\n\ntype GalleryItem = {\n  id: string;\n  title: string;\n  category: string;\n  image: string;\n  imageHeight: number;\n};\n\nconst PAGE_SIZE = 6;\nconst MAX_ITEMS = 42;\n\nconst gallerySource = [\n  {\n    title: \"Soft geometry\",\n    category: \"Architecture\",\n    image:\n      \"https://images.unsplash.com/photo-1511818966892-d7d671e672a2?auto=format&fit=crop&w=720&q=80\",\n    imageHeight: 260,\n  },\n  {\n    title: \"Open horizon\",\n    category: \"Landscape\",\n    image:\n      \"https://images.unsplash.com/photo-1500530855697-b586d89ba3ee?auto=format&fit=crop&w=720&q=80\",\n    imageHeight: 190,\n  },\n  {\n    title: \"Working rhythm\",\n    category: \"Workspace\",\n    image:\n      \"https://images.unsplash.com/photo-1497366754035-f200968a6e72?auto=format&fit=crop&w=720&q=80\",\n    imageHeight: 230,\n  },\n  {\n    title: \"Shared table\",\n    category: \"Studio\",\n    image:\n      \"https://images.unsplash.com/photo-1524758631624-e2822e304c36?auto=format&fit=crop&w=720&q=80\",\n    imageHeight: 300,\n  },\n  {\n    title: \"In session\",\n    category: \"People\",\n    image:\n      \"https://images.unsplash.com/photo-1521737711867-e3b97375f902?auto=format&fit=crop&w=720&q=80\",\n    imageHeight: 210,\n  },\n  {\n    title: \"After hours\",\n    category: \"Office\",\n    image:\n      \"https://images.unsplash.com/photo-1504384308090-c894fdcc538d?auto=format&fit=crop&w=720&q=80\",\n    imageHeight: 280,\n  },\n] as const;\n\nfunction createItems(start: number, count: number): GalleryItem[] {\n  return Array.from({ length: count }, (_, offset) => {\n    const index = start + offset;\n    const source = gallerySource[index % gallerySource.length];\n\n    return {\n      ...source,\n      id: `gallery-${index}`,\n      title: `${source.title} ${Math.floor(index / gallerySource.length) + 1}`,\n    };\n  });\n}\n\nexport function InfiniteMasonryPreview() {\n  const [items, setItems] = useState(() => createItems(0, 12));\n  const [loading, setLoading] = useState(false);\n  const hasMore = items.length < MAX_ITEMS;\n\n  const loadMore = useCallback(async () => {\n    if (loading || !hasMore) return;\n    setLoading(true);\n\n    await new Promise((resolve) => setTimeout(resolve, 650));\n    setItems((current) => {\n      const count = Math.min(PAGE_SIZE, MAX_ITEMS - current.length);\n      return count > 0\n        ? [...current, ...createItems(current.length, count)]\n        : current;\n    });\n    setLoading(false);\n  }, [hasMore, loading]);\n\n  return (\n    <div className=\"w-full max-w-5xl\">\n      <div className=\"mb-3 flex items-end justify-between gap-4 px-1\">\n        <div>\n          <p className=\"text-sm font-medium text-foreground\">Visual index</p>\n          <p className=\"mt-0.5 text-xs text-muted-foreground\">\n            Scroll to load the next page\n          </p>\n        </div>\n        <p className=\"font-mono text-xs tabular-nums text-muted-foreground\">\n          {items.length} items\n        </p>\n      </div>\n\n      <InfiniteMasonry\n        items={items}\n        getItemKey={(item) => item.id}\n        renderItem={(item) => (\n          <figure className=\"overflow-hidden rounded-2xl border border-border bg-card\">\n            <div\n              className=\"relative w-full overflow-hidden\"\n              style={{ height: item.imageHeight }}\n            >\n              <img\n                src={item.image}\n                alt={item.title}\n                loading=\"lazy\"\n                decoding=\"async\"\n                className=\"absolute inset-0 size-full object-cover\"\n              />\n            </div>\n            <figcaption className=\"flex items-start justify-between gap-3 p-3\">\n              <p className=\"text-sm font-medium text-foreground\">\n                {item.title}\n              </p>\n              <p className=\"shrink-0 text-[11px] uppercase tracking-wider text-muted-foreground\">\n                {item.category}\n              </p>\n            </figcaption>\n          </figure>\n        )}\n        onLoadMore={loadMore}\n        hasMore={hasMore}\n        loading={loading}\n        estimateSize={(item) => item.imageHeight + 66}\n        ariaLabel=\"Visual inspiration gallery\"\n        className=\"h-[34rem]\"\n      />\n    </div>\n  );\n}\n"}]}