All checks were successful
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
42 lines
2.3 KiB
TypeScript
42 lines
2.3 KiB
TypeScript
'use client'
|
||
import Image from 'next/image'
|
||
import { useCallback, useEffect, useState } from 'react'
|
||
|
||
type Photo = { src: string; alt: string }
|
||
type Labels = { photoClose: string; photoPrev: string; photoNext: string }
|
||
|
||
export function PhotoGallery({ photos, t }: { photos: Photo[]; t: Labels }) {
|
||
const [open, setOpen] = useState<number | null>(null)
|
||
const count = photos.length
|
||
const step = useCallback((d: number) => setOpen(i => (i === null ? i : (i + d + count) % count)), [count])
|
||
useEffect(() => {
|
||
if (open === null) return
|
||
const onKey = (e: KeyboardEvent) => {
|
||
if (e.key === 'Escape') setOpen(null)
|
||
else if (e.key === 'ArrowRight') step(1)
|
||
else if (e.key === 'ArrowLeft') step(-1)
|
||
}
|
||
window.addEventListener('keydown', onKey)
|
||
return () => window.removeEventListener('keydown', onKey)
|
||
}, [open, step])
|
||
if (count === 0) return null
|
||
const btn = 'absolute flex h-11 w-11 items-center justify-center rounded-full bg-paper text-2xl text-graphite'
|
||
return <>
|
||
<ul className="mt-6 grid grid-cols-2 gap-3 sm:grid-cols-3 md:grid-cols-4">
|
||
{photos.map((p, i) => <li key={p.src}>
|
||
<button type="button" onClick={() => setOpen(i)} className="block aspect-square w-full overflow-hidden" aria-label={p.alt}>
|
||
<Image src={p.src} alt={p.alt} width={300} height={300} sizes="(min-width: 768px) 25vw, 50vw" className="h-full w-full object-cover" />
|
||
</button>
|
||
</li>)}
|
||
</ul>
|
||
{open !== null && <div role="dialog" aria-modal="true" className="fixed inset-0 z-50 flex items-center justify-center bg-black/85 p-4" onClick={() => setOpen(null)}>
|
||
<Image src={photos[open].src} alt={photos[open].alt} width={1600} height={1600} sizes="100vw" onClick={e => e.stopPropagation()} className="h-auto max-h-[90vh] w-auto max-w-full object-contain" />
|
||
<button type="button" aria-label={t.photoClose} onClick={() => setOpen(null)} className={`${btn} right-4 top-4`}>×</button>
|
||
{count > 1 && <>
|
||
<button type="button" aria-label={t.photoPrev} onClick={e => { e.stopPropagation(); step(-1) }} className={`${btn} left-3 top-1/2 -translate-y-1/2`}>‹</button>
|
||
<button type="button" aria-label={t.photoNext} onClick={e => { e.stopPropagation(); step(1) }} className={`${btn} right-3 top-1/2 -translate-y-1/2`}>›</button>
|
||
</>}
|
||
</div>}
|
||
</>
|
||
}
|