Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-website/components/sections/ScreenshotAccordion.tsx
T

400 lines
15 KiB
TypeScript

"use client";
import { useState, useEffect, useRef } from "react";
import { CHAMFER } from "@/lib/chamfer";
import Link from "next/link";
import { ChevronDown, ArrowRight } from "lucide-react";
import styles from "./ScreenshotAccordion.module.css";
/* ─── Types ───────────────────────────────────────────────────────────── */
export interface ScreenshotAccordionItem {
title: string;
body: string;
/** Path to screenshot image — falls back to a generated placeholder */
screenshot?: string;
/** Optional deep-link to the matching product page (e.g. "/produkt/chat") */
href?: string;
}
interface ScreenshotAccordionProps {
items: ScreenshotAccordionItem[];
/** Optional section badge (e.g. "FUNKTIONEN") */
badge?: string;
/** Optional section headline */
headline?: string;
/** Optional section subtext */
subtext?: string;
/** Auto-cycle interval in ms — set 0 to disable (default: 5000) */
interval?: number;
/** Section background (default white); set to match an adjacent section. */
background?: string;
/** Optional section spacing overrides (default: 80px each). */
paddingTop?: number;
paddingBottom?: number;
}
/* ─── Placeholder SVG ─────────────────────────────────────────────────── */
function placeholderSvg(label: string, index: number): string {
const colors = ["#EEF2FF", "#F0FDF4", "#FFF7ED", "#FDF2F8"];
const accents = ["#6366F1", "#22C55E", "#F97316", "#EC4899"];
const bg = colors[index % colors.length];
const accent = accents[index % accents.length];
return `data:image/svg+xml,${encodeURIComponent(`
<svg xmlns="http://www.w3.org/2000/svg" width="960" height="600" viewBox="0 0 960 600">
<rect width="960" height="600" rx="16" fill="${bg}"/>
<rect x="40" y="40" width="180" height="520" rx="12" fill="white" stroke="#E5E7EB" stroke-width="1"/>
<rect x="56" y="72" width="148" height="32" rx="6" fill="${accent}" opacity="0.15"/>
<rect x="56" y="120" width="120" height="12" rx="4" fill="#D1D5DB"/>
<rect x="56" y="148" width="100" height="12" rx="4" fill="#D1D5DB"/>
<rect x="56" y="176" width="130" height="12" rx="4" fill="#D1D5DB"/>
<rect x="56" y="204" width="90" height="12" rx="4" fill="#D1D5DB"/>
<rect x="56" y="232" width="110" height="12" rx="4" fill="#D1D5DB"/>
<rect x="260" y="40" width="660" height="520" rx="12" fill="white" stroke="#E5E7EB" stroke-width="1"/>
<text x="590" y="280" text-anchor="middle" font-family="Inter,system-ui,sans-serif" font-size="22" font-weight="600" fill="${accent}">${label}</text>
<rect x="420" y="310" width="340" height="8" rx="4" fill="#E5E7EB"/>
<rect x="460" y="334" width="260" height="8" rx="4" fill="#F3F4F6"/>
<rect x="300" y="400" width="180" height="44" rx="8" fill="${accent}" opacity="0.12"/>
<rect x="500" y="400" width="180" height="44" rx="8" fill="${accent}" opacity="0.08"/>
<rect x="300" y="460" width="380" height="44" rx="8" fill="${accent}" opacity="0.06"/>
</svg>
`)}`;
}
/* ─── Component ───────────────────────────────────────────────────────── */
export default function ScreenshotAccordion({
items,
badge,
headline,
subtext,
interval = 5000,
background = "var(--surface)",
paddingTop = 80,
paddingBottom = 80,
}: ScreenshotAccordionProps) {
const [activeIndex, setActiveIndex] = useState(0);
const [progressRunId, setProgressRunId] = useState(0);
const [isInView, setIsInView] = useState(true);
const [bodyMinHeight, setBodyMinHeight] = useState(0);
const cycleTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const sectionRef = useRef<HTMLElement>(null);
const accordionColumnRef = useRef<HTMLDivElement>(null);
const autoCycleEnabled = interval > 0 && items.length > 1;
const shouldAutoCycle = autoCycleEnabled && isInView;
useEffect(() => {
const section = sectionRef.current;
if (!section || typeof IntersectionObserver === "undefined") {
return;
}
const observer = new IntersectionObserver(
(entries) => {
const entry = entries[0];
setIsInView(Boolean(entry?.isIntersecting));
},
{ threshold: 0.2 }
);
observer.observe(section);
return () => {
observer.disconnect();
};
}, []);
useEffect(() => {
if (!shouldAutoCycle) {
if (cycleTimeoutRef.current) {
clearTimeout(cycleTimeoutRef.current);
cycleTimeoutRef.current = null;
}
return;
}
cycleTimeoutRef.current = setTimeout(() => {
setActiveIndex((prev) => (prev + 1) % items.length);
setProgressRunId((prev) => prev + 1);
}, interval);
return () => {
if (cycleTimeoutRef.current) {
clearTimeout(cycleTimeoutRef.current);
cycleTimeoutRef.current = null;
}
};
}, [activeIndex, interval, items.length, shouldAutoCycle]);
useEffect(() => {
const column = accordionColumnRef.current;
if (!column || typeof window === "undefined") {
return;
}
let frameId = 0;
const measure = () => {
const columnWidth = column.clientWidth;
if (columnWidth <= 0) {
return;
}
const bodyWidth = Math.max(0, columnWidth - 24 * 2);
const probe = document.createElement("p");
probe.style.position = "absolute";
probe.style.visibility = "hidden";
probe.style.pointerEvents = "none";
probe.style.margin = "0";
probe.style.padding = "0";
probe.style.width = `${bodyWidth}px`;
probe.style.fontSize = "14px";
probe.style.lineHeight = "1.625";
probe.style.fontFamily = "var(--font-sans)";
probe.style.whiteSpace = "normal";
probe.style.color = "var(--text-secondary)";
column.appendChild(probe);
let maxHeight = 0;
for (const item of items) {
probe.textContent = item.body;
const height = Math.ceil(probe.getBoundingClientRect().height);
if (height > maxHeight) {
maxHeight = height;
}
}
column.removeChild(probe);
setBodyMinHeight(maxHeight);
};
const scheduleMeasure = () => {
if (frameId) {
cancelAnimationFrame(frameId);
}
frameId = requestAnimationFrame(measure);
};
scheduleMeasure();
const resizeObserver =
typeof ResizeObserver !== "undefined"
? new ResizeObserver(scheduleMeasure)
: null;
resizeObserver?.observe(column);
window.addEventListener("resize", scheduleMeasure);
return () => {
resizeObserver?.disconnect();
window.removeEventListener("resize", scheduleMeasure);
if (frameId) {
cancelAnimationFrame(frameId);
}
};
}, [items]);
const handleClick = (index: number) => {
setActiveIndex(index);
setProgressRunId((prev) => prev + 1);
};
const hasHeader = badge || headline || subtext;
const activeHref = items[activeIndex]?.href;
return (
<section
ref={sectionRef}
className="section-full"
style={{ background, paddingTop, paddingBottom }}
>
<div className="mx-auto" style={{ maxWidth: "var(--content-width)" }}>
{/* Optional header */}
{hasHeader && (
<div className="flex flex-col items-center gap-4 text-center mb-16">
{badge && (
<span
className="text-[12px] font-semibold tracking-widest chamfer-badge px-2 py-0.5"
style={{
background: "var(--accent-yellow-light)",
color: "var(--accent-yellow-dark)",
}}
>
{badge}
</span>
)}
{headline && (
<h2
className="text-[32px] md:text-[42px] font-bold leading-[1.15]"
style={{ color: "var(--text-primary)" }}
>
{headline}
</h2>
)}
{subtext && (
<p
className="text-[16px] md:text-[18px] leading-relaxed max-w-2xl"
style={{ color: "var(--text-secondary)" }}
>
{subtext}
</p>
)}
</div>
)}
{/* Content: Accordion + Screenshot */}
<div className="flex flex-col lg:flex-row items-start gap-12">
{/* Left: Accordion */}
<div ref={accordionColumnRef} className="flex flex-col w-full lg:w-[400px] shrink-0">
{items.map((item, i) => {
const isActive = i === activeIndex;
return (
<div
key={item.title}
// When active, bleed the card outward by its padding (lg only,
// to avoid horizontal overflow on the full-width mobile column)
// so its text lines up flush with the closed items instead of
// being indented by the 24px padding.
className={isActive ? "lg:-mx-6" : undefined}
style={{
background: isActive ? "var(--container-bg)" : "transparent",
clipPath: isActive ? CHAMFER : undefined,
padding: isActive ? "24px" : 0,
transition: "all 0.2s ease",
}}
>
{/* Header row = the toggle. Body + link live OUTSIDE the button
so the product link can be a valid <a> (no <a> in <button>). */}
<button
onClick={() => handleClick(i)}
className="w-full text-left group"
style={{ padding: isActive ? 0 : "20px 0", background: "transparent" }}
>
{/* Progress bar for active item */}
{isActive && autoCycleEnabled && (
<div
className="mb-4 rounded-full overflow-hidden"
style={{ height: 3, background: "var(--border-light)" }}
>
<div
key={`${activeIndex}-${progressRunId}-${shouldAutoCycle ? "run" : "pause"}`}
className="h-full rounded-full"
style={{
width: "100%",
background: "var(--accent-yellow)",
transformOrigin: "left center",
transform: "scaleX(0)",
animation: shouldAutoCycle
? `accordion-progress-fill ${interval}ms linear forwards`
: "none",
}}
/>
</div>
)}
<div className="flex items-center justify-between gap-3">
<span
className="text-[18px] font-bold"
style={{
color: isActive
? "var(--text-primary)"
: "var(--text-secondary)",
}}
>
{item.title}
</span>
<ChevronDown
size={18}
className="shrink-0 transition-transform duration-200"
style={{
color: "var(--text-muted)",
transform: isActive ? "rotate(180deg)" : "rotate(0deg)",
}}
/>
</div>
</button>
{/* Body + product-page link (active only) */}
{isActive && (
<div className="mt-2 flex flex-col items-start gap-3">
<p
className="text-[14px] leading-relaxed"
style={{
color: "var(--text-secondary)",
minHeight: bodyMinHeight > 0 ? `${bodyMinHeight}px` : undefined,
}}
>
{item.body}
</p>
{item.href && (
<Link
href={item.href}
className="inline-flex items-center gap-1.5 text-[14px] font-semibold transition-transform duration-200 hover:translate-x-0.5"
style={{ color: "var(--accent-yellow-dark)" }}
>
Mehr zu {item.title}
<ArrowRight size={15} />
</Link>
)}
</div>
)}
{/* Divider between closed items */}
{!isActive && i !== items.length - 1 && activeIndex !== i + 1 && (
<div style={{ height: 1, background: "var(--border-light)" }} />
)}
</div>
);
})}
</div>
{/* Right: fixed screenshot stage. Every image occupies the same absolute
layer, so switching modules never changes the row dimensions. The
stage itself lifts on hover; the image is never zoomed inside a
clipped frame, keeping every edge of the product UI visible. */}
<div className="flex-1 w-full min-w-0">
{(() => {
const screenLayer = (
<span className={styles.screenLayer}>
{items.map((item, i) => (
<img
key={item.title}
src={item.screenshot ?? placeholderSvg(item.title, i)}
alt={`Screenshot: ${item.title}`}
className={styles.screenshot}
style={{
opacity: i === activeIndex ? 1 : 0,
objectFit: item.screenshot ? "contain" : "cover",
}}
loading="lazy"
decoding="async"
draggable={false}
/>
))}
</span>
);
return activeHref ? (
<Link
href={activeHref}
aria-label={`Mehr zu ${items[activeIndex]?.title}`}
className={`${styles.stage} cursor-pointer`}
>
{screenLayer}
</Link>
) : (
<div className={styles.stage}>
{screenLayer}
</div>
);
})()}
</div>
</div>
</div>
</section>
);
}