{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "motion-terminal",
  "title": "Terminal",
  "description": "A command line for developer docs that types its commands and prints their output line by line.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "components/core/terminal.tsx",
      "content": "'use client';\nimport { cn } from '@/lib/utils';\nimport {\n  AnimatePresence,\n  animate,\n  motion,\n  useInView,\n  useMotionValue,\n  useTransform,\n  type MotionValue,\n  type Transition,\n} from 'motion/react';\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { CheckIcon, CopyIcon, RotateCcwIcon } from '@/registry/ndeko/lib/icons';\nimport { Button } from '@/registry/ndeko/ui/button';\n\nexport type TerminalTone =\n  'default' | 'muted' | 'success' | 'warning' | 'error';\n\nexport type TerminalLine =\n  | { kind: 'command'; text: string }\n  | { kind: 'output'; text: string; tone?: TerminalTone; delay?: number };\n\ntype LineStatus = 'done' | 'active' | 'pending';\n\nconst TONE_CLASS: Record<TerminalTone, string> = {\n  default: 'text-foreground',\n  muted: 'text-muted-foreground',\n  success: 'text-success-foreground',\n  warning: 'text-warning-foreground',\n  error: 'text-destructive-ink',\n};\n\nconst PAUSE_AFTER_COMMAND_MS = 400;\n\nconst COPIED_DURATION_MS = 1600;\n\nconst BLINK: Transition = {\n  duration: 1.06,\n  times: [0, 0.5, 0.5, 1],\n  repeat: Infinity,\n  ease: 'linear',\n};\n\nconst STEADY: Transition = { duration: 0 };\n\nconst SWAP_TRANSITION = { duration: 0.15, ease: [0.2, 0, 0, 1] } as const;\n\nfunction statusOf(index: number, cursor: number): LineStatus {\n  if (index < cursor) {\n    return 'done';\n  }\n  if (index === cursor) {\n    return 'active';\n  }\n  return 'pending';\n}\n\nfunction cursorOf(\n  shouldReduceMotion: boolean | null,\n  activeLine: number,\n  lineCount: number\n) {\n  if (shouldReduceMotion) {\n    return lineCount;\n  }\n  return activeLine;\n}\n\nfunction typedClipOf(typed: number) {\n  const visibleCharacters = Math.round(typed);\n  return `inset(0 calc(100% - ${visibleCharacters}ch) 0 0)`;\n}\n\nfunction caretOffsetOf(typed: number) {\n  const visibleCharacters = Math.round(typed);\n  return `translateX(${visibleCharacters}ch)`;\n}\n\nfunction caretMotionOf(shouldBlink: boolean) {\n  if (shouldBlink) {\n    return { animate: { opacity: [1, 1, 0, 0] }, transition: BLINK };\n  }\n  return { animate: { opacity: 1 }, transition: STEADY };\n}\n\nfunction swapMotionOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return {\n      initial: { opacity: 0 },\n      animate: { opacity: 1 },\n      exit: { opacity: 0 },\n    };\n  }\n  return {\n    initial: { opacity: 0, scale: 0.6, filter: 'blur(2px)' },\n    animate: { opacity: 1, scale: 1, filter: 'blur(0px)' },\n    exit: { opacity: 0, scale: 0.6, filter: 'blur(2px)' },\n  };\n}\n\nfunction Caret({\n  isBlinking,\n  shouldReduceMotion,\n  style,\n}: {\n  isBlinking: boolean;\n  shouldReduceMotion: boolean | null;\n  style?: { transform: MotionValue<string> };\n}) {\n  const shouldBlink = isBlinking && !shouldReduceMotion;\n  const caretMotion = caretMotionOf(shouldBlink);\n\n  return (\n    <motion.span\n      aria-hidden='true'\n      className='bg-foreground/70 absolute inset-y-[0.2em] left-0 w-[1ch]'\n      style={style}\n      {...caretMotion}\n    />\n  );\n}\n\nfunction Prompt({ prompt }: { prompt: string }) {\n  return (\n    <span\n      aria-hidden='true'\n      className='text-muted-foreground shrink-0 pr-[1ch] select-none'\n    >\n      {prompt}\n    </span>\n  );\n}\n\nfunction TypedCommand({\n  text,\n  prompt,\n  isPlaying,\n  typingSpeed,\n  shouldReduceMotion,\n  onTyped,\n}: {\n  text: string;\n  prompt: string;\n  isPlaying: boolean;\n  typingSpeed: number;\n  shouldReduceMotion: boolean | null;\n  onTyped: () => void;\n}) {\n  const typed = useMotionValue(0);\n  const clipPath = useTransform(typed, typedClipOf);\n  const caretOffset = useTransform(typed, caretOffsetOf);\n\n  useEffect(() => {\n    if (!isPlaying) {\n      return;\n    }\n    let isCancelled = false;\n    let pauseTimer: ReturnType<typeof setTimeout> | undefined;\n    const typingMs = text.length * typingSpeed;\n    const typingSeconds = typingMs / 1000;\n    const controls = animate(typed, text.length, {\n      duration: typingSeconds,\n      ease: 'linear',\n    });\n    controls.then(() => {\n      if (isCancelled) {\n        return;\n      }\n      pauseTimer = setTimeout(onTyped, PAUSE_AFTER_COMMAND_MS);\n    });\n    return () => {\n      isCancelled = true;\n      controls.stop();\n      clearTimeout(pauseTimer);\n    };\n  }, [isPlaying, text, typingSpeed, typed, onTyped]);\n\n  return (\n    <div className='flex'>\n      <Prompt prompt={prompt} />\n      <span className='relative shrink-0'>\n        <motion.span className='block' style={{ clipPath }}>\n          {text}\n        </motion.span>\n        <Caret\n          isBlinking={!isPlaying}\n          shouldReduceMotion={shouldReduceMotion}\n          style={{ transform: caretOffset }}\n        />\n      </span>\n    </div>\n  );\n}\n\nfunction StaticLine({\n  line,\n  prompt,\n  isHidden,\n}: {\n  line: TerminalLine;\n  prompt: string;\n  isHidden: boolean;\n}) {\n  if (line.kind === 'command') {\n    return (\n      <div className={cn('flex', isHidden && 'opacity-0')}>\n        <Prompt prompt={prompt} />\n        <span className='shrink-0'>{line.text}</span>\n      </div>\n    );\n  }\n  const tone = line.tone ?? 'default';\n\n  return (\n    <div className={cn('shrink-0', TONE_CLASS[tone], isHidden && 'opacity-0')}>\n      {line.text}\n    </div>\n  );\n}\n\nfunction TerminalSession({\n  lines,\n  prompt,\n  typingSpeed,\n  outputDelay,\n}: {\n  lines: TerminalLine[];\n  prompt: string;\n  typingSpeed: number;\n  outputDelay: number;\n}) {\n  const sessionRef = useRef<HTMLDivElement>(null);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const isInView = useInView(sessionRef, { once: true, amount: 0.5 });\n  const [activeLine, setActiveLine] = useState(0);\n  const cursor = cursorOf(shouldReduceMotion, activeLine, lines.length);\n  const isPlaying = isInView && !shouldReduceMotion;\n  const isFinished = cursor >= lines.length;\n\n  const advance = useCallback(() => {\n    setActiveLine((current) => current + 1);\n  }, []);\n\n  useEffect(() => {\n    if (!isPlaying) {\n      return;\n    }\n    const line = lines[activeLine];\n    const isOutput = line?.kind === 'output';\n    if (!isOutput) {\n      return;\n    }\n    const delay = line.delay ?? outputDelay;\n    const timer = setTimeout(advance, delay);\n    return () => clearTimeout(timer);\n  }, [activeLine, isPlaying, lines, outputDelay, advance]);\n\n  return (\n    <div ref={sessionRef} className='overflow-x-auto px-4 pb-4 whitespace-pre'>\n      {lines.map((line, index) => {\n        const key = `${index}-${line.text}`;\n        const status = statusOf(index, cursor);\n        const isTyping = status === 'active' && line.kind === 'command';\n\n        if (isTyping) {\n          return (\n            <TypedCommand\n              key={key}\n              text={line.text}\n              prompt={prompt}\n              isPlaying={isPlaying}\n              typingSpeed={typingSpeed}\n              shouldReduceMotion={shouldReduceMotion}\n              onTyped={advance}\n            />\n          );\n        }\n        return (\n          <StaticLine\n            key={key}\n            line={line}\n            prompt={prompt}\n            isHidden={status === 'pending'}\n          />\n        );\n      })}\n      <div\n        aria-hidden='true'\n        className={cn('flex', !isFinished && 'opacity-0')}\n      >\n        <Prompt prompt={prompt} />\n        <span className='relative'>\n          <span className='invisible'> </span>\n          <Caret isBlinking shouldReduceMotion={shouldReduceMotion} />\n        </span>\n      </div>\n    </div>\n  );\n}\n\nfunction CopyCommandsButton({ commands }: { commands: string }) {\n  const [hasCopied, setHasCopied] = useState(false);\n  const resetTimer = useRef<ReturnType<typeof setTimeout> | null>(null);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const swap = swapMotionOf(shouldReduceMotion);\n\n  useEffect(() => {\n    return () => {\n      if (resetTimer.current) {\n        clearTimeout(resetTimer.current);\n      }\n    };\n  }, []);\n\n  async function copy() {\n    try {\n      await navigator.clipboard.writeText(commands);\n    } catch {\n      return;\n    }\n    setHasCopied(true);\n    if (resetTimer.current) {\n      clearTimeout(resetTimer.current);\n    }\n    resetTimer.current = setTimeout(() => {\n      setHasCopied(false);\n    }, COPIED_DURATION_MS);\n  }\n\n  return (\n    <Button\n      variant='ghost'\n      size='icon-sm'\n      className='text-muted-foreground'\n      aria-label='Copy commands'\n      onClick={copy}\n    >\n      <AnimatePresence mode='popLayout' initial={false}>\n        {hasCopied && (\n          <motion.span\n            key='copied'\n            className='flex'\n            transition={SWAP_TRANSITION}\n            {...swap}\n          >\n            <CheckIcon />\n          </motion.span>\n        )}\n        {!hasCopied && (\n          <motion.span\n            key='copy'\n            className='flex'\n            transition={SWAP_TRANSITION}\n            {...swap}\n          >\n            <CopyIcon />\n          </motion.span>\n        )}\n      </AnimatePresence>\n      <span className='sr-only' aria-live='polite'>\n        {hasCopied && 'Copied'}\n      </span>\n    </Button>\n  );\n}\n\nexport type TerminalProps = {\n  lines: TerminalLine[];\n  title?: string;\n  prompt?: string;\n  typingSpeed?: number;\n  outputDelay?: number;\n  className?: string;\n};\n\nexport function Terminal({\n  lines,\n  title = 'Terminal',\n  prompt = '$',\n  typingSpeed = 32,\n  outputDelay = 120,\n  className,\n}: TerminalProps) {\n  const [run, setRun] = useState(0);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const commandLines = lines.filter((line) => line.kind === 'command');\n  const commandTexts = commandLines.map((line) => line.text);\n  const commands = commandTexts.join('\\n');\n  const hasCommands = commandLines.length > 0;\n  const canReplay = !shouldReduceMotion;\n\n  function replay() {\n    setRun((current) => current + 1);\n  }\n\n  return (\n    <div\n      className={cn(\n        'bg-card hairline-ring ring-border w-full overflow-hidden rounded-xl font-mono text-[13px] leading-6 shadow-[0_1px_2px_rgb(0_0_0/0.04)]',\n        className\n      )}\n    >\n      <div className='flex h-10 items-center justify-between gap-2 pr-1.5 pl-4'>\n        <span className='text-muted-foreground font-sans text-xs'>{title}</span>\n        <div className='flex items-center gap-0.5'>\n          {canReplay && (\n            <Button\n              variant='ghost'\n              size='icon-sm'\n              className='text-muted-foreground'\n              aria-label='Replay'\n              onClick={replay}\n            >\n              <RotateCcwIcon />\n            </Button>\n          )}\n          {hasCommands && <CopyCommandsButton commands={commands} />}\n        </div>\n      </div>\n      <TerminalSession\n        key={run}\n        lines={lines}\n        prompt={prompt}\n        typingSpeed={typingSpeed}\n        outputDelay={outputDelay}\n      />\n    </div>\n  );\n}\n",
      "type": "registry:component",
      "target": "components/core/terminal.tsx"
    }
  ],
  "categories": [
    "motion"
  ],
  "type": "registry:component"
}