Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

components.tsx 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. import { Img } from "./media";
  2. import { useEffect, useRef, useState, ReactNode, FormEvent } from "react";
  3. import {
  4. ArrowUpRight,
  5. ArrowRight,
  6. Search,
  7. Heart,
  8. ShoppingBag,
  9. UserRound,
  10. Menu,
  11. X,
  12. Plus,
  13. Minus,
  14. Leaf,
  15. MapPin,
  16. Package,
  17. MessageCircle,
  18. Instagram,
  19. Facebook,
  20. Check,
  21. ChevronLeft,
  22. ChevronDown,
  23. ChevronRight,
  24. Quote,
  25. Pause,
  26. Play,
  27. Mail,
  28. } from "lucide-react";
  29. import {
  30. Product,
  31. products,
  32. money,
  33. categories,
  34. collections,
  35. reviews,
  36. social,
  37. contact,
  38. articles,
  39. } from "./data";
  40. import { useStore } from "./store";
  41. import { Link, navigate } from "./router";
  42. export function Button({
  43. children,
  44. href,
  45. onClick,
  46. secondary = false,
  47. className = "",
  48. type = "button",
  49. disabled = false,
  50. }: {
  51. children: ReactNode;
  52. href?: string;
  53. onClick?: () => void;
  54. secondary?: boolean;
  55. className?: string;
  56. type?: "button" | "submit";
  57. disabled?: boolean;
  58. }) {
  59. const cls = `button ${secondary ? "secondary" : ""} ${className}`;
  60. return href ? (
  61. <Link className={cls} href={href} onClick={onClick}>
  62. {children}
  63. </Link>
  64. ) : (
  65. <button className={cls} onClick={onClick} type={type} disabled={disabled}>
  66. {children}
  67. </button>
  68. );
  69. }
  70. export function IconButton({
  71. label,
  72. children,
  73. onClick,
  74. className = "",
  75. }: {
  76. label: string;
  77. children: ReactNode;
  78. onClick: () => void;
  79. className?: string;
  80. }) {
  81. return (
  82. <button
  83. className={"icon-button " + className}
  84. aria-label={label}
  85. onClick={onClick}
  86. >
  87. {children}
  88. </button>
  89. );
  90. }
  91. export function SectionHeading({
  92. eyebrow,
  93. title,
  94. copy,
  95. link,
  96. label = "Explore all",
  97. }: {
  98. eyebrow?: string;
  99. title: string;
  100. copy?: string;
  101. link?: string;
  102. label?: string;
  103. }) {
  104. return (
  105. <div className="section-heading">
  106. <div>
  107. {eyebrow && <p className="eyebrow">{eyebrow}</p>}
  108. <h2>{title}</h2>
  109. {copy && <p className="muted">{copy}</p>}
  110. </div>
  111. {link && (
  112. <Link className="text-link" href={link}>
  113. {label}
  114. <ArrowUpRight size={18} />
  115. </Link>
  116. )}
  117. </div>
  118. );
  119. }
  120. export const nav = [
  121. ["Home", "/"],
  122. ["Shop", "/shop"],
  123. ["Collections", "/collections"],
  124. ["About", "/about"],
  125. ["Journal", "/journal"],
  126. ["Contact", "/contact"],
  127. ];
  128. const shopLinks = [
  129. { label: "All snacks", href: "/shop", note: "Find your next favourite" },
  130. { label: "New arrivals", href: "/shop?new=1", note: "Fresh to the snack club" },
  131. ...categories.filter(category => category !== "All snacks").map(category => ({
  132. label: category, href: "/shop?category=" + encodeURIComponent(category), note: "",
  133. })),
  134. ];
  135. function ShopDropdown({ path = "", mobile = false, onNavigate }: { path?: string; mobile?: boolean; onNavigate?: () => void }) {
  136. const [open, setOpen] = useState(false);
  137. const root = useRef<HTMLDivElement>(null);
  138. const trigger = useRef<HTMLButtonElement>(null);
  139. const id = mobile ? "mobile-shop-links" : "desktop-shop-links";
  140. useEffect(() => { setOpen(false); }, [path]);
  141. useEffect(() => {
  142. if (!open) return;
  143. const outside = (event: PointerEvent) => {
  144. if (!root.current?.contains(event.target as Node)) setOpen(false);
  145. };
  146. document.addEventListener("pointerdown", outside);
  147. return () => document.removeEventListener("pointerdown", outside);
  148. }, [open]);
  149. return <div ref={root} className={`shop-dropdown ${mobile ? "shop-dropdown-mobile" : ""}`}
  150. onBlur={event => { if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setOpen(false); }}
  151. onKeyDown={event => { if (event.key === "Escape" && open) { event.preventDefault(); event.stopPropagation(); setOpen(false); trigger.current?.focus(); } }}>
  152. <div className="shop-dropdown-heading">
  153. {mobile && <Link href="/shop" onClick={onNavigate}>Shop</Link>}
  154. <button ref={trigger} type="button" className="shop-dropdown-trigger" data-active={path.startsWith("/shop")}
  155. aria-label={mobile ? "Shop categories" : undefined} aria-expanded={open} aria-controls={id}
  156. onClick={() => setOpen(value => !value)}>
  157. {!mobile && "Shop"}<ChevronDown size={14} aria-hidden="true" />
  158. </button>
  159. </div>
  160. <div id={id} className="shop-dropdown-panel" hidden={!open}>
  161. <p className="eyebrow">CHOOSE YOUR CRUNCH</p>
  162. {shopLinks.map(link => <Link key={link.href} href={link.href} onClick={() => { setOpen(false); onNavigate?.(); }}>
  163. <span><strong>{link.label}</strong>{link.note && <small>{link.note}</small>}</span><ArrowUpRight size={16} aria-hidden="true" />
  164. </Link>)}
  165. </div>
  166. </div>;
  167. }
  168. export function Header({ path }: { path: string }) {
  169. const { setOverlay, count, wishlist } = useStore();
  170. const [scrolled, setScrolled] = useState(false);
  171. const [announcementPaused, setAnnouncementPaused] = useState(false);
  172. useEffect(() => {
  173. const f = () => setScrolled(scrollY > 20);
  174. window.addEventListener("scroll", f, { passive: true });
  175. return () => window.removeEventListener("scroll", f);
  176. }, []);
  177. return (
  178. <>
  179. <div className="announcement" data-paused={announcementPaused}>
  180. <Link className="announcement-viewport" href="/shop?new=1">
  181. <div className="announcement-track">
  182. {[0, 1].map(copy => (
  183. <div className="announcement-group" key={copy} aria-hidden={copy === 1 ? true : undefined}>
  184. <span>GOOD SNACKS. GREAT MOMENTS.</span>
  185. <span>MEET YOUR NEW FAVOURITE CRUNCH <ArrowUpRight size={12} /></span>
  186. <span>MALAYSIAN MADE, SHARED WITH THE WORLD</span>
  187. </div>
  188. ))}
  189. </div>
  190. </Link>
  191. <button className="announcement-toggle" type="button"
  192. aria-label={announcementPaused ? "Resume announcement animation" : "Pause announcement animation"}
  193. onClick={() => setAnnouncementPaused(paused => !paused)}>
  194. {announcementPaused ? <Play size={12} aria-hidden="true" /> : <Pause size={12} aria-hidden="true" />}
  195. </button>
  196. </div>
  197. <header className={scrolled ? "header scrolled" : "header"}>
  198. <div className="header-inner">
  199. <IconButton
  200. className="mobile-only"
  201. label="Open menu"
  202. onClick={() => setOverlay("menu")}
  203. >
  204. <Menu />
  205. </IconButton>
  206. <Link href="/" className="brand" aria-label="ADUCKTIVE home">
  207. <Img
  208. src="/images/logo.webp"
  209. width="128"
  210. height="72"
  211. alt="ADUCKTIVE"
  212. />
  213. </Link>
  214. <nav className="desktop-nav" aria-label="Main navigation">
  215. {nav.map(([label, href]) => label === "Shop" ? <ShopDropdown key={href} path={path} /> : (
  216. <Link
  217. key={href}
  218. href={href}
  219. aria-current={
  220. (href === "/" ? path === "/" : path.startsWith(href))
  221. ? "page"
  222. : undefined
  223. }
  224. >
  225. {label}
  226. </Link>
  227. ))}
  228. </nav>
  229. <div className="header-tools">
  230. <IconButton label="Search" onClick={() => setOverlay("search")}>
  231. <Search />
  232. </IconButton>
  233. <Link
  234. href="/account"
  235. className="icon-button desktop-only"
  236. aria-label="My account"
  237. >
  238. <UserRound />
  239. </Link>
  240. <Link
  241. href="/wishlist"
  242. className="icon-button desktop-only"
  243. aria-label={`Wishlist (${wishlist.length})`}
  244. >
  245. <Heart />
  246. </Link>
  247. <button
  248. className="cart-trigger"
  249. onClick={() => setOverlay("cart")}
  250. aria-label={`Open shopping bag (${count})`}
  251. >
  252. <ShoppingBag size={21} />
  253. <span>{count}</span>
  254. </button>
  255. </div>
  256. </div>
  257. </header>
  258. </>
  259. );
  260. }
  261. export function ProductCard({ product: p }: { product: Product }) {
  262. const { add, wishlist, toggleWish } = useStore();
  263. return (
  264. <article className="product-card">
  265. <div className={`product-picture tone-${p.id % 4}`} data-category={p.category}>
  266. <Link
  267. className="product-image-link"
  268. href={"/product/" + p.slug}
  269. aria-label={"View " + p.name}
  270. >
  271. <Img
  272. src={p.images[0]}
  273. sizes="(max-width: 900px) 45vw, 24vw"
  274. alt={`${p.name} ${p.weight}`}
  275. loading="lazy"
  276. width="500"
  277. height="500"
  278. />
  279. {p.images[1] && (
  280. <Img
  281. className="secondary-image"
  282. sizes="(max-width: 900px) 45vw, 24vw"
  283. src={p.images[1]}
  284. alt=""
  285. loading="lazy"
  286. width="500"
  287. height="500"
  288. />
  289. )}
  290. </Link>
  291. <div className="product-badges">
  292. {p.isNew ? (
  293. <span className="badge">JUST LANDED</span>
  294. ) : p.regularPrice > p.price ? (
  295. <span className="badge">SPECIAL PRICE</span>
  296. ) : (
  297. <span className="badge neutral">
  298. {p.spicy ? "A LITTLE HEAT" : "THE ORIGINAL"}
  299. </span>
  300. )}
  301. </div>
  302. <button
  303. className={"wish-button " + (wishlist.includes(p.id) ? "saved" : "")}
  304. aria-label={`${wishlist.includes(p.id) ? "Remove" : "Save"} ${p.name} ${wishlist.includes(p.id) ? "from" : "to"} wishlist`}
  305. aria-pressed={wishlist.includes(p.id)}
  306. onClick={() => toggleWish(p.id)}
  307. >
  308. <Heart size={18} />
  309. </button>
  310. <Link className="view-product" href={"/product/" + p.slug}>
  311. View product <ArrowUpRight size={15} />
  312. </Link>
  313. </div>
  314. <div className="product-meta">
  315. <span>
  316. {p.pack} {p.weight && `· ${p.weight}`}
  317. </span>
  318. <span>{p.spicy ? "Spicy" : "Original"}</span>
  319. </div>
  320. <h3>
  321. <Link href={"/product/" + p.slug}>{p.name}</Link>
  322. </h3>
  323. <p className="product-caption">
  324. {p.category === "Cornflakes"
  325. ? "Golden crunch. Rich salted egg."
  326. : p.category === "Mushroom crisps"
  327. ? "A satisfyingly different crunch."
  328. : p.category === "Tempeh chips"
  329. ? "A new twist on snack time."
  330. : "Made for the moments you share."}
  331. </p>
  332. <div className="product-bottom">
  333. <div>
  334. <strong>{money(p.price)}</strong>
  335. {p.regularPrice > p.price && <del>{money(p.regularPrice)}</del>}
  336. </div>
  337. <button
  338. className="quick-add"
  339. disabled={!p.stock}
  340. aria-label={`Quick add ${p.name} ${p.weight}`}
  341. onClick={() => add(p)}
  342. >
  343. <span>{p.stock ? "Quick add" : "Sold out"}</span>
  344. <Plus size={17} />
  345. </button>
  346. </div>
  347. </article>
  348. );
  349. }
  350. export function ProductGrid({ items }: { items: Product[] }) {
  351. return (
  352. <div className="product-grid">
  353. {items.map((p) => (
  354. <ProductCard key={p.id} product={p} />
  355. ))}
  356. </div>
  357. );
  358. }
  359. export function QuantitySelector({
  360. value,
  361. onChange,
  362. max = 99,
  363. label = "Quantity",
  364. }: {
  365. value: number;
  366. onChange: (q: number) => void;
  367. max?: number;
  368. label?: string;
  369. }) {
  370. return (
  371. <div className="quantity">
  372. <button
  373. aria-label={"Decrease " + label}
  374. disabled={value <= 1}
  375. onClick={() => onChange(value - 1)}
  376. >
  377. <Minus size={15} />
  378. </button>
  379. <output aria-label={label}>{value}</output>
  380. <button
  381. aria-label={"Increase " + label}
  382. disabled={value >= max}
  383. onClick={() => onChange(value + 1)}
  384. >
  385. <Plus size={15} />
  386. </button>
  387. </div>
  388. );
  389. }
  390. export function Benefits() {
  391. return (
  392. <div className="benefits">
  393. {[
  394. [MapPin, "Malaysian at heart", "Our story began in Subang Jaya."],
  395. [
  396. Leaf,
  397. "Flavour comes first",
  398. "Discover our signature salted egg snacks.",
  399. ],
  400. [
  401. Package,
  402. "A pack for every moment",
  403. "From a little treat to something to share.",
  404. ],
  405. [
  406. MessageCircle,
  407. "Let’s talk snacks",
  408. "Our team is just a message away.",
  409. ],
  410. ].map(([Icon, title, text]: any) => (
  411. <div key={title}>
  412. <Icon size={28} strokeWidth={1.4} />
  413. <h3>{title}</h3>
  414. <p>{text}</p>
  415. </div>
  416. ))}
  417. </div>
  418. );
  419. }
  420. export function CollectionGrid({ all = false }: { all?: boolean }) {
  421. return (
  422. <div className={"collection-grid " + (all ? "all-collections" : "")}>
  423. {collections.slice(0, all ? 4 : 2).map((c, i) => (
  424. <Link
  425. key={c.title}
  426. className={"collection-card collection-" + i}
  427. href={"/shop?category=" + encodeURIComponent(c.category)}
  428. >
  429. <Img
  430. src={c.image}
  431. alt={c.category + " by ADUCKTIVE"}
  432. loading="lazy"
  433. />
  434. <div className="collection-shade" />
  435. <div className="collection-content">
  436. <p className="eyebrow">THE {c.category.toUpperCase()} COLLECTION</p>
  437. <h3>{c.title}</h3>
  438. <p>{c.subtitle}</p>
  439. <span className="text-link">
  440. {c.label}
  441. <ArrowUpRight size={20} />
  442. </span>
  443. </div>
  444. <span className="collection-number">0{i + 1}</span>
  445. </Link>
  446. ))}
  447. </div>
  448. );
  449. }
  450. export function Testimonials() {
  451. const ref = useRef<HTMLDivElement>(null);
  452. return (
  453. <section className="section testimonials">
  454. <SectionHeading
  455. eyebrow="THE WORD AROUND THE TABLE"
  456. title="One bite. And they’re hooked."
  457. />
  458. <div className="review-top">
  459. <p className="muted">Real words from the ADUCKTIVE community.</p>
  460. <div className="arrows">
  461. <IconButton
  462. label="Previous reviews"
  463. onClick={() =>
  464. ref.current?.scrollBy({ left: -400, behavior: "smooth" })
  465. }
  466. >
  467. <ChevronLeft />
  468. </IconButton>
  469. <IconButton
  470. label="Next reviews"
  471. onClick={() =>
  472. ref.current?.scrollBy({ left: 400, behavior: "smooth" })
  473. }
  474. >
  475. <ChevronRight />
  476. </IconButton>
  477. </div>
  478. </div>
  479. <div
  480. className="reviews"
  481. ref={ref}
  482. tabIndex={0}
  483. aria-label="Customer testimonials"
  484. >
  485. {reviews.map((r, i) => (
  486. <article className="review" key={i}>
  487. <Quote size={28} strokeWidth={1.2} />
  488. <blockquote>{r.message}</blockquote>
  489. <div className="review-author">
  490. <span className="avatar">{r.name.charAt(0)}</span>
  491. <div>
  492. <strong>{r.name}</strong>
  493. <small>From our customer community</small>
  494. </div>
  495. </div>
  496. </article>
  497. ))}
  498. </div>
  499. </section>
  500. );
  501. }
  502. export function Newsletter() {
  503. const [status, setStatus] = useState("");
  504. function submit(e: FormEvent<HTMLFormElement>) {
  505. e.preventDefault();
  506. const data = new FormData(e.currentTarget);
  507. const email = String(data.get("email"));
  508. window.location.href = `mailto:${contact.email}?subject=${encodeURIComponent("ADUCKTIVE newsletter sign-up request")}&body=${encodeURIComponent(`Please add ${email} to the ADUCKTIVE newsletter. I agree to receive new product and brand updates.`)}`;
  509. setStatus(
  510. "Your email app will open with a sign-up request. Send it to ask our team to subscribe you.",
  511. );
  512. }
  513. return (
  514. <section className="newsletter">
  515. <div>
  516. <p className="eyebrow">A LITTLE SOMETHING IN YOUR INBOX</p>
  517. <h2>Your inbox. Extra crunchy.</h2>
  518. <p>New releases, tasty updates, and more reasons to snack.</p>
  519. </div>
  520. <form onSubmit={submit}>
  521. <label htmlFor="newsletter-email">Email address</label>
  522. <div className="newsletter-field">
  523. <input
  524. id="newsletter-email"
  525. name="email"
  526. type="email"
  527. required
  528. placeholder="Your email address"
  529. autoComplete="email"
  530. />
  531. <button type="submit" aria-label="Subscribe to newsletter">
  532. SUBSCRIBE
  533. <ArrowUpRight size={20} />
  534. </button>
  535. </div>
  536. <p className="small">
  537. Request your subscription by email.{" "}
  538. <Link href="/help/privacy">Privacy information</Link>
  539. </p>
  540. <p role="status">{status}</p>
  541. </form>
  542. </section>
  543. );
  544. }
  545. export function Footer() {
  546. const groups = [
  547. [
  548. "SHOP",
  549. ["All snacks", "/shop"],
  550. ["New arrivals", "/shop?new=1"],
  551. ["Featured favourites", "/shop?featured=1"],
  552. ["Collections", "/collections"],
  553. ],
  554. [
  555. "OUR WORLD",
  556. ["Our story", "/about"],
  557. ["Journal", "/journal"],
  558. ["Find us", "https://aducktive.com/find-us"],
  559. ["Contact", "/contact"],
  560. ],
  561. [
  562. "HERE TO HELP",
  563. ["FAQs", "/help/faq"],
  564. ["Shipping", "/help/shipping"],
  565. ["Returns", "/help/returns"],
  566. ["Privacy", "/help/privacy"],
  567. ["Terms & conditions", "/help/terms"],
  568. ],
  569. ];
  570. return (
  571. <footer className="footer">
  572. <div className="footer-main">
  573. <div className="footer-brand">
  574. <Link href="/" aria-label="ADUCKTIVE home">
  575. <Img
  576. src="/images/logo.webp"
  577. alt="ADUCKTIVE"
  578. width="160"
  579. height="90"
  580. loading="lazy"
  581. />
  582. </Link>
  583. <p>
  584. Bold flavours. Savoury snacks.
  585. <br />
  586. Taste adventures.
  587. </p>
  588. <div className="social-links">
  589. <a
  590. href={social[0]}
  591. aria-label="ADUCKTIVE on Facebook"
  592. target="_blank"
  593. rel="noreferrer"
  594. >
  595. <Facebook size={19} />
  596. </a>
  597. <a
  598. href={social[1]}
  599. aria-label="ADUCKTIVE on Instagram"
  600. target="_blank"
  601. rel="noreferrer"
  602. >
  603. <Instagram size={19} />
  604. </a>
  605. <a
  606. href={social[2]}
  607. target="_blank"
  608. rel="noreferrer"
  609. aria-label="ADUCKTIVE on TikTok"
  610. >
  611. TikTok ↗
  612. </a>
  613. </div>
  614. </div>
  615. {groups.map(([title, ...links]) => (
  616. <div className="footer-column" key={String(title)}>
  617. <h3>{title}</h3>
  618. {links.map(([label, href]) => (
  619. <Link key={label} href={href}>
  620. {label}
  621. </Link>
  622. ))}
  623. </div>
  624. ))}
  625. <div className="footer-column footer-contact">
  626. <h3>SAY HELLO</h3>
  627. <a href={"mailto:" + contact.email}>{contact.email}</a>
  628. <a href="tel:+60182654321">{contact.phone}</a>
  629. <p>Selangor, Malaysia</p>
  630. <span className="country">MY / MYR (RM)</span>
  631. </div>
  632. </div>
  633. <div className="footer-signoff" aria-hidden="true">
  634. SO DUCKIN’ GOOD.
  635. </div>
  636. <div className="footer-bottom">
  637. <span>
  638. © {new Date().getFullYear()} Aducktive Food Sdn. Bhd. 1384616-H. All
  639. rights reserved.
  640. </span>
  641. <span>
  642. Made for the joy of snacking. <span aria-hidden="true">↗</span>
  643. </span>
  644. </div>
  645. </footer>
  646. );
  647. }
  648. export function EmptyState({ title, copy }: { title: string; copy: string }) {
  649. return (
  650. <div className="empty-state">
  651. <ShoppingBag size={42} strokeWidth={1} />
  652. <h2>{title}</h2>
  653. <p>{copy}</p>
  654. <Button href="/shop">
  655. Explore the snacks
  656. <ArrowUpRight size={18} />
  657. </Button>
  658. </div>
  659. );
  660. }
  661. export function Modal({
  662. title,
  663. children,
  664. onClose,
  665. side = false,
  666. }: {
  667. title: string;
  668. children: ReactNode;
  669. onClose: () => void;
  670. side?: boolean;
  671. }) {
  672. const ref = useRef<HTMLDialogElement>(null);
  673. const closeRef = useRef(onClose);
  674. closeRef.current = onClose;
  675. useEffect(() => {
  676. const d = ref.current!;
  677. const prev = document.activeElement as HTMLElement;
  678. d.showModal();
  679. d.querySelector<HTMLInputElement>("input")?.focus();
  680. const original = document.body.style.overflow;
  681. document.body.style.overflow = "hidden";
  682. return () => {
  683. d.close();
  684. document.body.style.overflow = original;
  685. prev?.focus();
  686. };
  687. }, []);
  688. return (
  689. <dialog
  690. ref={ref}
  691. className={"modal " + (side ? "drawer" : "")}
  692. aria-label={title}
  693. onKeyDownCapture={(e) => {
  694. if (e.key === "Escape") {
  695. e.preventDefault();
  696. e.stopPropagation();
  697. closeRef.current();
  698. }
  699. }}
  700. onCancel={(e) => {
  701. e.preventDefault();
  702. closeRef.current();
  703. }}
  704. onClick={(e) => {
  705. if (e.target === ref.current) {
  706. const r = ref.current.getBoundingClientRect();
  707. if (
  708. e.clientX < r.left ||
  709. e.clientX > r.right ||
  710. e.clientY < r.top ||
  711. e.clientY > r.bottom
  712. )
  713. onClose();
  714. }
  715. }}
  716. >
  717. <div className="modal-heading">
  718. <h2>{title}</h2>
  719. <IconButton label={"Close " + title.toLowerCase()} onClick={onClose}>
  720. <X />
  721. </IconButton>
  722. </div>
  723. {children}
  724. </dialog>
  725. );
  726. }
  727. export function CartItems() {
  728. const { cart, update } = useStore();
  729. return (
  730. <div className="cart-items">
  731. {cart.map((line) => {
  732. const p = products.find((p) => p.id === line.id)!;
  733. return (
  734. <article className="cart-line" key={p.id}>
  735. <Link href={"/product/" + p.slug}>
  736. <Img src={p.images[0]} alt={p.name} width="100" height="110" />
  737. </Link>
  738. <div className="cart-line-content">
  739. <Link href={"/product/" + p.slug}>
  740. <h3>{p.name}</h3>
  741. </Link>
  742. <p>
  743. {p.pack} · {p.weight}
  744. </p>
  745. <QuantitySelector
  746. value={line.quantity}
  747. max={p.stock}
  748. onChange={(q) => update(p.id, q)}
  749. label={p.name}
  750. />
  751. </div>
  752. <div className="cart-line-price">
  753. <strong>{money(p.price * line.quantity)}</strong>
  754. <button
  755. className="text-button"
  756. onClick={() => update(p.id, 0)}
  757. aria-label={"Remove " + p.name}
  758. >
  759. Remove
  760. </button>
  761. </div>
  762. </article>
  763. );
  764. })}
  765. </div>
  766. );
  767. }
  768. export function Overlays() {
  769. const { overlay, setOverlay, cart, subtotal, count, toast } = useStore();
  770. return (
  771. <>
  772. {overlay === "menu" && (
  773. <Modal title="Explore ADUCKTIVE" onClose={() => setOverlay(null)} side>
  774. <nav className="mobile-nav" aria-label="Mobile navigation">
  775. {[
  776. ...nav,
  777. ["Wishlist", "/wishlist"],
  778. ["My account", "/account"],
  779. ].map(([title, path]) => title === "Shop" ? <ShopDropdown key={path} mobile onNavigate={() => setOverlay(null)} /> : (
  780. <Link key={path} href={path} onClick={() => setOverlay(null)}>
  781. {title}
  782. <ArrowUpRight />
  783. </Link>
  784. ))}
  785. </nav>
  786. <p className="menu-foot">A little crunch. A lot of joy.</p>
  787. </Modal>
  788. )}
  789. {overlay === "search" && <SearchOverlay />}
  790. {overlay === "cart" && (
  791. <Modal
  792. title={`Your bag (${count})`}
  793. side
  794. onClose={() => setOverlay(null)}
  795. >
  796. {cart.length ? (
  797. <>
  798. <div className="drawer-body">
  799. <p className="bag-message">A little happiness, ready to go.</p>
  800. <CartItems />
  801. </div>
  802. <div className="cart-summary">
  803. <div>
  804. <span>Subtotal</span>
  805. <strong>{money(subtotal)}</strong>
  806. </div>
  807. <p>
  808. Shipping and any discounts are confirmed by the official
  809. store.
  810. </p>
  811. <Button href="/checkout" onClick={() => setOverlay(null)}>
  812. Review & checkout
  813. <ArrowRight size={18} />
  814. </Button>
  815. <Link
  816. className="text-link"
  817. href="/cart"
  818. onClick={() => setOverlay(null)}
  819. >
  820. View your bag
  821. </Link>
  822. </div>
  823. </>
  824. ) : (
  825. <div
  826. onClick={(e) => {
  827. if ((e.target as HTMLElement).closest("a")) setOverlay(null);
  828. }}
  829. >
  830. <EmptyState
  831. title="Your bag is empty"
  832. copy="Good things start with a little crunch."
  833. />
  834. </div>
  835. )}
  836. </Modal>
  837. )}
  838. {toast && (
  839. <div className="toast" role="status">
  840. <Check size={18} />
  841. {toast}
  842. </div>
  843. )}
  844. </>
  845. );
  846. }
  847. function SearchOverlay() {
  848. const { setOverlay } = useStore();
  849. const [q, setQ] = useState("");
  850. const query = q.trim().toLowerCase();
  851. const results = products.filter((p) =>
  852. (p.name + " " + p.pack + " " + p.category).toLowerCase().includes(query),
  853. );
  854. const news = articles.filter((a) => a.title.toLowerCase().includes(query));
  855. return (
  856. <Modal title="Find your next favourite" onClose={() => setOverlay(null)}>
  857. <form
  858. className="search-form"
  859. onSubmit={(e) => {
  860. e.preventDefault();
  861. setOverlay(null);
  862. navigate("/shop?q=" + encodeURIComponent(q));
  863. }}
  864. >
  865. <Search size={22} />
  866. <label className="sr-only" htmlFor="search-query">
  867. Search snacks, collections and stories
  868. </label>
  869. <input
  870. id="search-query"
  871. type="search"
  872. value={q}
  873. onChange={(e) => setQ(e.target.value)}
  874. placeholder="Search snacks, collections, stories…"
  875. />
  876. <button
  877. type="submit"
  878. className="icon-button"
  879. aria-label="View all search results"
  880. >
  881. <ArrowRight />
  882. </button>
  883. </form>
  884. {!query && (
  885. <div className="popular">
  886. <p className="eyebrow">POPULAR SEARCHES</p>
  887. <div>
  888. {["Salted egg", "Mushroom", "Tempeh", "Fun pack"].map((x) => (
  889. <button key={x} onClick={() => setQ(x)}>
  890. {x}
  891. <ArrowUpRight size={14} />
  892. </button>
  893. ))}
  894. </div>
  895. </div>
  896. )}
  897. <p className="eyebrow">
  898. {query ? `${results.length} PRODUCT RESULTS` : "EXPLORE OUR SNACKS"}
  899. </p>
  900. <div className="search-results">
  901. {results.slice(0, 5).map((p) => (
  902. <Link
  903. href={"/product/" + p.slug}
  904. key={p.id}
  905. onClick={() => setOverlay(null)}
  906. >
  907. <Img src={p.images[0]} alt="" width="65" height="65" />
  908. <div>
  909. <strong>{p.name}</strong>
  910. <small>
  911. {p.pack} · {p.weight}
  912. </small>
  913. </div>
  914. <span>{money(p.price)}</span>
  915. <ArrowUpRight size={18} />
  916. </Link>
  917. ))}
  918. </div>
  919. {query &&
  920. categories
  921. .filter((c) => c.toLowerCase().includes(query))
  922. .map((c) => (
  923. <Link
  924. key={c}
  925. className="search-category"
  926. href={"/shop?category=" + encodeURIComponent(c)}
  927. onClick={() => setOverlay(null)}
  928. >
  929. Collection: {c}
  930. <ArrowUpRight size={16} />
  931. </Link>
  932. ))}
  933. {query &&
  934. news.slice(0, 3).map((a) => (
  935. <Link
  936. className="search-category"
  937. key={a.id}
  938. href={"/journal/" + a.id}
  939. onClick={() => setOverlay(null)}
  940. >
  941. Journal: {a.title}
  942. <ArrowUpRight size={16} />
  943. </Link>
  944. ))}
  945. {query && !results.length && !news.length && (
  946. <p className="muted">
  947. No matches yet. Try “cornflakes”, “mushroom” or “tempeh”.
  948. </p>
  949. )}
  950. </Modal>
  951. );
  952. }
  953. export function SocialGallery() {
  954. const imgs = [
  955. "/images/snack-23-2.webp",
  956. "/images/snack-26-2.webp",
  957. "/images/snack-22-3.webp",
  958. "/images/snack-130-2.webp",
  959. "/images/snack-25-2.webp",
  960. "/images/snack-145-1.webp",
  961. ];
  962. return (
  963. <section className="section social-section">
  964. <SectionHeading
  965. eyebrow="GOOD SNACKS BRING PEOPLE TOGETHER"
  966. title="A seriously ADUCKTIVE world."
  967. link={social[1]}
  968. label="@aducktive.saltedegg.hq"
  969. />
  970. <div className="social-grid">
  971. {imgs.map((src, i) => (
  972. <a
  973. key={src}
  974. href={social[1]}
  975. target="_blank"
  976. rel="noreferrer"
  977. aria-label={"Follow ADUCKTIVE on Instagram, photo " + (i + 1)}
  978. >
  979. <Img
  980. src={src}
  981. alt={`ADUCKTIVE ${["cornflakes", "mushroom crisps", "salted egg snacks", "tempeh chips", "original mushroom crisps", "party pack"][i]}`}
  982. loading="lazy"
  983. width="300"
  984. height="300"
  985. />
  986. <Instagram size={20} />
  987. </a>
  988. ))}
  989. </div>
  990. <p className="muted">
  991. Follow us for the latest products, updates, and a little snack
  992. inspiration.
  993. </p>
  994. </section>
  995. );
  996. }