You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CampaignCarousel.tsx 4.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { ReactNode, useRef, useState } from "react";
  2. import { ArrowLeft, ArrowRight, ExternalLink } from "lucide-react";
  3. const campaigns = [
  4. { title: "Your snack-time manifesto", src: "", alt: "" },
  5. { title: "Your savoury snack experts", src: "/campaigns/snack-range.webp", alt: "ADUCKTIVE’s range of salted egg cornflakes, rolls and mushroom crisps. Your unique savoury snacks expert." },
  6. { title: "Find us at selected AEON stores", src: "/campaigns/aeon.webp", alt: "ADUCKTIVE campaign announcing availability at selected AEON stores, with the duck mascot and snack range. Open the full banner to read the listed locations." },
  7. { title: "Get your crunch at emart24", src: "/campaigns/emart24.webp", alt: "ADUCKTIVE original and spicy salted egg cornflakes mini packs. Get your salted egg snacks at emart24." },
  8. { title: "Bold flavours. Taste adventure.", src: "/campaigns/bold-flavours.jpg", alt: "ADUCKTIVE snack range: bold flavours, savoury snacks, taste adventure. Cornflakes, rolls and mushroom crisps pictured with their product information." },
  9. { title: "Snack happiness every day", src: "/campaigns/new-banner.webp", alt: "A family sharing ADUCKTIVE snacks together. Snack happiness every day." },
  10. { title: "Bold flavours at OH!SOME", src: "/campaigns/oh-some.jpg", alt: "ADUCKTIVE tempeh chips, mushroom crisps and cornflakes. Now available at OH!SOME." },
  11. { title: "Family pack. Dua pilihan, satu keceriaan.", src: "/campaigns/family-pack.jpg", alt: "ADUCKTIVE original and spicy salted egg cornflakes family packs. Open the full banner for promotional details." },
  12. { title: "Office snack fiesta with pandamart", src: "/campaigns/pandamart-fiesta.png", alt: "Lapar kat office? ADUCKTIVE snacks on pandamart. Open the full banner for promotional details." },
  13. { title: "More salted egg favourites at emart24", src: "/campaigns/emart-update.webp", alt: "ADUCKTIVE spicy and original salted egg cornflakes mini packs at emart24." },
  14. { title: "Find us at ST Rosyam Mart", src: "/campaigns/rosyam-mart.webp", alt: "ADUCKTIVE snacks now available at ST Rosyam Mart. Open the full banner to read the listed locations." },
  15. ];
  16. export function CampaignCarousel({ children }: { children: ReactNode }) {
  17. const [active, setActive] = useState(0);
  18. const touch = useRef<{ x: number; y: number } | null>(null);
  19. const move = (delta: number) => setActive(value => (value + delta + campaigns.length) % campaigns.length);
  20. return <section className="campaign-carousel" aria-label="ADUCKTIVE highlights" aria-roledescription="carousel">
  21. <div className="campaign-controls">
  22. <div className="campaign-caption"><span className="eyebrow">MORE REASONS TO CRUNCH</span><p aria-live="polite" aria-atomic="true">{active + 1} / {campaigns.length} · {campaigns[active].title}</p></div>
  23. <div className="campaign-navigation">
  24. <button type="button" aria-label="Previous banner" onClick={() => move(-1)}><ArrowLeft size={20}/></button>
  25. <div className="campaign-dots" aria-label="Choose a banner">{campaigns.map((campaign, index) => <button key={campaign.title} type="button" aria-label={`Show banner ${index + 1}: ${campaign.title}`} aria-current={index === active ? "true" : undefined} onClick={() => setActive(index)}><span/></button>)}</div>
  26. <button type="button" aria-label="Next banner" onClick={() => move(1)}><ArrowRight size={20}/></button>
  27. </div>
  28. </div>
  29. <div className="campaign-stage" tabIndex={0} aria-label="Banner viewer. Use left and right arrow keys to change banners."
  30. onKeyDown={event => { if (event.key === "ArrowRight" || event.key === "ArrowLeft") { event.preventDefault(); move(event.key === "ArrowRight" ? 1 : -1); } }}
  31. onTouchStart={event => { const point = event.touches[0]; touch.current = { x: point.clientX, y: point.clientY }; }}
  32. onTouchCancel={() => { touch.current = null; }}
  33. onTouchEnd={event => { if (!touch.current) return; const point = event.changedTouches[0]; const dx = point.clientX - touch.current.x; const dy = point.clientY - touch.current.y; touch.current = null; if (Math.abs(dx) > 55 && Math.abs(dx) > Math.abs(dy) * 1.5) move(dx < 0 ? 1 : -1); }}>
  34. <div className="campaign-slide campaign-original" role="group" aria-roledescription="slide" aria-label={`1 of ${campaigns.length}: Your snack-time manifesto`} hidden={active !== 0}>{children}<div className="campaign-original-footer">A little crunch. A lot of main character.</div></div>
  35. {campaigns.slice(1).map((campaign, index) => <div className="campaign-slide campaign-image-slide" role="group" aria-roledescription="slide" aria-label={`${index + 2} of ${campaigns.length}: ${campaign.title}`} key={campaign.src} hidden={active !== index + 1}>
  36. <img src={campaign.src} alt={campaign.alt} width={1920} height={1080} loading="lazy"/>
  37. <a className="campaign-full-image" href={campaign.src} target="_blank" rel="noreferrer">View full banner <ExternalLink size={14}/></a>
  38. </div>)}
  39. </div>
  40. </section>;
  41. }