import { useEffect, useRef } from "react"; import { grid } from "../lib/shop.ts"; import { Icon } from "./Icon.jsx"; const typing = (target) => target instanceof HTMLElement && /^(INPUT|TEXTAREA|SELECT)$/.test(target.tagName); export function Carousel({ slides, index, onIndex, keys = false, pixel = false, full = false, hold, onFull, label }) { const many = slides.length > 1; const one = slides[index] ?? slides[0]; const strip = useRef(null); const swiped = useRef(false); const from = useRef([0, 0]); const first = useRef(true); const step = (delta) => onIndex((((index + delta) % slides.length) + slides.length) % slides.length); useEffect(() => { if (first.current) { first.current = false; return; } strip.current?.children[index]?.scrollIntoView({ block: "nearest", inline: "center", behavior: "smooth" }); }, [index]); useEffect(() => { if (!keys) return; const down = (event) => { if (event.metaKey || event.ctrlKey || event.altKey || typing(event.target)) return; if (event.key === "ArrowLeft") step(-1); else if (event.key === "ArrowRight") step(1); else return; event.preventDefault(); }; document.addEventListener("keydown", down); return () => document.removeEventListener("keydown", down); }); return (
{one ? ( ) : null} {full ? ( ) : null} {many ? ( <> {`${index + 1} / ${slides.length}`} ) : null}
{many ? (
{slides.map((slide, i) => ( ))}
) : null}
); } export const mockupSlides = (images, title) => images.map((image) => ({ style: image.style, thumb: grid(image.url, 400), full: grid(image.url, 1200), link: grid(image.url, 2000), alt: image.alt || title, name: image.style, })); export function Mockups({ images, title, width = 800, onJump }) { return (
{images.map((image, i) => ( ))}
); } export function FullGrid({ images, title, box, onJump }) { return ( event.target === box.current && box.current?.close()}>
{`${images.length} images`}
); }