A layout-grid component from Aceternity UI.
From Aceternity UI
1"use client";
2import React, { useState } from "react";
3import { motion } from "motion/react";
4import { cn } from "@/lib/utils";
5
6type Card = {
7 id: number;
8 content: JSX.Element | React.ReactNode | string;
9 className: string;
10 thumbnail: string;
11};
12
13export const LayoutGrid = ({ cards }: { cards: Card[] }) => {
14 const [selected, setSelected] = useState<Card | null>(null);
15 const [lastSelected, setLastSelected] = useState<Card | null>(null);
16
17 const handleClick = (card: Card) => {
18 setLastSelected(selected);
19 setSelected(card);
20 };
21
22 const handleOutsideClick = () => {
23 setLastSelected(selected);
24 setSelected(null);
25 };
26
27 return (
28 <div className="w-full h-full p-10 grid grid-cols-1 md:grid-cols-3 max-w-7xl mx-auto gap-4 relative">
29 {cards.map((card, i) => (
30 <div key={i} className={cn(card.className, "")}>
31 <motion.div
32 onClick={() => handleClick(card)}
33 className={cn(
34 card.className,
35 "relative overflow-hidden",
36 selected?.id === card.id
37 ? "rounded-lg cursor-pointer absolute inset-0 h-1/2 w-full md:w-1/2 m-auto z-50 flex justify-center items-center flex-wrap flex-col"
38 : lastSelected?.id === card.id
39 ? "z-40 bg-white rounded-xl h-full w-full"
40 : "bg-white rounded-xl h-full w-full"
41 )}
42 layoutId={`card-${card.id}`}
43 >
44 {selected?.id === card.id && <SelectedCard selected={selected} />}
45 <ImageComponent card={card} />
46 </motion.div>
47 </div>
48 ))}
49 <motion.div
50 onClick={handleOutsideClick}
51 className={cn(
52 "absolute h-full w-full left-0 top-0 bg-black opacity-0 z-10",
53 selected?.id ? "pointer-events-auto" : "pointer-events-none"
54 )}
55 animate={{ opacity: selected?.id ? 0.3 : 0 }}
56 />
57 </div>
58 );
59};
60
61const ImageComponent = ({ card }: { card: Card }) => {
62 return (
63 <motion.img
64 layoutId={`image-${card.id}-image`}
65 src={card.thumbnail}
66 height="500"
67 width="500"
68 className={cn(
69 "object-cover object-top absolute inset-0 h-full w-full transition duration-200"
70 )}
71 alt="thumbnail"
72 />
73 );
74};
75
76const SelectedCard = ({ selected }: { selected: Card | null }) => {
77 return (
78 <div className="bg-transparent h-full w-full flex flex-col justify-end rounded-lg shadow-2xl relative z-[60]">
79 <motion.div
80 initial={{
81 opacity: 0,
82 }}
83 animate={{
84 opacity: 0.6,
85 }}
86 className="absolute inset-0 h-full w-full bg-black opacity-60 z-10"
87 />
88 <motion.div
89 layoutId={`content-${selected?.id}`}
90 initial={{
91 opacity: 0,
92 y: 100,
93 }}
94 animate={{
95 opacity: 1,
96 y: 0,
97 }}
98 exit={{
99 opacity: 0,
100 y: 100,
101 }}
102 transition={{
103 duration: 0.3,
104 ease: "easeInOut",
105 }}
106 className="relative px-8 pb-4 z-[70]"
107 >
108 {selected?.content}
109 </motion.div>
110 </div>
111 );
112};
113