113 lines
5.6 KiB
TypeScript
113 lines
5.6 KiB
TypeScript
"use client";
|
|
|
|
import Image from "next/image";
|
|
import { useEffect, useId, useRef, useState, type MouseEvent, type ReactNode } from "react";
|
|
import { createPortal } from "react-dom";
|
|
import { DirectionalArrow, ICONS, SiteIcon } from "@/components/grace-site-icons";
|
|
import type { Language } from "@/data/portfolio";
|
|
|
|
type Props = { title: string; images: string[]; imageAlt: string; label: string; language: Language; children?: ReactNode; className?: string };
|
|
|
|
export function ProjectSheetViewer({ title, images, imageAlt, label, language, children, className = "project-sheet-link" }: Props) {
|
|
const [open, setOpen] = useState(false);
|
|
const [zoomed, setZoomed] = useState(false);
|
|
const [index, setIndex] = useState(0);
|
|
const dialog = useRef<HTMLDialogElement>(null);
|
|
const trigger = useRef<HTMLAnchorElement>(null);
|
|
const closeButton = useRef<HTMLButtonElement>(null);
|
|
const viewport = useRef<HTMLDivElement>(null);
|
|
const titleId = useId();
|
|
const dir = language === "ar" ? "rtl" : "ltr";
|
|
const labels = language === "ar"
|
|
? { close: "إغلاق اللوحة", zoom: "تكبير التفاصيل", fit: "عرض اللوحة كاملة", original: "فتح الصورة الأصلية", previous: "اللوحة السابقة", next: "اللوحة التالية", sheet: "لوحة" }
|
|
: { close: "Close sheet", zoom: "Zoom into details", fit: "Fit full sheet", original: "Open original image", previous: "Previous sheet", next: "Next sheet", sheet: "Sheet" };
|
|
|
|
useEffect(() => {
|
|
if (!open || !dialog.current) return;
|
|
const node = dialog.current;
|
|
const previousOverflow = document.body.style.overflow;
|
|
const previousPadding = document.body.style.paddingInlineEnd;
|
|
const scrollbar = window.innerWidth - document.documentElement.clientWidth;
|
|
document.body.style.overflow = "hidden";
|
|
if (scrollbar) document.body.style.paddingInlineEnd = `${scrollbar}px`;
|
|
node.showModal();
|
|
closeButton.current?.focus();
|
|
return () => {
|
|
node.close();
|
|
document.body.style.overflow = previousOverflow;
|
|
document.body.style.paddingInlineEnd = previousPadding;
|
|
trigger.current?.focus({ preventScroll: true });
|
|
};
|
|
}, [open]);
|
|
|
|
useEffect(() => {
|
|
viewport.current?.scrollTo({ top: 0, left: 0, behavior: "instant" });
|
|
}, [index, zoomed]);
|
|
|
|
function openViewer(event: MouseEvent<HTMLAnchorElement>) {
|
|
// Preserve the real image link without JS and for modified/new-tab clicks.
|
|
if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey || typeof HTMLDialogElement === "undefined") return;
|
|
event.preventDefault();
|
|
setIndex(0);
|
|
setZoomed(false);
|
|
setOpen(true);
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<a ref={trigger} href={images[0]} className={className} onClick={openViewer} aria-haspopup="dialog" aria-label={children ? `${label} — ${title}` : undefined}>
|
|
{children ?? <><span>{label}</span><DirectionalArrow dir={dir} size={19} /></>}
|
|
</a>
|
|
{open && createPortal(
|
|
<dialog
|
|
ref={dialog}
|
|
className="sheet-viewer"
|
|
lang={dir === "rtl" ? "ar" : "en"}
|
|
dir={dir}
|
|
aria-labelledby={titleId}
|
|
onCancel={() => setOpen(false)}
|
|
onClose={() => setOpen(false)}
|
|
onClick={event => { if (event.target === event.currentTarget) setOpen(false); }}
|
|
>
|
|
<div className="sheet-viewer-panel">
|
|
<header className="sheet-viewer-header">
|
|
<div className="sheet-viewer-heading">
|
|
<p className="section-label">{label}</p><h2 id={titleId} dir="auto">{title}</h2>
|
|
</div>
|
|
<div className="sheet-viewer-controls">
|
|
<button type="button" onClick={() => setZoomed(value => !value)} aria-pressed={zoomed} aria-label={zoomed ? labels.fit : labels.zoom} title={zoomed ? labels.fit : labels.zoom}>
|
|
<SiteIcon icon={ICONS.interface.expand} size={20} /><span>{zoomed ? labels.fit : labels.zoom}</span>
|
|
</button>
|
|
<a href={images[index]} target="_blank" rel="noreferrer" aria-label={labels.original} title={labels.original}>
|
|
<SiteIcon icon={ICONS.arrows.external} size={20} />
|
|
</a>
|
|
<button ref={closeButton} type="button" onClick={() => setOpen(false)} aria-label={labels.close} title={labels.close}>
|
|
<SiteIcon icon={ICONS.interface.close} size={22} />
|
|
</button>
|
|
</div>
|
|
</header>
|
|
<div ref={viewport} className="sheet-viewer-viewport" data-zoomed={zoomed} tabIndex={0} aria-label={zoomed ? labels.zoom : labels.fit}>
|
|
<Image
|
|
key={images[index]}
|
|
src={images[index]}
|
|
alt={index === 0 ? imageAlt : `${title} — ${labels.sheet} ${index + 1}`}
|
|
width={1493} height={2479} sizes="100vw" unoptimized
|
|
className="sheet-viewer-image"
|
|
/>
|
|
</div>
|
|
{images.length > 1 && <nav className="sheet-viewer-pagination" aria-label={label}>
|
|
<button type="button" disabled={index === 0} onClick={() => setIndex(value => value - 1)} aria-label={labels.previous}>
|
|
<DirectionalArrow direction="backward" dir={dir} size={20} />
|
|
</button>
|
|
<p aria-live="polite">{labels.sheet} {index + 1} / {images.length}</p>
|
|
<button type="button" disabled={index === images.length - 1} onClick={() => setIndex(value => value + 1)} aria-label={labels.next}>
|
|
<DirectionalArrow dir={dir} size={20} />
|
|
</button>
|
|
</nav>}
|
|
</div>
|
|
</dialog>, document.body,
|
|
)}
|
|
</>
|
|
);
|
|
}
|