{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "card-05",
  "title": "Story quote",
  "description": "A quote card that plays like a story, where the segments along the bottom fill one after another before the next quote fades in, a tap on either side steps and Space pauses.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/avatar.json",
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/chat-motion.json",
    "https://ui.ndekocode.dev/r/company-mark.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/live-region.json",
    "https://ui.ndekocode.dev/r/motion-presets.json",
    "https://ui.ndekocode.dev/r/recipes.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-agent-run.json",
    "https://ui.ndekocode.dev/r/use-kept-height.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/use-timeouts.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/card/card-05.tsx",
      "content": "'use client';\n\nimport { AnimatePresence, motion, useInView } from 'motion/react';\nimport { useEffect, useId, useRef, useState, type CSSProperties } from 'react';\n\nimport { useDocumentVisible } from '@/registry/ndeko/hooks/use-agent-run';\nimport { useKeptHeight } from '@/registry/ndeko/hooks/use-kept-height';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport {\n  ChevronLeftIcon,\n  ChevronRightIcon,\n  PauseIcon,\n  PlayIcon,\n  TextQuoteIcon,\n} from '@/registry/ndeko/lib/icons';\nimport { risePresenceOf } from '@/registry/ndeko/lib/motion-presets';\nimport { FOCUS_RING } from '@/registry/ndeko/lib/recipes';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { Avatar } from '@/registry/ndeko/ui/avatar';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport { Swap } from '@/registry/ndeko/ui/chat-motion';\nimport { CompanyMark } from '@/registry/ndeko/ui/company-mark';\nimport { LiveRegion } from '@/registry/ndeko/ui/live-region';\n\nimport { wrapIndex } from './_parts/deck';\nimport { isKeyboardArrival, isOnControl, sideStepOf } from './_parts/keys';\nimport { PEOPLE } from './_parts/people';\nimport { SampleNote } from './_parts/sample-note';\n\nexport type StoryQuote = {\n  id: string;\n  text: string;\n  author: string;\n  role: string;\n  company: string;\n};\n\nconst DEFAULT_DURATION_MS = 6000;\n\nconst FILL_KEYFRAMES = [{ transform: 'scaleX(0)' }, { transform: 'scaleX(1)' }];\n\nconst EMPTY: CSSProperties = { transform: 'scaleX(0)' };\n\nconst FILL = 'bg-foreground block h-full origin-left rounded-full';\n\nconst SIDE_HINT = [\n  'text-muted-foreground pointer-events-none absolute inset-y-0 flex items-center opacity-0',\n  'transition-opacity duration-150 group-hover/story:opacity-100 group-focus-visible/story:opacity-100',\n].join(' ');\n\ntype ActiveFillProps = {\n  durationMs: number;\n  isRunning: boolean;\n  onDone: () => void;\n};\n\n/**\n * The segment of the quote on screen. The browser fills it by itself, so the clock of the story\n * costs no render: it is paused and resumed where it stopped, and it says when it is full.\n */\nfunction ActiveFill({ durationMs, isRunning, onDone }: ActiveFillProps) {\n  const barRef = useRef<HTMLSpanElement>(null);\n  const fillRef = useRef<Animation | null>(null);\n  const onDoneRef = useRef(onDone);\n\n  useEffect(() => {\n    onDoneRef.current = onDone;\n  });\n\n  useEffect(() => {\n    const bar = barRef.current;\n    if (!bar) {\n      return;\n    }\n    const fill = bar.animate(FILL_KEYFRAMES, {\n      duration: durationMs,\n      easing: 'linear',\n      fill: 'forwards',\n    });\n    fill.pause();\n    fill.onfinish = () => onDoneRef.current();\n    fillRef.current = fill;\n    return () => {\n      fill.cancel();\n      fillRef.current = null;\n    };\n  }, [durationMs]);\n\n  useEffect(() => {\n    const fill = fillRef.current;\n    if (!fill) {\n      return;\n    }\n    if (isRunning) {\n      fill.play();\n      return;\n    }\n    fill.pause();\n  }, [isRunning, durationMs]);\n\n  return <span ref={barRef} style={EMPTY} className={FILL} />;\n}\n\ntype StorySegmentProps = {\n  position: number;\n  activePosition: number;\n  /** Nothing fills by itself: the segment of the quote on screen is drawn full. */\n  isStill: boolean;\n  durationMs: number;\n  isRunning: boolean;\n  onDone: () => void;\n};\n\nfunction StorySegment({\n  position,\n  activePosition,\n  isStill,\n  durationMs,\n  isRunning,\n  onDone,\n}: StorySegmentProps) {\n  const isActive = position === activePosition;\n  const isPast = position < activePosition;\n  const isHeldFull = isActive && isStill;\n  const isFull = isPast || isHeldFull;\n  const isFilling = isActive && !isStill;\n\n  return (\n    <span className='bg-foreground/10 block h-1 flex-1 overflow-hidden rounded-full'>\n      {isFull && <span className={FILL} />}\n      {isFilling && (\n        <ActiveFill\n          durationMs={durationMs}\n          isRunning={isRunning}\n          onDone={onDone}\n        />\n      )}\n    </span>\n  );\n}\n\nfunction pauseLabelOf(isPaused: boolean) {\n  if (isPaused) {\n    return 'Play the quotes';\n  }\n  return 'Pause the quotes';\n}\n\nfunction pausedMessageOf(isPaused: boolean) {\n  if (isPaused) {\n    return 'Quotes paused';\n  }\n  return 'Quotes playing';\n}\n\nfunction keysHintOf(canPlay: boolean) {\n  if (canPlay) {\n    return 'Left and Right arrows step between the quotes, Space plays or pauses them';\n  }\n  return 'Left and Right arrows step between the quotes';\n}\n\nfunction spokenQuoteOf(quote: StoryQuote, position: number, count: number) {\n  return `Quote ${position} of ${count}. ${quote.text} ${quote.author}, ${quote.role} at ${quote.company}`;\n}\n\nexport type StoryQuotesProps = {\n  quotes: StoryQuote[];\n  /** How long each quote stays before the next one, in milliseconds. */\n  durationMs?: number;\n  /** Says the quotes are made up. Leave it out for real ones. */\n  note?: string;\n  label?: string;\n  className?: string;\n};\n\nexport function StoryQuotes({\n  quotes,\n  durationMs = DEFAULT_DURATION_MS,\n  note,\n  label = 'Quotes',\n  className,\n}: StoryQuotesProps) {\n  const rootRef = useRef<HTMLDivElement>(null);\n  const hintId = useId();\n  const [index, setIndex] = useState(0);\n  const [isPaused, setIsPaused] = useState(false);\n  const [announcement, setAnnouncement] = useState('');\n  const isInView = useInView(rootRef, { amount: 0.3 });\n  const isVisible = useDocumentVisible();\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const count = quotes.length;\n  const activeIndex = wrapIndex(index, count);\n  const quote = quotes[activeIndex];\n  const isOnScreen = isInView && isVisible;\n  const isRunning = isOnScreen && !isPaused;\n  const canPlay = !shouldReduceMotion;\n  const keysHint = keysHintOf(canPlay);\n  const rise = risePresenceOf(shouldReduceMotion);\n\n  function advance() {\n    const stepped = activeIndex + 1;\n    const nextIndex = wrapIndex(stepped, count);\n    setIndex(nextIndex);\n  }\n\n  /** A person who steps is reading at their own pace: the story holds until they play it again. */\n  function step(direction: 1 | -1) {\n    const stepped = activeIndex + direction;\n    const nextIndex = wrapIndex(stepped, count);\n    const nextQuote = quotes[nextIndex];\n    const nextPosition = nextIndex + 1;\n    setIndex(nextIndex);\n    setIsPaused(true);\n    if (nextQuote) {\n      setAnnouncement(spokenQuoteOf(nextQuote, nextPosition, count));\n    }\n  }\n\n  /** Nothing changes under a keyboard that has just arrived: the story holds, and Space plays it. Focus that moves inside the card changes nothing. */\n  function holdOnKeyboardFocus(event: React.FocusEvent<HTMLDivElement>) {\n    if (isKeyboardArrival(event)) {\n      setIsPaused(true);\n    }\n  }\n\n  function togglePause() {\n    const willPause = !isPaused;\n    setIsPaused(willPause);\n    setAnnouncement(pausedMessageOf(willPause));\n  }\n\n  function stepFromSide(event: React.MouseEvent<HTMLDivElement>) {\n    if (isOnControl(event.target)) {\n      return;\n    }\n    const box = event.currentTarget.getBoundingClientRect();\n    const half = box.width / 2;\n    const middle = box.left + half;\n    if (event.clientX < middle) {\n      step(-1);\n      return;\n    }\n    step(1);\n  }\n\n  function actOnKeys(event: React.KeyboardEvent<HTMLDivElement>) {\n    const direction = sideStepOf(event.key);\n    if (direction !== 0) {\n      event.preventDefault();\n      step(direction);\n      return;\n    }\n    const isSpace = event.key === ' ';\n    const isOnCard = !isOnControl(event.target);\n    const canPause = isSpace && isOnCard && canPlay;\n    if (canPause) {\n      event.preventDefault();\n      togglePause();\n    }\n  }\n\n  if (!quote) {\n    return (\n      <p className='hairline-border border-foreground/20 text-muted-foreground flex h-40 w-full items-center justify-center rounded-xl border-dashed text-[13px]'>\n        No quotes yet\n      </p>\n    );\n  }\n\n  return (\n    <div\n      ref={rootRef}\n      role='group'\n      aria-roledescription='carousel'\n      aria-label={label}\n      aria-describedby={hintId}\n      tabIndex={0}\n      onClick={stepFromSide}\n      onKeyDown={actOnKeys}\n      onFocus={holdOnKeyboardFocus}\n      className={cn(\n        'group/story bg-card hairline-ring ring-border relative flex w-full cursor-pointer flex-col gap-4 rounded-xl p-5 shadow-[0_1px_2px_rgb(0_0_0/0.04)]',\n        FOCUS_RING,\n        className\n      )}\n    >\n      <div className='flex h-7 items-center justify-between gap-2'>\n        <div className='flex min-w-0 items-center gap-2'>\n          <span className='keycap-3d text-muted-foreground flex size-6 shrink-0 items-center justify-center rounded-md'>\n            <TextQuoteIcon className='size-3.5' />\n          </span>\n          {note && <SampleNote className='truncate'>{note}</SampleNote>}\n        </div>\n        {!shouldReduceMotion && (\n          <Button\n            type='button'\n            variant='ghost'\n            size='icon-sm'\n            aria-label={pauseLabelOf(isPaused)}\n            onClick={togglePause}\n            className='text-muted-foreground -me-1.5'\n          >\n            <Swap swapKey={String(isPaused)}>\n              {isPaused && <PlayIcon />}\n              {!isPaused && <PauseIcon />}\n            </Swap>\n          </Button>\n        )}\n      </div>\n      <div className='relative min-h-36'>\n        <AnimatePresence mode='popLayout' initial={false}>\n          <motion.figure\n            key={quote.id}\n            className='flex min-h-36 flex-col justify-between gap-4'\n            {...rise}\n          >\n            <blockquote className='text-base leading-6 font-medium text-balance'>\n              {quote.text}\n            </blockquote>\n            <figcaption className='flex items-center gap-2.5'>\n              <Avatar seed={quote.author} />\n              <span className='min-w-0'>\n                <span className='block truncate text-[13px] font-medium'>\n                  {quote.author}\n                </span>\n                <span className='text-muted-foreground flex flex-wrap items-center gap-x-1.5 text-xs'>\n                  <span className='truncate'>{quote.role}</span>\n                  <span className='flex min-w-0 items-center gap-1.5'>\n                    <CompanyMark seed={quote.company} size='sm' />\n                    <span className='truncate'>{quote.company}</span>\n                  </span>\n                </span>\n              </span>\n            </figcaption>\n          </motion.figure>\n        </AnimatePresence>\n      </div>\n      <div aria-hidden='true' className='flex gap-1'>\n        {quotes.map((candidate, position) => (\n          <StorySegment\n            key={candidate.id}\n            position={position}\n            activePosition={activeIndex}\n            isStill={shouldReduceMotion}\n            durationMs={durationMs}\n            isRunning={isRunning}\n            onDone={advance}\n          />\n        ))}\n      </div>\n      <span aria-hidden='true' className={cn(SIDE_HINT, 'start-0.5')}>\n        <ChevronLeftIcon className='size-4' />\n      </span>\n      <span aria-hidden='true' className={cn(SIDE_HINT, 'end-0.5')}>\n        <ChevronRightIcon className='size-4' />\n      </span>\n      <span className='sr-only'>\n        <span id={hintId}>{keysHint}</span>\n        <button type='button' tabIndex={-1} onClick={() => step(-1)}>\n          Previous quote\n        </button>\n        <button type='button' tabIndex={-1} onClick={() => step(1)}>\n          Next quote\n        </button>\n      </span>\n      <LiveRegion message={announcement} />\n    </div>\n  );\n}\n\nconst QUOTES: StoryQuote[] = [\n  {\n    id: 'board',\n    text: 'We stopped asking who is working on what. The board answers before the question comes up.',\n    author: PEOPLE.amara.name,\n    role: PEOPLE.amara.role,\n    company: PEOPLE.amara.company,\n  },\n  {\n    id: 'reviews',\n    text: 'Reviews used to wait for a meeting. Now a change gets read the hour it lands.',\n    author: PEOPLE.jonas.name,\n    role: PEOPLE.jonas.role,\n    company: PEOPLE.jonas.company,\n  },\n  {\n    id: 'handoff',\n    text: 'The handoff notes come from the task itself, so a design review starts on the work.',\n    author: PEOPLE.mei.name,\n    role: PEOPLE.mei.role,\n    company: PEOPLE.mei.company,\n  },\n  {\n    id: 'timeline',\n    text: 'Support reads the same timeline as engineering. One tool fewer to check before a reply.',\n    author: PEOPLE.rafael.name,\n    role: PEOPLE.rafael.role,\n    company: PEOPLE.rafael.company,\n  },\n];\n\nexport default function Card05() {\n  const ref = useRef<HTMLDivElement>(null);\n\n  useKeptHeight(ref);\n\n  return (\n    <div ref={ref} className='min-h-[15.5rem] w-full max-w-[22rem]'>\n      <StoryQuotes quotes={QUOTES} note='Sample quote' />\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/card/card-05.tsx"
    },
    {
      "path": "registry/ndeko/components/card/_parts/deck.ts",
      "content": "'use client';\n\nimport { useCallback, useLayoutEffect, useReducer } from 'react';\n\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { useTimeouts } from '@/registry/ndeko/hooks/use-timeouts';\n\ntype DeckDirection = 1 | -1;\n\ntype DeckState = {\n  front: number;\n  /** The card held out of the deck for a moment: the one that left the front, or the one about to come back to it. */\n  out: number | null;\n  /** The out card waits in its pose, then comes back to the front from there. */\n  isReturning: boolean;\n};\n\ntype DeckAction =\n  | { type: 'leave'; count: number }\n  | { type: 'summon'; count: number }\n  | { type: 'jump'; direction: DeckDirection; count: number }\n  | { type: 'land' };\n\nconst RESTING: DeckState = { front: 0, out: null, isReturning: false };\n\n/** An index that runs past either end of a list comes back in at the other end. */\nexport function wrapIndex(index: number, count: number) {\n  if (count <= 0) {\n    return 0;\n  }\n  const remainder = index % count;\n  const positive = remainder + count;\n  return positive % count;\n}\n\n/** How many cards sit in front of this one: 0 for the front card. */\nexport function depthOf(index: number, front: number, count: number) {\n  const behind = index - front;\n  return wrapIndex(behind, count);\n}\n\nfunction landed(deck: DeckState): DeckState {\n  if (deck.out === null) {\n    return deck;\n  }\n  if (deck.isReturning) {\n    return { front: deck.out, out: null, isReturning: false };\n  }\n  return { front: deck.front, out: null, isReturning: false };\n}\n\nfunction reduceDeck(deck: DeckState, action: DeckAction): DeckState {\n  const settled = landed(deck);\n  if (action.type === 'land') {\n    return settled;\n  }\n  if (action.type === 'jump') {\n    const stepped = settled.front + action.direction;\n    const front = wrapIndex(stepped, action.count);\n    return { front, out: null, isReturning: false };\n  }\n  if (action.type === 'leave') {\n    const stepped = settled.front + 1;\n    const front = wrapIndex(stepped, action.count);\n    return { front, out: settled.front, isReturning: false };\n  }\n  const before = settled.front - 1;\n  const out = wrapIndex(before, action.count);\n  return { front: settled.front, out, isReturning: true };\n}\n\n/**\n * A deck that turns one card at a time. Going forward, the front card is held out while the others\n * step up, then it joins the back. Going back, the last card is placed out first and comes to the\n * front from there. Each piece draws the out pose its own way: dropped, swung aside.\n */\nexport function useDeck(count: number, leaveMs: number) {\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const [deck, dispatch] = useReducer(reduceDeck, RESTING);\n  const { schedule, clearAll } = useTimeouts();\n  const canTurn = count > 1;\n\n  useLayoutEffect(() => {\n    if (!deck.isReturning) {\n      return;\n    }\n    // A transition starts from the last pose the browser applied. Reading a layout value applies\n    // the out pose now, so the card travels from there and not from the back of the deck.\n    document.body.getBoundingClientRect();\n    const frame = requestAnimationFrame(() => dispatch({ type: 'land' }));\n    return () => cancelAnimationFrame(frame);\n  }, [deck.isReturning, deck.out]);\n\n  const next = useCallback(() => {\n    if (!canTurn) {\n      return;\n    }\n    clearAll();\n    if (shouldReduceMotion) {\n      dispatch({ type: 'jump', direction: 1, count });\n      return;\n    }\n    dispatch({ type: 'leave', count });\n    schedule(() => dispatch({ type: 'land' }), leaveMs);\n  }, [canTurn, clearAll, count, leaveMs, schedule, shouldReduceMotion]);\n\n  const previous = useCallback(() => {\n    if (!canTurn) {\n      return;\n    }\n    clearAll();\n    if (shouldReduceMotion) {\n      dispatch({ type: 'jump', direction: -1, count });\n      return;\n    }\n    dispatch({ type: 'summon', count });\n  }, [canTurn, clearAll, count, shouldReduceMotion]);\n\n  const front = wrapIndex(deck.front, count);\n\n  return {\n    front,\n    out: deck.out,\n    isReturning: deck.isReturning,\n    next,\n    previous,\n  };\n}\n",
      "type": "registry:component",
      "target": "components/card/_parts/deck.ts"
    },
    {
      "path": "registry/ndeko/components/card/_parts/keys.ts",
      "content": "/** The step an arrow key asks for along a row: -1 for Left, 1 for Right, 0 for any other key. */\nexport function sideStepOf(key: string) {\n  if (key === 'ArrowLeft') {\n    return -1;\n  }\n  if (key === 'ArrowRight') {\n    return 1;\n  }\n  return 0;\n}\n\n/** True when a press landed on a control inside a surface that also answers presses of its own. */\nexport function isOnControl(target: EventTarget) {\n  if (!(target instanceof Element)) {\n    return false;\n  }\n  return target.closest('button, a, input') !== null;\n}\n\n/** True when the keyboard brings focus into an element from outside it, not when focus moves within it. */\nexport function isKeyboardArrival(event: React.FocusEvent<HTMLElement>) {\n  const isKeyboardFocus = event.target.matches(':focus-visible');\n  const cameFromInside = event.currentTarget.contains(event.relatedTarget);\n  return isKeyboardFocus && !cameFromInside;\n}\n\n/**\n * A card that goes behind the others turns inert and would drop the focus it holds. Before a deck\n * turns from the keyboard, the group takes the focus, so the next arrow key still reaches it.\n */\nexport function holdFocusOutsideCards(group: HTMLElement) {\n  const focused = document.activeElement;\n  if (!(focused instanceof Element)) {\n    return;\n  }\n  const card = focused.closest('li');\n  if (!card) {\n    return;\n  }\n  if (group.contains(card)) {\n    group.focus();\n  }\n}\n",
      "type": "registry:component",
      "target": "components/card/_parts/keys.ts"
    },
    {
      "path": "registry/ndeko/components/card/_parts/people.ts",
      "content": "export type Person = {\n  id: string;\n  name: string;\n  role: string;\n  company: string;\n};\n\n/** The invented people of the card demos. The same person keeps the same picture on every card. */\nexport const PEOPLE = {\n  amara: {\n    id: 'amara',\n    name: 'Amara Diallo',\n    role: 'Product lead',\n    company: 'Halden Works',\n  },\n  jonas: {\n    id: 'jonas',\n    name: 'Jonas Weber',\n    role: 'Staff engineer',\n    company: 'Brightwater',\n  },\n  mei: {\n    id: 'mei',\n    name: 'Mei Tanaka',\n    role: 'Design director',\n    company: 'Kiln Studio',\n  },\n  rafael: {\n    id: 'rafael',\n    name: 'Rafael Ortega',\n    role: 'Head of support',\n    company: 'Tallyard',\n  },\n  ingrid: {\n    id: 'ingrid',\n    name: 'Ingrid Solheim',\n    role: 'Finance manager',\n    company: 'Quillhouse',\n  },\n} satisfies Record<string, Person>;\n",
      "type": "registry:component",
      "target": "components/card/_parts/people.ts"
    },
    {
      "path": "registry/ndeko/components/card/_parts/sample-note.tsx",
      "content": "import { cn } from '@/registry/ndeko/lib/utils';\n\ntype SampleNoteProps = {\n  /** What the sample is: \"Illustrative\", \"Sample quote\". */\n  children?: React.ReactNode;\n  className?: string;\n};\n\n/** The word that says the figures or the words of a demo are made up. */\nexport function SampleNote({\n  children = 'Illustrative',\n  className,\n}: SampleNoteProps) {\n  return (\n    <span className={cn('text-muted-foreground text-xs', className)}>\n      {children}\n    </span>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/card/_parts/sample-note.tsx"
    }
  ],
  "categories": [
    "components",
    "card"
  ],
  "type": "registry:component"
}