{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "opentui/text-area",
  "title": "Text Area",
  "description": "Multi-line text editor with word wrap",
  "dependencies": [
    "@opentui/react"
  ],
  "registryDependencies": [
    "https://termcn.dev/r/opentui/use-theme.json"
  ],
  "files": [
    {
      "path": "registry/opentui/ui/text-area.tsx",
      "content": "/* @jsxImportSource @opentui/react */\nimport { useKeyboard } from \"@opentui/react\";\nimport { useState } from \"react\";\n\nimport { useTheme } from \"@/hooks/use-theme\";\nimport type { BorderStyle } from \"@/components/ui/types\";\n\nexport interface TextAreaProps {\n  value?: string;\n  onChange?: (value: string) => void;\n  onSubmit?: (value: string) => void;\n  placeholder?: string;\n  rows?: number;\n  label?: string;\n  id?: string;\n  borderStyle?: BorderStyle;\n  paddingX?: number;\n  cursor?: string;\n}\n\nconst getLines = (v: string): string[] => v.split(\"\\n\");\n\nconst joinLines = (lines: string[]): string => lines.join(\"\\n\");\n\nexport const TextArea = ({\n  value: controlledValue,\n  onChange,\n  onSubmit,\n  placeholder = \"\",\n  rows = 4,\n  label,\n  id: _id,\n  borderStyle = \"rounded\",\n  paddingX = 1,\n  cursor = \"█\",\n}: TextAreaProps) => {\n  const [internalValue, setInternalValue] = useState(\"\");\n  const [cursorLine, setCursorLine] = useState(0);\n  const [cursorCol, setCursorCol] = useState(0);\n  const [scrollOffset, setScrollOffset] = useState(0);\n  const theme = useTheme();\n  const [isFocused] = useState(true);\n\n  const value = controlledValue ?? internalValue;\n\n  const setValue = (newVal: string) => {\n    if (onChange) {\n      onChange(newVal);\n    } else {\n      setInternalValue(newVal);\n    }\n  };\n\n  useKeyboard((key) => {\n    if (!isFocused) {\n      return;\n    }\n    const lines = getLines(value);\n    if (key.name === \"return\" && key.ctrl) {\n      onSubmit?.(value);\n      return;\n    }\n    if (key.name === \"return\") {\n      const totalLines = lines.length;\n      if (totalLines >= rows && cursorLine === rows - 1) {\n        return;\n      }\n      const currentLine = lines[cursorLine] ?? \"\";\n      const before = currentLine.slice(0, cursorCol);\n      const after = currentLine.slice(cursorCol);\n      const newLines = [\n        ...lines.slice(0, cursorLine),\n        before,\n        after,\n        ...lines.slice(cursorLine + 1),\n      ];\n      setValue(joinLines(newLines));\n      const newLine = cursorLine + 1;\n      setCursorLine(newLine);\n      setCursorCol(0);\n      if (newLine >= scrollOffset + rows) {\n        setScrollOffset(newLine - rows + 1);\n      }\n      return;\n    }\n    if (key.name === \"backspace\" || key.name === \"delete\") {\n      const currentLine = lines[cursorLine] ?? \"\";\n      if (cursorCol > 0) {\n        const newLine =\n          currentLine.slice(0, cursorCol - 1) + currentLine.slice(cursorCol);\n        const newLines = [\n          ...lines.slice(0, cursorLine),\n          newLine,\n          ...lines.slice(cursorLine + 1),\n        ];\n        setValue(joinLines(newLines));\n        setCursorCol(cursorCol - 1);\n      } else if (cursorLine > 0) {\n        const prevLine = lines[cursorLine - 1] ?? \"\";\n        const mergedLine = prevLine + currentLine;\n        const newLines = [\n          ...lines.slice(0, cursorLine - 1),\n          mergedLine,\n          ...lines.slice(cursorLine + 1),\n        ];\n        setValue(joinLines(newLines));\n        const newLineIdx = cursorLine - 1;\n        setCursorLine(newLineIdx);\n        setCursorCol(prevLine.length);\n        if (newLineIdx < scrollOffset) {\n          setScrollOffset(newLineIdx);\n        }\n      }\n      return;\n    }\n    if (key.name === \"left\") {\n      if (cursorCol > 0) {\n        setCursorCol(cursorCol - 1);\n      } else if (cursorLine > 0) {\n        const prevLine = lines[cursorLine - 1] ?? \"\";\n        const newLineIdx = cursorLine - 1;\n        setCursorLine(newLineIdx);\n        setCursorCol(prevLine.length);\n        if (newLineIdx < scrollOffset) {\n          setScrollOffset(newLineIdx);\n        }\n      }\n      return;\n    }\n    if (key.name === \"right\") {\n      const currentLine = lines[cursorLine] ?? \"\";\n      if (cursorCol < currentLine.length) {\n        setCursorCol(cursorCol + 1);\n      } else if (cursorLine < lines.length - 1) {\n        const newLineIdx = cursorLine + 1;\n        setCursorLine(newLineIdx);\n        setCursorCol(0);\n        if (newLineIdx >= scrollOffset + rows) {\n          setScrollOffset(newLineIdx - rows + 1);\n        }\n      }\n      return;\n    }\n    if (key.name === \"up\") {\n      if (cursorLine > 0) {\n        const newLineIdx = cursorLine - 1;\n        const targetLine = lines[newLineIdx] ?? \"\";\n        setCursorLine(newLineIdx);\n        setCursorCol(Math.min(cursorCol, targetLine.length));\n        if (newLineIdx < scrollOffset) {\n          setScrollOffset(newLineIdx);\n        }\n      }\n      return;\n    }\n    if (key.name === \"down\") {\n      if (cursorLine < lines.length - 1) {\n        const newLineIdx = cursorLine + 1;\n        const targetLine = lines[newLineIdx] ?? \"\";\n        setCursorLine(newLineIdx);\n        setCursorCol(Math.min(cursorCol, targetLine.length));\n        if (newLineIdx >= scrollOffset + rows) {\n          setScrollOffset(newLineIdx - rows + 1);\n        }\n      }\n      return;\n    }\n    if (key.name === \"escape\" || key.name === \"tab\") {\n      return;\n    }\n    if (key.name.length === 1) {\n      const currentLine = lines[cursorLine] ?? \"\";\n      const newLine =\n        currentLine.slice(0, cursorCol) +\n        key.name +\n        currentLine.slice(cursorCol);\n      const newLines = [\n        ...lines.slice(0, cursorLine),\n        newLine,\n        ...lines.slice(cursorLine + 1),\n      ];\n      setValue(joinLines(newLines));\n      setCursorCol(cursorCol + key.name.length);\n    }\n  });\n\n  const borderColor = isFocused ? theme.colors.focusRing : theme.colors.border;\n  const lines = getLines(value);\n  const visibleLines = lines.slice(scrollOffset, scrollOffset + rows);\n\n  const paddedLines: string[] = [...visibleLines];\n  while (paddedLines.length < rows) {\n    paddedLines.push(\"\");\n  }\n\n  const isEmpty = value.length === 0;\n\n  return (\n    <box flexDirection=\"column\">\n      {label && (\n        <text>\n          <b>{label}</b>\n        </text>\n      )}\n      <box\n        flexDirection=\"column\"\n        borderStyle={borderStyle}\n        borderColor={borderColor}\n        paddingLeft={paddingX}\n        paddingRight={paddingX}\n      >\n        {paddedLines.map((line, rowIdx) => {\n          const absoluteLineIdx = rowIdx + scrollOffset;\n          const isActiveLine = isFocused && absoluteLineIdx === cursorLine;\n          if (isEmpty && rowIdx === 0) {\n            return (\n              <box key={rowIdx} flexDirection=\"row\">\n                <text fg={theme.colors.mutedForeground}>{placeholder}</text>\n                {isFocused && <text fg={theme.colors.focusRing}>{cursor}</text>}\n              </box>\n            );\n          }\n          if (isActiveLine) {\n            const before = line.slice(0, cursorCol);\n            const after = line.slice(cursorCol);\n            return (\n              <box key={rowIdx} flexDirection=\"row\">\n                <text fg={theme.colors.foreground}>{before}</text>\n                <text fg={theme.colors.focusRing}>{cursor}</text>\n                <text fg={theme.colors.foreground}>{after}</text>\n              </box>\n            );\n          }\n          return (\n            <box key={rowIdx}>\n              <text fg={theme.colors.foreground}>{line}</text>\n            </box>\n          );\n        })}\n      </box>\n    </box>\n  );\n};\n",
      "type": "registry:ui",
      "target": "components/ui/text-area.tsx"
    }
  ],
  "categories": [
    "input"
  ],
  "type": "registry:ui"
}