avatar-flip.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 { cn, onIdle } from "@/lib/utils";
  11. /**
  12. * Each face is hidden outright while it's turned away, switching as the flip
  13. * passes edge-on (114 ms into the spring below). backface-visibility alone isn't
  14. * enough: the theme switch's snapshot ignores it, which showed Bert.
  15. */
  16. const face = (shown: boolean): CSSProperties => ({
  17. visibility: shown ? "visible" : "hidden",
  18. transition: "visibility 0s linear 114ms",
  19. });
  20. /** Tap the avatar to flip it over and meet Bert. */
  21. export function AvatarFlip({
  22. avatarUrl,
  23. dogUrl,
  24. }: {
  25. avatarUrl: string;
  26. dogUrl: string;
  27. }) {
  28. const [flipped, setFlipped] = useState(false);
  29. // Bert is only in the page while he's showing or turning away, so nothing
  30. // (a theme switch, a browser's snapshot of the page) can paint him over Eric.
  31. const [dogIn, setDogIn] = useState(false);
  32. const warmed = useRef(false);
  33. // Fetches Bert at the size his face will ask for, so he's there on the flip.
  34. const warm = useCallback(() => {
  35. if (warmed.current) return;
  36. warmed.current = true;
  37. const { props } = getImageProps({
  38. alt: "",
  39. fill: true,
  40. sizes: "60px",
  41. src: dogUrl,
  42. });
  43. const img = new window.Image();
  44. img.sizes = props.sizes ?? "";
  45. img.srcset = props.srcSet ?? "";
  46. img.src = props.src;
  47. }, [dogUrl]);
  48. useEffect(() => {
  49. const meet = () => {
  50. setDogIn(true);
  51. setFlipped(true);
  52. };
  53. const cancelIdle = onIdle(warm, 5000);
  54. window.addEventListener("eric:bert", meet);
  55. return () => {
  56. window.removeEventListener("eric:bert", meet);
  57. cancelIdle();
  58. };
  59. }, [warm]);
  60. // Once Bert has turned away (his face hides 114 ms in), take him out.
  61. useEffect(() => {
  62. if (flipped) return;
  63. const id = setTimeout(() => setDogIn(false), 250);
  64. return () => clearTimeout(id);
  65. }, [flipped]);
  66. return (
  67. <button
  68. aria-label={flipped ? "Show Eric again" : "Flip to meet Bert, my dog"}
  69. aria-pressed={flipped}
  70. className="group relative size-14 shrink-0 rounded-full [perspective:700px] sm:size-[60px]"
  71. data-cursor={flipped ? "Back to Eric" : "Meet Bert"}
  72. type="button"
  73. onClick={() => {
  74. if (!flipped) setDogIn(true);
  75. setFlipped(!flipped);
  76. }}
  77. onFocus={warm}
  78. onPointerEnter={warm}
  79. >
  80. <span
  81. 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"
  82. data-flipper
  83. style={{ transform: flipped ? "rotateY(180deg)" : undefined }}
  84. >
  85. <span
  86. 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]"
  87. data-face
  88. style={face(!flipped)}
  89. >
  90. <Image
  91. fill
  92. preload
  93. alt="Eric Wu"
  94. className="object-cover"
  95. sizes="60px"
  96. src={avatarUrl}
  97. />
  98. </span>
  99. <span
  100. className="absolute inset-0 overflow-hidden rounded-full bg-card-2 ring-1 ring-line [backface-visibility:hidden] [transform:rotateY(180deg)]"
  101. data-face
  102. style={face(flipped)}
  103. >
  104. {dogIn && (
  105. <Image
  106. fill
  107. alt="Bert, Eric's dog"
  108. className="object-cover"
  109. sizes="60px"
  110. src={dogUrl}
  111. />
  112. )}
  113. </span>
  114. </span>
  115. <span
  116. aria-hidden
  117. className={cn(
  118. "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",
  119. )}
  120. >
  121. <span className="lg-caustic" />
  122. <span>{flipped ? "👋" : "🐶"}</span>
  123. </span>
  124. </button>
  125. );
  126. }