{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "heroes-12",
  "title": "Hero with a drawn agent",
  "description": "A hero for a creator's home page with a drawn agent whose eyes follow the pointer and blink, beside a headline whose last words type, hold, erase and give way to the next phrase.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/demonstration.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-agent-run.json",
    "https://ui.ndekocode.dev/r/use-autoplay.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json",
    "https://ui.ndekocode.dev/r/wash.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/blocks/heroes/heroes-12/index.tsx",
      "content": "'use client';\n\nimport { useMemo, useState } from 'react';\n\nimport {\n  type AutoplayStep,\n  typingSteps,\n  useAutoplay,\n} from '@/registry/ndeko/hooks/use-autoplay';\nimport { keyDelayOf, prefixesOf } from '@/registry/ndeko/lib/demonstration';\nimport { ArrowRightIcon } from '@/registry/ndeko/lib/icons';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport { Wash } from '@/registry/ndeko/ui/wash';\n\nimport { AgentFace } from './agent-face';\nimport { HERO, INITIAL_VIEW, PHRASES, type PhraseView } from './data';\nimport { TypedPhrase } from './typed-phrase';\n\nconst HOLD_MS = 1800;\nconst ERASE_MS = 22;\nconst BEFORE_TYPING_MS = 350;\nconst TYPE_MS = 55;\n\nconst FACE = <AgentFace />;\n\nconst SPOKEN_PHRASES = PHRASES.join(', ');\n\ntype ShowView = (view: PhraseView) => void;\n\nfunction erasingSteps(index: number, show: ShowView): AutoplayStep[] {\n  const prefixes = prefixesOf(PHRASES[index]);\n  const shorter = prefixes.slice(0, -1);\n  const remaining = [...shorter.reverse(), ''];\n  return remaining.map((typed, position) => {\n    const after = keyDelayOf(position, HOLD_MS, ERASE_MS);\n    return { after, run: () => show({ index, typed }) };\n  });\n}\n\nfunction writingSteps(index: number, show: ShowView): AutoplayStep[] {\n  const options = { startAfter: BEFORE_TYPING_MS, characterMs: TYPE_MS };\n  return typingSteps(\n    PHRASES[index],\n    (typed) => show({ index, typed }),\n    options\n  );\n}\n\n/** Each phrase is erased and the next one typed; the last one types the first again, so a round ends where it began. */\nfunction phraseSteps(show: ShowView): AutoplayStep[] {\n  const rounds = PHRASES.map((_, index) => {\n    const next = (index + 1) % PHRASES.length;\n    return [...erasingSteps(index, show), ...writingSteps(next, show)];\n  });\n  return rounds.flat();\n}\n\nfunction shownTextOf(isAutoplaying: boolean, typed: string, phrase: string) {\n  if (isAutoplaying) {\n    return typed;\n  }\n  return phrase;\n}\n\nexport default function Heroes12() {\n  const [view, setView] = useState<PhraseView>(INITIAL_VIEW);\n\n  function startOver() {\n    setView(INITIAL_VIEW);\n  }\n\n  const steps = useMemo(() => phraseSteps(setView), []);\n  const { ref, isInView, isAutoplaying } = useAutoplay(steps, {\n    loopAfter: 0,\n    onReset: startOver,\n  });\n  const phrase = PHRASES[view.index];\n  const typed = shownTextOf(isAutoplaying, view.typed, phrase);\n\n  return (\n    <section className='@container'>\n      <div ref={ref} className='relative isolate'>\n        <Wash className='h-[26rem]' />\n        <div className='mx-auto grid max-w-5xl items-center gap-8 px-4 py-16 @2xl:px-6 @2xl:py-24 @3xl:grid-cols-[auto_minmax(0,1fr)] @3xl:gap-14'>\n          {FACE}\n          <div className='flex min-w-0 flex-col items-start gap-6'>\n            <h1 className='text-4xl font-semibold text-balance @2xl:text-5xl @4xl:text-6xl'>\n              {HERO.titleStart}{' '}\n              <span className='sr-only'>{SPOKEN_PHRASES}</span>\n              <span className='block'>\n                <TypedPhrase\n                  phrase={phrase}\n                  typed={typed}\n                  hasCaret={isAutoplaying}\n                  isLive={isInView}\n                />\n              </span>\n            </h1>\n            <p className='text-muted-foreground max-w-xl text-base text-pretty'>\n              {HERO.description}\n            </p>\n            <div className='flex flex-wrap gap-2'>\n              <Button size='lg' nativeButton={false} render={<a href='#' />}>\n                {HERO.primaryAction}\n                <ArrowRightIcon />\n              </Button>\n              <Button\n                size='lg'\n                variant='outline'\n                nativeButton={false}\n                render={<a href='#' />}\n              >\n                {HERO.secondaryAction}\n              </Button>\n            </div>\n          </div>\n        </div>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/blocks/heroes-12/index.tsx"
    },
    {
      "path": "registry/ndeko/blocks/heroes/heroes-12/agent-face.tsx",
      "content": "'use client';\n\nimport { useInView } from 'motion/react';\nimport { useRef } from 'react';\n\nimport { useDocumentVisible } from '@/registry/ndeko/hooks/use-agent-run';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\n\nimport { HERO } from './data';\nimport { useGaze } from './use-gaze';\n\n/** The stroke weight of the icon module, kept at the same width however large the drawing is shown. */\nconst STROKE_WIDTH = 1.75;\n\nconst OUTLINE = 'stroke-foreground/70';\n\nconst PLATE = 'fill-card stroke-foreground/70';\n\nconst VISOR = 'fill-muted stroke-foreground/70';\n\nconst EYE = 'fill-foreground origin-center [transform-box:fill-box]';\n\nconst EYES_FOLLOW_GAZE =\n  'transition-transform duration-150 ease-out [transform:translate(calc(var(--gaze-x,0)*1px),calc(var(--gaze-y,0)*1px))]';\n\nexport function AgentFace() {\n  const ref = useRef<SVGSVGElement>(null);\n  const isInView = useInView(ref, { amount: 0.3 });\n  const isVisible = useDocumentVisible();\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const isOnScreen = isInView && isVisible;\n  const isActive = isOnScreen && !shouldReduceMotion;\n\n  useGaze(ref, isActive);\n\n  return (\n    <div className='flex flex-col items-start gap-3'>\n      <div className='bg-foreground/[0.04] size-32 rounded-2xl p-4 @3xl:size-40'>\n        <svg\n          ref={ref}\n          viewBox='0 0 96 96'\n          fill='none'\n          strokeWidth={STROKE_WIDTH}\n          strokeLinecap='round'\n          strokeLinejoin='round'\n          aria-hidden='true'\n          className='size-full **:[vector-effect:non-scaling-stroke]'\n        >\n          <path d='M48 20V12' className={OUTLINE} />\n          <circle\n            cx='48'\n            cy='9'\n            r='3.5'\n            className='fill-primary stroke-primary'\n          />\n          <rect x='7' y='41' width='7' height='18' rx='3.5' className={PLATE} />\n          <rect\n            x='82'\n            y='41'\n            width='7'\n            height='18'\n            rx='3.5'\n            className={PLATE}\n          />\n          <rect\n            x='14'\n            y='20'\n            width='68'\n            height='58'\n            rx='20'\n            className={PLATE}\n          />\n          <rect\n            x='24'\n            y='36'\n            width='48'\n            height='24'\n            rx='12'\n            className={VISOR}\n          />\n          <g className={EYES_FOLLOW_GAZE}>\n            <rect\n              data-eye=''\n              x='38'\n              y='42'\n              width='6'\n              height='12'\n              rx='3'\n              className={EYE}\n            />\n            <rect\n              data-eye=''\n              x='52'\n              y='42'\n              width='6'\n              height='12'\n              rx='3'\n              className={EYE}\n            />\n          </g>\n          <path d='M41 68q7 4.5 14 0' className={OUTLINE} />\n        </svg>\n      </div>\n      <div className='flex flex-col'>\n        <p className='text-[13px] font-medium'>{HERO.agentName}</p>\n        <p className='text-muted-foreground text-xs'>{HERO.agentRole}</p>\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/blocks/heroes-12/agent-face.tsx"
    },
    {
      "path": "registry/ndeko/blocks/heroes/heroes-12/use-gaze.ts",
      "content": "'use client';\n\nimport { type RefObject, useEffect } from 'react';\n\nimport { type Gaze, GLANCES } from './data';\n\n/** How far the eyes travel from the middle, in the units of the drawing: about three pixels on screen. */\nconst MAX_SHIFT = 2;\n\n/** A pointer this far from the face makes the eyes look as far as they go. */\nconst FULL_LOOK_PX = 220;\n\nconst GLANCE_EVERY_MS = 1900;\n\nconst BLINK_GAPS_MS = [3200, 2400, 4200, 2900];\n\nconst BLINK_MS = 160;\n\nconst BLINK_FRAMES = [\n  { transform: 'scaleY(1)' },\n  { transform: 'scaleY(0.1)' },\n  { transform: 'scaleY(1)' },\n];\n\nconst CENTRE: Gaze = { x: 0, y: 0 };\n\nfunction gazeToward(dx: number, dy: number): Gaze {\n  const distance = Math.hypot(dx, dy);\n  if (distance === 0) {\n    return CENTRE;\n  }\n  const strength = Math.min(1, distance / FULL_LOOK_PX);\n  const unitX = dx / distance;\n  const unitY = dy / distance;\n  return { x: unitX * strength, y: unitY * strength };\n}\n\n/**\n * Turns the eyes of the drawing toward the pointer while it is active. With no pointer they\n * glance about on their own, and they blink now and then. The look is written to two custom\n * properties of the drawing, never through state, so a moving pointer renders nothing.\n */\nexport function useGaze(\n  faceRef: RefObject<SVGSVGElement | null>,\n  isActive: boolean\n) {\n  useEffect(() => {\n    const face = faceRef.current;\n    if (!face || !isActive) {\n      return;\n    }\n    const drawing: SVGSVGElement = face;\n    let isFollowing = false;\n    let frame = 0;\n    let pointerX = 0;\n    let pointerY = 0;\n    let glanceIndex = 0;\n    let blinkIndex = 0;\n    let glanceTimer: ReturnType<typeof setTimeout> | undefined;\n    let blinkTimer: ReturnType<typeof setTimeout> | undefined;\n\n    function look(gaze: Gaze) {\n      const shiftX = gaze.x * MAX_SHIFT;\n      const shiftY = gaze.y * MAX_SHIFT;\n      drawing.style.setProperty('--gaze-x', String(shiftX));\n      drawing.style.setProperty('--gaze-y', String(shiftY));\n    }\n\n    function followPointer() {\n      frame = 0;\n      const box = drawing.getBoundingClientRect();\n      const centreX = box.left + box.width / 2;\n      const centreY = box.top + box.height / 2;\n      const dx = pointerX - centreX;\n      const dy = pointerY - centreY;\n      look(gazeToward(dx, dy));\n    }\n\n    function onPointerMove(event: PointerEvent) {\n      isFollowing = true;\n      pointerX = event.clientX;\n      pointerY = event.clientY;\n      if (frame === 0) {\n        frame = requestAnimationFrame(followPointer);\n      }\n    }\n\n    function onPointerLeave() {\n      isFollowing = false;\n      look(CENTRE);\n    }\n\n    function glanceAbout() {\n      glanceTimer = setTimeout(() => {\n        if (!isFollowing) {\n          const position = glanceIndex % GLANCES.length;\n          look(GLANCES[position]);\n          glanceIndex += 1;\n        }\n        glanceAbout();\n      }, GLANCE_EVERY_MS);\n    }\n\n    function closeEyes() {\n      const eyes = drawing.querySelectorAll('[data-eye]');\n      eyes.forEach((eye) => eye.animate(BLINK_FRAMES, { duration: BLINK_MS }));\n    }\n\n    function blinkSometimes() {\n      const position = blinkIndex % BLINK_GAPS_MS.length;\n      const gap = BLINK_GAPS_MS[position];\n      blinkIndex += 1;\n      blinkTimer = setTimeout(() => {\n        closeEyes();\n        blinkSometimes();\n      }, gap);\n    }\n\n    window.addEventListener('pointermove', onPointerMove, { passive: true });\n    document.documentElement.addEventListener('pointerleave', onPointerLeave);\n    glanceAbout();\n    blinkSometimes();\n\n    return () => {\n      window.removeEventListener('pointermove', onPointerMove);\n      document.documentElement.removeEventListener(\n        'pointerleave',\n        onPointerLeave\n      );\n      cancelAnimationFrame(frame);\n      clearTimeout(glanceTimer);\n      clearTimeout(blinkTimer);\n      look(CENTRE);\n    };\n  }, [faceRef, isActive]);\n}\n",
      "type": "registry:component",
      "target": "components/blocks/heroes-12/use-gaze.ts"
    },
    {
      "path": "registry/ndeko/blocks/heroes/heroes-12/typed-phrase.tsx",
      "content": "import { cn } from '@/registry/ndeko/lib/utils';\n\nimport { PHRASES } from './data';\n\nfunction caretClassOf(isLive: boolean) {\n  if (isLive) {\n    return 'animate-pulse';\n  }\n  return undefined;\n}\n\ntype TypedPhraseProps = {\n  phrase: string;\n  typed: string;\n  hasCaret: boolean;\n  /** The caret only pulses while the hero is on screen. */\n  isLive: boolean;\n};\n\n/**\n * Every phrase sits invisible in one grid cell, so the block always has the width and height of\n * the longest one, and the live phrase is laid out whole with the part not typed yet hidden, so\n * its lines break where they will break once it is typed. The caret takes no width.\n */\nexport function TypedPhrase({\n  phrase,\n  typed,\n  hasCaret,\n  isLive,\n}: TypedPhraseProps) {\n  const rest = phrase.slice(typed.length);\n\n  return (\n    <span className='text-primary-ink inline-grid text-start'>\n      {PHRASES.map((candidate) => (\n        <span\n          key={candidate}\n          aria-hidden='true'\n          className='invisible col-start-1 row-start-1'\n        >\n          {candidate}\n        </span>\n      ))}\n      <span aria-hidden='true' className='col-start-1 row-start-1'>\n        {typed}\n        {hasCaret && (\n          <span className='relative inline-block w-0 align-baseline'>\n            <span\n              className={cn(\n                'absolute start-0 top-[0.14em] h-[0.9em] w-[0.06em] rounded-full bg-current',\n                caretClassOf(isLive)\n              )}\n            />\n          </span>\n        )}\n        <span className='invisible'>{rest}</span>\n      </span>\n    </span>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/blocks/heroes-12/typed-phrase.tsx"
    },
    {
      "path": "registry/ndeko/blocks/heroes/heroes-12/data.ts",
      "content": "export type Gaze = {\n  /** From -1 (far left) to 1 (far right). */\n  x: number;\n  /** From -1 (up) to 1 (down). */\n  y: number;\n};\n\nexport type PhraseView = {\n  index: number;\n  typed: string;\n};\n\nexport const HERO = {\n  titleStart: \"Hi, I'm Ida. I make\",\n  description:\n    'Pip is my assistant. Ask it about my work, my rates, or when I am free to start something with you.',\n  primaryAction: 'Ask Pip',\n  secondaryAction: 'See the work',\n  agentName: 'Pip',\n  agentRole: 'My assistant',\n};\n\nexport const PHRASES = [\n  'short films about rivers',\n  'zines for night owls',\n  'small tools for writers',\n  'podcasts about maps',\n];\n\nexport const INITIAL_VIEW: PhraseView = { index: 0, typed: PHRASES[0] };\n\nexport const GLANCES: Gaze[] = [\n  { x: 1, y: -0.3 },\n  { x: 0.7, y: 0.9 },\n  { x: 0, y: 0 },\n  { x: -0.8, y: 0.2 },\n  { x: 0.2, y: -0.9 },\n  { x: 0, y: 0 },\n];\n",
      "type": "registry:component",
      "target": "components/blocks/heroes-12/data.ts"
    }
  ],
  "categories": [
    "blocks",
    "heroes"
  ],
  "type": "registry:block"
}