AbiirSkincare/Hydrogen/web/app/components/Testimonials.tsx

291 lines
11 KiB
TypeScript

import {useCallback, useState} from 'react';
import {AnimatePresence, motion} from 'motion/react';
import {ChevronLeft, ChevronRight, Quote, Star} from 'lucide-react';
import {PAGE_TITLE_TYPOGRAPHY_CLASS} from '~/lib/pageTitle';
import {useSwipeNavigate} from '~/lib/useSwipeNavigate';
import type {TestimonialsSectionCms} from '~/lib/testimonials';
import {MOCK_TESTIMONIALS_SECTION_CMS} from '~/lib/testimonials';
/**
* GROQ: `home` singleton → testimonials module.
* Use with `context.sanity.loadQuery(TESTIMONIALS_SECTION_GROQ)` or `useQuery` in the browser.
*/
export const TESTIMONIALS_SECTION_GROQ = `*[_type == "home" && _id == "home"][0].testimonialsSection{
title,
testimonials[]{
name,
location,
avatarInitials,
rating,
product,
text,
verified
},
stats[]{
value,
label,
sub
},
sectionBackground,
cardBackground,
gold,
charcoal,
muted,
verifiedGreen,
gridLine,
dotInactive
}` as const;
export {MOCK_TESTIMONIALS_SECTION_CMS};
export type {TestimonialsSectionCms};
export type TestimonialsProps = {
cms: TestimonialsSectionCms;
};
/**
* `React/src/app/components/Testimonials.tsx` — carousel + stats grid; avatars are initials (no raster in reference).
*/
export function Testimonials({cms}: TestimonialsProps) {
const [currentIndex, setCurrentIndex] = useState(0);
const [direction, setDirection] = useState(1);
const sectionBg = cms.sectionBackground;
const cardBg = cms.cardBackground;
const gold = cms.gold;
const charcoal = cms.charcoal;
const muted = cms.muted;
const verifiedGreen = cms.verifiedGreen;
const gridLine = cms.gridLine;
const dotInactive = cms.dotInactive;
const list = cms.testimonials;
const len = list.length;
const goNext = useCallback(() => {
setDirection(1);
setCurrentIndex((prev) => (prev + 1) % len);
}, [len]);
const goPrev = useCallback(() => {
setDirection(-1);
setCurrentIndex((prev) => (prev - 1 + len) % len);
}, [len]);
const swipe = useSwipeNavigate(goPrev, goNext);
const current = list[currentIndex];
return (
<section className="w-full" style={{background: sectionBg}}>
<div className="mx-auto max-w-[1710px] px-6 pt-16 pb-16 sm:px-10 sm:pt-20 sm:pb-20 lg:px-16 lg:pt-[94px] lg:pb-[94px] xl:px-24 2xl:px-[206px]">
<div className="flex flex-col items-center gap-6 lg:gap-16">
<motion.h2
initial={false}
whileInView={{opacity: 1, y: 0}}
viewport={{once: true}}
transition={{duration: 0.55}}
className={`m-0 w-full max-w-[1298px] text-center ${PAGE_TITLE_TYPOGRAPHY_CLASS}`}
style={{color: charcoal}}
>
{cms.title}
</motion.h2>
<div className="flex w-full max-w-[816px] flex-col gap-6 lg:gap-[34px]">
<div
className="relative w-full overflow-hidden"
{...(len > 1 ? swipe : {})}
>
<AnimatePresence custom={direction} mode="wait">
<motion.div
key={currentIndex}
animate={{opacity: 1, x: 0}}
className="flex w-full flex-col items-center gap-7 px-6 py-10 text-center sm:px-10 sm:py-12 lg:px-[51px] lg:py-[51px]"
custom={direction}
exit={{opacity: 0, x: direction * -40}}
initial={false}
style={{background: cardBg}}
transition={{duration: 0.45, ease: [0.25, 0.1, 0.25, 1]}}
>
<Quote
aria-hidden
size={32}
strokeWidth={1.25}
style={{color: gold}}
/>
<div
aria-label={`${current.rating} out of 5 stars`}
className="flex items-start justify-center gap-[4.25px]"
>
{Array.from({length: current.rating}).map((_, i) => (
<Star
key={i}
aria-hidden
fill={gold}
size={14}
strokeWidth={0}
/>
))}
</div>
<p
className="m-0 w-full max-w-[708px] whitespace-pre-line font-['Cormorant_Garamond',serif] font-normal"
style={{
color: charcoal,
fontStyle: 'italic',
fontWeight: 400,
/* +40% vs 22.95px / 36.72px — inline beats reset.css `p { font-size: 1rem }` */
fontSize: '22px',
lineHeight: '41px',
}}
>
&ldquo;{current.text}&rdquo;
</p>
<div className="flex w-full flex-col items-center gap-3">
<div className="flex flex-wrap items-center justify-center gap-[17px]">
<div
className="flex size-[42.5px] shrink-0 items-center justify-center rounded-full font-['Jost',sans-serif] text-[11.9px] font-semibold leading-[17.85px] not-italic"
style={{
background: gold,
color: cardBg,
}}
>
{current.avatarInitials}
</div>
<div className="flex min-w-0 flex-col items-start gap-0.5">
<div className="flex flex-wrap items-center gap-1">
<p
className="m-0 font-['Jost',sans-serif] text-[13.6px] font-medium leading-[24.48px] not-italic"
style={{color: charcoal}}
>
{current.name}
</p>
{current.verified ? (
<span
className="inline-flex items-center px-[8.5px] py-[2px]"
style={{background: sectionBg}}
>
<span
className="font-['Jost',sans-serif] text-[8.5px] font-medium uppercase leading-[12.75px] tracking-[0.85px] not-italic"
style={{color: verifiedGreen}}
>
{'\u2713'} Verified
</span>
</span>
) : null}
</div>
<p
className="m-0 font-['Raleway',sans-serif] font-medium not-italic"
style={{
color: muted,
fontSize: '11px',
lineHeight: 'normal',
}}
>
{current.location?.trim()
? `${current.location.trim()} · ${current.product}`
: current.product}
</p>
</div>
</div>
</div>
</motion.div>
</AnimatePresence>
</div>
<div className="flex w-full items-center justify-center gap-5 lg:gap-[25.5px]">
<button
type="button"
aria-label="Previous testimonial"
className="flex size-[43.5px] shrink-0 cursor-pointer items-center justify-center transition-opacity duration-200 hover:opacity-70"
style={{
border: '1px solid rgba(26, 26, 24, 0.15)',
background: 'transparent',
color: charcoal,
}}
onClick={goPrev}
>
<ChevronLeft size={16} strokeWidth={1.5} />
</button>
<div
aria-label="Testimonial slides"
className="flex min-w-0 max-w-[280px] flex-1 items-center justify-center gap-[8.5px] sm:max-w-[320px]"
role="tablist"
>
{list.map((t, i) => (
<button
key={`${t.name}-${i}`}
type="button"
role="tab"
aria-label={`Go to testimonial ${i + 1}`}
aria-selected={i === currentIndex}
className="cursor-pointer rounded-[3px] border-0 p-0 transition-all duration-300"
style={{
height: 6,
width: i === currentIndex ? 24 : 6,
minWidth: i === currentIndex ? 24 : 6,
background: i === currentIndex ? gold : dotInactive,
}}
onClick={() => {
setDirection(i > currentIndex ? 1 : -1);
setCurrentIndex(i);
}}
/>
))}
</div>
<button
type="button"
aria-label="Next testimonial"
className="flex size-[43.5px] shrink-0 cursor-pointer items-center justify-center transition-opacity duration-200 hover:opacity-70"
style={{
border: '1px solid rgba(26, 26, 24, 0.15)',
background: 'transparent',
color: charcoal,
}}
onClick={goNext}
>
<ChevronRight size={16} strokeWidth={1.5} />
</button>
</div>
</div>
<div
className="grid w-full max-w-[1298px] grid-cols-3 gap-px"
style={{background: gridLine}}
>
{cms.stats.map((stat) => (
<div
key={stat.label}
className="flex min-w-0 flex-col items-center gap-1 px-1.5 py-5 text-center sm:gap-[3px] sm:px-6 sm:py-[34px]"
style={{background: sectionBg}}
>
<p
className="m-0 font-['Cormorant_Garamond',serif] text-[clamp(.5rem,6.5vw,2.125rem)] font-light leading-[1.15] not-italic sm:text-[42px] sm:leading-[72px]"
style={{color: charcoal,fontSize:'2rem'}}
>
{stat.value}
</p>
<p
className="m-0 min-w-0 max-w-full font-['Jost',sans-serif] text-[8px] font-medium uppercase leading-[1.25] tracking-[0.085em] not-italic sm:text-[11.05px] sm:leading-[19.89px] sm:tracking-[1.326px]"
style={{color: charcoal}}
>
{stat.label}
</p>
<p
className="m-0 max-w-full font-['Raleway',sans-serif] text-[9px] font-medium leading-[1.3] not-italic sm:text-[12px] sm:leading-normal"
style={{color: muted}}
>
{stat.sub}
</p>
</div>
))}
</div>
</div>
</div>
</section>
);
}