import React from "react"; import { useEffect, useState } from "react"; import { Testimonial, TestimonialsProps } from "../../types"; import { cn } from "../../lib/utils"; import { motion } from "framer-motion"; import { TestimonialCard } from "./TestimonialCard"; interface MasonryLayoutProps extends Omit { testimonials: Omit[]; } export function MasonryLayout({ testimonials, theme = "light", className, columns = { sm: 1, md: 2, lg: 3, xl: 4 }, }: MasonryLayoutProps) { const [isMounted, setIsMounted] = useState(false); const [columnCount, setColumnCount] = useState(columns.lg || 3); // Create columns based on screen size const getColumnCount = () => { if (typeof window === "undefined") return columns.lg || 3; const width = window.innerWidth; if (width >= 1280) return columns.xl || 4; if (width >= 1024) return columns.lg || 3; if (width >= 768) return columns.md || 2; return columns.sm || 1; }; useEffect(() => { setIsMounted(true); setColumnCount(getColumnCount()); }, []); useEffect(() => { const handleResize = () => { setColumnCount(getColumnCount()); }; window.addEventListener("resize", handleResize); return () => window.removeEventListener("resize", handleResize); }, []); if (!isMounted) { return (
{testimonials.map((testimonial, index) => ( ))}
); } // Distribute testimonials into columns const distributeIntoColumns = () => { const cols: (typeof testimonials)[] = Array.from( { length: columnCount }, () => [] ); testimonials.forEach((testimonial, index) => { const columnIndex = index % columnCount; cols[columnIndex].push(testimonial); }); return cols; }; const columns_data = distributeIntoColumns(); return ( {columns_data.map((column, columnIndex) => (
{column.map((testimonial, index) => ( ))}
))}
); }