avatar-flip.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use client";
  2. import Image, { getImageProps } from "next/image";
  3. import {
  4. useCallback,
  5. useEffect,
  6. useRef,
  7. useState,
  8. type CSSProperties,
  9. } from "react";
  10. import { useT } from "@/components/locale-provider";
  11. import { cn, onIdle } from "@/lib/utils";
  12. /**
  13. * Each face is hidden outright while it's turned away, switching as the flip
  14. * passes edge-on (114 ms into the spring below). backface-visibility alone isn't
  15. * enough: the theme switch's snapshot ignores it, which showed Yilu.
  16. */
  17. const face = (shown: boolean): CSSProperties => ({
  18. visibility: shown ? "visible" : "hidden",
  19. transition: "visibility 0s linear 114ms",
  20. });
  21. /** Tap the avatar to flip it over and meet Yilu, the cat. */
  22. export function AvatarFlip({
  23. avatarUrl,
  24. dogUrl,
  25. }: {
  26. avatarUrl?: string;
  27. dogUrl?: string;
  28. }) {
  29. const { avatar } = useT();
  30. const [flipped, setFlipped] = useState(false);
  31. // Yilu is only in the page while showing or turning away, so nothing
  32. // (a theme switch, a browser's snapshot of the page) can paint the cat over
  33. // the owner's photo. `dogIn` / `dogUrl` are legacy names kept for callers.
  34. const [dogIn, setDogIn] = useState(false);
  35. const warmed = useRef(false);
  36. // Warms the cat photo at the size the flip asks for.
  37. const warm = useCallback(() => {
  38. if (warmed.current || !dogUrl) return;
  39. warmed.current = true;
  40. const { props } = getImageProps({
  41. alt: "",
  42. fill: true,
  43. sizes: "60px",
  44. src: dogUrl,
  45. });
  46. const img = new window.Image();
  47. img.sizes = props.sizes ?? "";
  48. img.srcset = props.srcSet ?? "";
  49. img.src = props.src;
  50. }, [dogUrl]);
  51. useEffect(() => {
  52. const meet = () => {
  53. setDogIn(true);
  54. setFlipped(true);
  55. };
  56. const cancelIdle = onIdle(warm, 5000);
  57. window.addEventListener("eric:bert", meet);
  58. return () => {
  59. window.removeEventListener("eric:bert", meet);
  60. cancelIdle();
  61. };
  62. }, [warm]);
  63. // Once Yilu has turned away (the face hides 114 ms in), take it out.
  64. useEffect(() => {
  65. if (flipped) return;
  66. const id = setTimeout(() => setDogIn(false), 250);
  67. return () => clearTimeout(id);
  68. }, [flipped]);
  69. return (
  70. <button
  71. aria-label={flipped ? avatar.showEric : avatar.meetYilu}
  72. aria-pressed={flipped}
  73. className="group relative size-14 shrink-0 rounded-full [perspective:700px] sm:size-[60px]"
  74. data-cursor={flipped ? avatar.backCursor : avatar.meetCursor}
  75. type="button"
  76. onClick={() => {
  77. if (!flipped) setDogIn(true);
  78. setFlipped(!flipped);
  79. }}
  80. onFocus={warm}
  81. onPointerEnter={warm}
  82. >
  83. <span
  84. className="relative block size-full transition-transform duration-[900ms] [transform-style:preserve-3d] [transition-timing-function:cubic-bezier(.3,1.35,.45,1)] group-active:scale-95"
  85. data-flipper
  86. style={{ transform: flipped ? "rotateY(180deg)" : undefined }}
  87. >
  88. <span
  89. className="absolute inset-0 overflow-hidden rounded-full shadow-[0_8px_24px_-10px_rgb(0_0_0/0.45)] ring-1 ring-line [backface-visibility:hidden]"
  90. data-face
  91. style={face(!flipped)}
  92. >
  93. {avatarUrl && (
  94. <Image
  95. fill
  96. preload
  97. alt={avatar.eric}
  98. className="object-cover"
  99. sizes="60px"
  100. src={avatarUrl}
  101. />
  102. )}
  103. </span>
  104. <span
  105. className="absolute inset-0 overflow-hidden rounded-full bg-card-2 ring-1 ring-line [backface-visibility:hidden] [transform:rotateY(180deg)]"
  106. data-face
  107. style={face(flipped)}
  108. >
  109. {dogIn && dogUrl && (
  110. <Image
  111. fill
  112. alt={avatar.bert}
  113. className="object-cover"
  114. sizes="60px"
  115. src={dogUrl}
  116. // Both photos are 3:4 portraits. Centring would crop Yilu's
  117. // face off the top, so bias the square crop toward it.
  118. style={{ objectPosition: "center 18%" }}
  119. />
  120. )}
  121. </span>
  122. </span>
  123. <span
  124. aria-hidden
  125. className={cn(
  126. "lg absolute -right-1 -bottom-0.5 grid size-6 place-items-center rounded-full text-[11px] transition-transform duration-500 group-hover:rotate-12",
  127. )}
  128. >
  129. <span className="lg-caustic" />
  130. <span>{flipped ? "👋" : "🐱"}</span>
  131. </span>
  132. </button>
  133. );
  134. }