Crear la estructura base del componente de galería...

Erstellt am: 12. Mai 2025

Beantwortet mit O4 Mini von Chat01

Frage

Crear la estructura base del componente de galería con funcionalidad básica de visualización.

Características:
Diseño Base:
Grid responsive de miniaturas (Tailwind CSS)
Efecto hover sobre imágenes
Sistema de columnas adaptable (mobile/desktop)

Funcionalidad Básica:
Carga de imágenes desde array de URLs
Lightbox básico al hacer clic
Navegación entre imágenes (anterior/siguiente)

Optimizaciones Iniciales:
Lazy loading nativo
Placeholders durante carga
Alt text accesible

Entregable:
Componente funcional con diseño básico
Lightbox operativo
Ejemplo con imágenes de prueba

Gedanke

Thought for a few seconds

Antwort

Aquí tienes un ejemplo de un componente de galería en React con Tailwind CSS que cubre las características que pediste:

jsx
import { useState } from 'react'; const Gallery = ({ images }) => { const [lightboxIndex, setLightboxIndex] = useState(null); const openLightbox = (index) => { setLightboxIndex(index); }; const closeLightbox = () => { setLightboxIndex(null); }; const showPrev = (e) => { e.stopPropagation(); setLightboxIndex((i) => (i > 0 ? i - 1 : images.length - 1)); }; const showNext = (e) => { e.stopPropagation(); setLightboxIndex((i) => (i < images.length - 1 ? i + 1 : 0)); }; return ( <> {/* Grid de miniaturas */} <div className="grid gap-2 grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6"> {images.map((src, idx) => ( <div key={idx} className="relative overflow-hidden bg-gray-100 rounded-lg cursor-pointer" onClick={() => openLightbox(idx)} > {/* Placeholder gris mientras carga */} <img src={src} alt={`Imagen ${idx + 1}`} loading="lazy" className="w-full h-32 object-cover transform transition-transform duration-200 hover:scale-105" /> </div> ))} </div> {/* Lightbox */} {lightboxIndex !== null && ( <div className="fixed inset-0 bg-black bg-opacity-75 flex items-center justify-center z-50" onClick={closeLightbox} > <button onClick={showPrev} className="absolute left-4 text-white text-3xl p-2 hover:opacity-75" aria-label="Anterior" > </button> <img src={images[lightboxIndex]} alt={`Imagen grande ${lightboxIndex + 1}`} className="max-h-full max-w-full rounded" /> <button onClick={showNext} className="absolute right-4 text-white text-3xl p-2 hover:opacity-75" aria-label="Siguiente" > </button> <button onClick={closeLightbox} className="absolute top-4 right-4 text-white text-2xl p-2 hover:opacity-75" aria-label="Cerrar" > × </button> </div> )} </> ); }; // Ejemplo de uso con imágenes de prueba const sampleImages = [ 'https://via.placeholder.com/600x400?text=1', 'https://via.placeholder.com/600x400?text=2', 'https://via.placeholder.com/600x400?text=3', 'https://via.placeholder.com/600x400?text=4', 'https://via.placeholder.com/600x400?text=5', 'https://via.placeholder.com/600x400?text=6', ]; export default function App() { return ( <div className="p-4"> <h1 className="text-2xl font-bold mb-4">Galería de Imágenes</h1> <Gallery images={sampleImages} /> </div> ); }

Explicación rápida:

  • Grid responsive: usamos grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 para adaptarnos a distintos tamaños de pantalla.
  • Hover: efecto hover:scale-105 sobre cada miniatura.
  • Lightbox: capa fija (fixed inset-0) con fondo semitransparente y botones para navegar/cerrar.
  • Lazy loading: atributo loading="lazy" en <img>.
  • Placeholder: fondo bg-gray-100 mientras carga cada miniatura.
  • Alt text accesible para cada imagen.

Puedes pegar este código en tu proyecto y ajustar detalles de estilo o estructura según necesites.

Diesen Q&A teilen