packbook.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use client";
  2. import Image from "next/image";
  3. import { useEffect, useRef, useState } from "react";
  4. import { Eyebrow, TextLink } from "./ui";
  5. import buy from "@/assets/packbook/buy.webp";
  6. import identify from "@/assets/packbook/identify.webp";
  7. import pack from "@/assets/packbook/pack.webp";
  8. import share from "@/assets/packbook/share.webp";
  9. import wall from "@/assets/packbook/wall.webp";
  10. import { projects } from "@/config/site";
  11. import { cn } from "@/lib/utils";
  12. const SCREENS = [
  13. {
  14. src: wall,
  15. label: "Snap",
  16. alt: "PackBook gear wall with die-cut gear photos",
  17. },
  18. {
  19. src: identify,
  20. label: "Identify",
  21. alt: "PackBook identifying a Petzl GRIGRI",
  22. },
  23. {
  24. src: pack,
  25. label: "Pack",
  26. alt: "PackBook packing list with base weight by category",
  27. },
  28. { src: share, label: "Share", alt: "PackBook share card for a trip" },
  29. {
  30. src: buy,
  31. label: "Shop",
  32. alt: "PackBook gear detail with where-to-buy prices",
  33. },
  34. ];
  35. export function PackBookCard() {
  36. const [index, setIndex] = useState(0);
  37. const [hovering, setHovering] = useState(false);
  38. const [loaded, setLoaded] = useState(1);
  39. const rootRef = useRef<HTMLDivElement>(null);
  40. const go = (i: number) => {
  41. setLoaded((n) => Math.max(n, i + 2));
  42. setIndex(i);
  43. };
  44. // Flip through the app while the card is hovered.
  45. useEffect(() => {
  46. if (!hovering) return;
  47. const id = window.setInterval(
  48. () =>
  49. setIndex((i) => {
  50. const next = (i + 1) % SCREENS.length;
  51. setLoaded((n) => Math.max(n, next + 2));
  52. return next;
  53. }),
  54. 1800,
  55. );
  56. return () => window.clearInterval(id);
  57. }, [hovering]);
  58. return (
  59. <div
  60. ref={rootRef}
  61. className="flex h-full gap-4 p-5 max-sm:flex-row sm:flex-col sm:p-6"
  62. onPointerEnter={(e) => {
  63. if (e.pointerType !== "touch") {
  64. setLoaded((n) => Math.max(n, 3));
  65. setHovering(true);
  66. }
  67. }}
  68. onPointerLeave={() => setHovering(false)}
  69. >
  70. <div className="flex min-w-0 flex-col max-sm:flex-1 max-sm:justify-between">
  71. <div>
  72. <Eyebrow>Side project · iOS</Eyebrow>
  73. <h2 className="title mt-1 text-[24px]">PackBook</h2>
  74. <p className="mt-1.5 text-[15px] leading-snug text-muted">
  75. Snap your gear. Pack it. Share it.
  76. </p>
  77. </div>
  78. <div className="mt-3 flex flex-wrap gap-1 sm:hidden">
  79. {SCREENS.map((s, i) => (
  80. <button
  81. key={s.label}
  82. className={cn(
  83. "rounded-full border px-2 py-0.5 text-[11px] font-medium",
  84. i === index
  85. ? "border-glacier text-ink"
  86. : "border-line text-muted",
  87. )}
  88. type="button"
  89. onClick={() => go(i)}
  90. >
  91. {s.label}
  92. </button>
  93. ))}
  94. </div>
  95. <TextLink className="mt-3 sm:hidden" href={projects.packbook.href}>
  96. {projects.packbook.linkLabel}
  97. </TextLink>
  98. </div>
  99. <button
  100. aria-label="Next PackBook screen"
  101. className="relative mx-auto aspect-[720/1564] h-full max-h-full min-h-0 shrink-0 rounded-[1.6rem] bg-[#0b0b0c] p-[5px] shadow-[0_24px_48px_-24px_rgb(0_0_0/0.6)] transition-transform duration-500 ease-out [transform:perspective(900px)_rotateY(calc(var(--tx,0)*-12deg))_rotateX(calc(var(--ty,0)*8deg))] sm:min-h-0 sm:flex-1"
  102. data-cursor="Next screen"
  103. type="button"
  104. onClick={() => go((index + 1) % SCREENS.length)}
  105. >
  106. <span className="relative block size-full overflow-hidden rounded-[1.25rem] bg-[#f4f0e8]">
  107. {SCREENS.slice(0, loaded).map((s, i) => (
  108. <Image
  109. key={s.label}
  110. fill
  111. alt={s.alt}
  112. className={cn(
  113. "object-cover object-top transition-[opacity,scale] duration-500",
  114. i === index
  115. ? "scale-100 opacity-100"
  116. : "scale-[1.04] opacity-0",
  117. )}
  118. sizes="(max-width: 640px) 160px, 220px"
  119. src={s.src}
  120. />
  121. ))}
  122. </span>
  123. </button>
  124. <div className="hidden items-center justify-between sm:flex">
  125. <div className="-ml-[9px] flex">
  126. {SCREENS.map((s, i) => (
  127. <button
  128. key={s.label}
  129. aria-label={`Show ${s.label}`}
  130. className="group/dot grid size-6 place-items-center"
  131. type="button"
  132. onClick={() => go(i)}
  133. >
  134. <span
  135. className={cn(
  136. "h-1.5 rounded-full transition-all duration-500",
  137. i === index
  138. ? "w-4 bg-glacier"
  139. : "w-1.5 bg-line-strong group-hover/dot:bg-muted",
  140. )}
  141. />
  142. </button>
  143. ))}
  144. </div>
  145. <TextLink href={projects.packbook.href}>
  146. {projects.packbook.linkLabel}
  147. </TextLink>
  148. </div>
  149. </div>
  150. );
  151. }