SPB Git

Repository Public

Gallery.tsx 6.3 KB 115 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : ORIX App Universe8 * File             : src/components/apps/Gallery.tsx9 * Module           : components10 * Repository       : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website  : https://www.orixlab.dev12 * Created          : 2026-11-2113 * Last Modified    : 2026-11-2114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243"use client";44import Image from "next/image";45import { useCallback, useEffect, useRef, useState } from "react";46import { DeviceFrame } from "./DeviceFrame";4748export type Shot = { src: string; width: number; height: number; device: "iphone" | "ipad" | "mac"; caption: string };49type Labels = { open: string; prev: string; next: string; close: string; counter: string };5051const stripHeight = (d: Shot["device"]) => (d === "mac" ? 360 : d === "ipad" ? 430 : 520);5253export function Gallery({ shots, labels }: { shots: Shot[]; labels: Labels }) {54  const [at, setAt] = useState<number | null>(null);55  const dialog = useRef<HTMLDialogElement>(null);56  const opener = useRef<HTMLElement | null>(null);57  const go = useCallback((d: number) => setAt((i) => (i === null ? i : (i + d + shots.length) % shots.length)), [shots.length]);5859  useEffect(() => {60    const d = dialog.current; if (!d) return;61    if (at !== null && !d.open) d.showModal();62    if (at === null && d.open) d.close();63  }, [at]);64  useEffect(() => {65    if (at === null) return;66    const key = (e: KeyboardEvent) => { if (e.key === "ArrowRight") go(1); else if (e.key === "ArrowLeft") go(-1); };67    addEventListener("keydown", key); return () => removeEventListener("keydown", key);68  }, [at, go]);6970  const cur = at === null ? null : shots[at];71  return (72    <>73      <ul className="scroll-x flex snap-x snap-mandatory items-end gap-6 px-[max(1.25rem,calc((100vw-1400px)/2))] pb-4 sm:gap-10" data-testid="gallery" tabIndex={0} aria-label="Screenshots">74        {shots.map((s, i) => {75          const h = stripHeight(s.device), w = Math.round(h * (s.width / s.height));76          return (77            <li key={s.src} className="flex-none snap-center" style={{ width: `min(${w}px, 82vw)` }}>78              <figure>79                <button type="button" className="block w-full cursor-zoom-in rounded-3xl transition-transform duration-500 ease-out-expo hover:-translate-y-1.5" aria-label={`${labels.open}: ${s.caption}`}80                  onClick={(e) => { opener.current = e.currentTarget; setAt(i); }} data-testid="shot">81                  <DeviceFrame device={s.device}><Image src={s.src} alt={s.caption} width={s.width} height={s.height} sizes={`(max-width: 640px) 82vw, ${w}px`} loading={i < 2 ? "eager" : "lazy"} /></DeviceFrame>82                </button>83                <figcaption className="mt-4 max-w-[26rem] text-[0.88rem] leading-snug text-ink2">{s.caption}</figcaption>84              </figure>85            </li>86          );87        })}88      </ul>8990      <dialog ref={dialog} className="lightbox" aria-label={cur?.caption} data-testid="lightbox"91        onClose={() => { setAt(null); opener.current?.focus(); }} onClick={(e) => { if (e.target === e.currentTarget) setAt(null); }}>92        {cur && at !== null && (93          <div className="flex h-full flex-col">94            <div className="flex items-center justify-between gap-4 px-5 py-4">95              <p className="mono text-[0.78rem] tracking-[0.1em] text-white/60" aria-live="polite">{labels.counter.replace("{i}", String(at + 1)).replace("{n}", String(shots.length))}</p>96              <button type="button" autoFocus className="grid h-11 w-11 place-items-center rounded-full bg-white/10 text-white hover:bg-white/20" aria-label={labels.close} onClick={() => setAt(null)}>97                <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden><path d="M3 3l10 10M13 3 3 13" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" /></svg>98              </button>99            </div>100            <div className="relative min-h-0 flex-1 px-4" onClick={(e) => { if (e.target === e.currentTarget) setAt(null); }}>101              <Image key={cur.src} src={cur.src} alt={cur.caption} width={cur.width} height={cur.height} sizes="100vw" className="mx-auto h-full w-auto max-w-full rounded-2xl object-contain" />102              {shots.length > 1 && (["prev", "next"] as const).map((k) => (103                <button key={k} type="button" aria-label={labels[k]} onClick={() => go(k === "prev" ? -1 : 1)} className={`absolute top-1/2 grid h-12 w-12 -translate-y-1/2 place-items-center rounded-full bg-white/10 text-white backdrop-blur hover:bg-white/20 ${k === "prev" ? "left-3 sm:left-6" : "right-3 sm:right-6"}`} data-testid={`lightbox-${k}`}>104                  <svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden style={{ transform: k === "prev" ? "scaleX(-1)" : undefined }}><path d="M6 3l5 5-5 5" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round" /></svg>105                </button>106              ))}107            </div>108            <p className="mx-auto max-w-2xl px-6 py-5 text-center text-[0.95rem] text-white/80">{cur.caption}</p>109          </div>110        )}111      </dialog>112    </>113  );114}115
Files main
Menu

Theme