All files / src/components LivingBodyHero.tsx

91.66% Statements 22/24
88.88% Branches 8/9
85.71% Functions 6/7
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 572x                             26x   26x 26x     26x         4x 4x 8x   4x 4x 4x   4x   4x 4x 4x                 4x 4x 4x 4x       26x    
"use client";
 
import { useEffect, useRef } from "react";
import type {
  LivingBodyInstance,
  LivingBodyTheme,
} from "@/lib/living-body/living-body";
 
/**
 * The start page's hero animation: the body as a network of organs, walking in
 * place, drawn on a canvas by the vendored living-body library. It loads on the
 * client only, in its own chunk, after the page has painted, and pauses while
 * scrolled out of view. The canvas carries its own aria-label.
 */
export function LivingBodyHero({ className = "" }: { className?: string }) {
  const ref = useRef<HTMLDivElement>(null);
 
  useEffect(() => {
    const el = ref.current;
    // jsdom, where the unit tests render this page, has no matchMedia and no
    // 2D canvas; the library needs both.
    if (!el || typeof window.matchMedia !== "function") return;
 
    // The UI marks dark mode with the "dark" class on <html> and nothing else.
    // Left on "auto", the library would read a missing class as "follow the
    // OS" and draw a dark figure on the light page for anyone with a dark OS.
    const root = document.documentElement;
    const theme = (): LivingBodyTheme =>
      root.classList.contains("dark") ? "dark" : "light";
 
    let body: LivingBodyInstance | null = null;
    let cancelled = false;
    const observer = new MutationObserver(() => body?.setTheme(theme()));
 
    import("@/lib/living-body/living-body")
      .then(({ mount }) => {
        Iif (cancelled) return;
        body = mount(el, { theme: theme() });
        observer.observe(root, {
          attributes: true,
          attributeFilter: ["class"],
        });
      })
      .catch((err) => {
        console.warn("Living body animation did not start:", err);
      });
 
    return () => {
      cancelled = true;
      observer.disconnect();
      body?.destroy();
    };
  }, []);
 
  return <div ref={ref} className={className} />;
}