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

173 lines
5.1 KiB
TypeScript

"use client";
import { useRef, useState } from "react";
import { ChevronUp, ChevronDown } from "lucide-react";
import { useGSAP } from "@gsap/react";
import { gsap } from "gsap";
import { faqs } from "@/lib/faqs";
gsap.registerPlugin(useGSAP);
export default function FAQSection({
items = faqs,
intro = "Finden Sie schnell Antworten auf die wichtigsten Fragen.",
}: {
items?: { q: string; a: string }[];
intro?: string;
}) {
const [open, setOpen] = useState<number | null>(0);
const root = useRef<HTMLElement>(null);
useGSAP(
() => {
const media = gsap.matchMedia();
media.add("(prefers-reduced-motion: no-preference)", () => {
const section = root.current;
const header = root.current?.querySelector("[data-faq-header]");
const rows = root.current?.querySelectorAll("[data-faq-row]");
if (!section || !header || !rows?.length) return;
const headerChildren = Array.from(header.children);
const rowElements = Array.from(rows);
gsap.set(headerChildren, { autoAlpha: 0, y: 22 });
gsap.set(rowElements, { autoAlpha: 0, y: 18 });
const revealTimeline = gsap.timeline({
paused: true,
defaults: { ease: "power3.out" },
});
revealTimeline
.to(headerChildren, {
autoAlpha: 1,
y: 0,
duration: 0.55,
stagger: 0.06,
})
.to(
rowElements,
{
autoAlpha: 1,
y: 0,
duration: 0.42,
stagger: 0.045,
},
"-=0.2",
);
const reveal = () => revealTimeline.play();
const observer = new IntersectionObserver(
([entry]) => {
if (!entry?.isIntersecting) return;
reveal();
observer.disconnect();
},
{ rootMargin: "0px 0px -16% 0px", threshold: 0.04 },
);
observer.observe(section);
return () => {
observer.disconnect();
revealTimeline.kill();
};
});
return () => media.revert();
},
{ scope: root },
);
useGSAP(
() => {
if (open === null || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
const answer = root.current?.querySelector('[data-faq-answer="' + open + '"]');
if (!answer) return;
gsap.fromTo(
answer,
{ autoAlpha: 0, y: -7 },
{
autoAlpha: 1,
y: 0,
duration: 0.3,
ease: "power2.out",
overwrite: "auto",
},
);
},
{ scope: root, dependencies: [open], revertOnUpdate: true },
);
return (
<section
ref={root}
className="section-full"
style={{ background: "var(--surface)", paddingBlock: 80 }}
>
<div
className="flex flex-col items-center gap-10 mx-auto"
style={{ maxWidth: "var(--content-width)" }}
>
{/* Header */}
<div className="flex flex-col items-center gap-4 text-center" data-faq-header>
<span
className="text-[12px] font-semibold tracking-widest chamfer-badge px-3 py-1"
style={{ background: "var(--accent-yellow-light)", color: "var(--accent-yellow-dark)" }}
>
FAQ
</span>
<h2
className="text-[32px] md:text-[42px] font-bold leading-[1.15]"
style={{ color: "var(--text-primary)" }}
>
Häufig gestellte Fragen.
</h2>
<p className="text-[16px]" style={{ color: "var(--text-secondary)" }}>
{intro}
</p>
</div>
{/* FAQ list */}
<div className="w-full" style={{ maxWidth: 720 }}>
{items.map((item, i) => (
<div key={item.q} data-faq-row>
<div
style={{
borderBottom: i < items.length - 1 ? "1px solid var(--border-neutral)" : "none",
}}
>
<button
onClick={() => setOpen(open === i ? null : i)}
className="flex items-center justify-between w-full text-left py-6"
>
<span
className="text-[16px] font-semibold pr-8"
style={{ color: "var(--text-primary)" }}
>
{item.q}
</span>
{open === i ? (
<ChevronUp size={20} className="shrink-0" style={{ color: "var(--text-secondary)" }} />
) : (
<ChevronDown size={20} className="shrink-0" style={{ color: "var(--text-secondary)" }} />
)}
</button>
{open === i && (
<p
data-faq-answer={i}
className="text-[15px] leading-relaxed pb-6"
style={{ color: "var(--text-secondary)" }}
>
{item.a}
</p>
)}
</div>
</div>
))}
</div>
</div>
</section>
);
}