{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "inline-edit",
  "title": "Inline Edit",
  "description": "Click-to-edit text that turns into an auto-sizing input with save/cancel controls, validation, async save support and a shake on error.",
  "dependencies": [
    "motion",
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/inline-edit/inline-edit.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { motion, AnimatePresence, useAnimationControls } from \"motion/react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\n// useLayoutEffect logs a warning when it runs server-side (no DOM to\n// measure before paint there); fall back to useEffect there and only\n// take the synchronous-before-paint measurement in the browser.\nconst useIsomorphicLayoutEffect = typeof window !== \"undefined\" ? React.useLayoutEffect : React.useEffect;\n\nexport type InlineEditSize = \"sm\" | \"md\" | \"lg\";\n\nexport interface InlineEditProps {\n  value?: string;\n  defaultValue?: string;\n  onSave?: (value: string) => void | Promise<void>;\n  onCancel?: () => void;\n  placeholder?: string;\n  multiline?: boolean;\n  maxLength?: number;\n  /** Return an error message to block saving, or null/undefined when the value is valid. */\n  validate?: (value: string) => string | null | undefined;\n  showButtons?: boolean;\n  editOnClick?: boolean;\n  size?: InlineEditSize;\n  theme?: \"dark\" | \"light\";\n  accentColor?: string;\n  emptyText?: string;\n  className?: string;\n}\n\nconst PALETTES = {\n  dark: { text: \"#F5F4F1\", muted: \"rgba(245,244,241,0.45)\", border: \"rgba(255,255,255,0.14)\", hover: \"rgba(255,255,255,0.06)\", inputBg: \"rgba(255,255,255,0.05)\", error: \"#FF7A6B\", success: \"#87FFE3\" },\n  light: { text: \"#0A0A0A\", muted: \"rgba(10,10,10,0.4)\", border: \"rgba(10,10,10,0.18)\", hover: \"rgba(10,10,10,0.05)\", inputBg: \"#FFFFFF\", error: \"#E5484D\", success: \"#0F9F7F\" },\n};\n\nconst SIZES: Record<InlineEditSize, { font: number; padY: number; padX: number; radius: number; icon: number }> = {\n  sm: { font: 13, padY: 4, padX: 7, radius: 7, icon: 13 },\n  md: { font: 14.5, padY: 5, padX: 8, radius: 8, icon: 14 },\n  lg: { font: 16.5, padY: 6, padX: 9, radius: 9, icon: 16 },\n};\n\nfunction PencilIcon({ size }: { size: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.5\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d=\"M11.3 2.3a1.5 1.5 0 0 1 2.1 2.1L5 12.8l-3 .9.9-3 8.4-8.4Z\" />\n    </svg>\n  );\n}\n\nfunction CheckIcon({ size }: { size: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" strokeLinejoin=\"round\" aria-hidden=\"true\">\n      <path d=\"M3 8.4L6.2 11.5L13 4.3\" />\n    </svg>\n  );\n}\n\nfunction XIcon({ size }: { size: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 16 16\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"1.7\" strokeLinecap=\"round\" aria-hidden=\"true\">\n      <path d=\"M4 4L12 12M12 4L4 12\" />\n    </svg>\n  );\n}\n\nfunction Spinner({ size }: { size: number }) {\n  return (\n    <svg width={size} height={size} viewBox=\"0 0 24 24\" fill=\"none\" style={{ animation: \"inline-edit-spin 0.7s linear infinite\" }} aria-hidden=\"true\">\n      <circle cx=\"12\" cy=\"12\" r=\"9.5\" strokeOpacity=\"0.25\" strokeWidth=\"2.5\" stroke=\"currentColor\" />\n      <path d=\"M21.5 12a9.5 9.5 0 0 0-9.5-9.5\" strokeWidth=\"2.5\" strokeLinecap=\"round\" stroke=\"currentColor\" />\n      <style>{\"@keyframes inline-edit-spin { to { transform: rotate(360deg); } }\"}</style>\n    </svg>\n  );\n}\n\nexport function InlineEdit({\n  value,\n  defaultValue = \"\",\n  onSave,\n  onCancel,\n  placeholder = \"Click to edit\",\n  multiline = false,\n  maxLength,\n  validate,\n  showButtons = true,\n  editOnClick = true,\n  size = \"md\",\n  theme = \"dark\",\n  accentColor = \"#F2A841\",\n  emptyText = \"Click to add a value\",\n  className,\n}: InlineEditProps) {\n  const p = PALETTES[theme];\n  const s = SIZES[size];\n  const uid = React.useId();\n  const isControlled = value !== undefined;\n  const [internal, setInternal] = React.useState(defaultValue);\n  const committed = isControlled ? value : internal;\n\n  const [editing, setEditing] = React.useState(false);\n  const [draft, setDraft] = React.useState(committed);\n  const [error, setError] = React.useState<string | null>(null);\n  const [saving, setSaving] = React.useState(false);\n  // Imperative rather than a shake-count-driven `animate` prop + `key`:\n  // remounting the wrapper via key to force the keyframes to replay would\n  // also remount the input/textarea inside it, stealing focus right when\n  // the user needs to keep typing to fix a validation error.\n  const shakeControls = useAnimationControls();\n  const shake = () => void shakeControls.start({ x: [0, -6, 6, -4, 4, -2, 2, 0], transition: { duration: 0.4 } });\n  const inputRef = React.useRef<HTMLInputElement | HTMLTextAreaElement>(null);\n  const measureRef = React.useRef<HTMLSpanElement>(null);\n  const [inputWidth, setInputWidth] = React.useState<number | null>(null);\n\n  const startEdit = () => {\n    setDraft(committed);\n    setError(null);\n    setEditing(true);\n  };\n\n  React.useEffect(() => {\n    if (!editing) return;\n    const el = inputRef.current;\n    if (!el) return;\n    el.focus();\n    const len = el.value.length;\n    el.setSelectionRange?.(len, len);\n  }, [editing]);\n\n  useIsomorphicLayoutEffect(() => {\n    if (!editing || multiline) return;\n    const m = measureRef.current;\n    if (m) setInputWidth(Math.ceil(m.getBoundingClientRect().width) + 4);\n  }, [draft, editing, multiline, placeholder]);\n\n  const cancel = () => {\n    setEditing(false);\n    setError(null);\n    setDraft(committed);\n    onCancel?.();\n  };\n\n  const save = async () => {\n    const problem = validate?.(draft);\n    if (problem) {\n      setError(problem);\n      shake();\n      return;\n    }\n    if (draft === committed) {\n      setEditing(false);\n      return;\n    }\n    try {\n      const result = onSave?.(draft);\n      if (result && typeof (result as Promise<void>).then === \"function\") {\n        setSaving(true);\n        await result;\n      }\n      if (!isControlled) setInternal(draft);\n      setSaving(false);\n      setEditing(false);\n      setError(null);\n    } catch (err) {\n      setSaving(false);\n      setError(err instanceof Error ? err.message : \"Couldn't save that value.\");\n      shake();\n    }\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (e.key === \"Escape\") {\n      e.preventDefault();\n      cancel();\n    } else if (e.key === \"Enter\" && (!multiline || e.metaKey || e.ctrlKey)) {\n      e.preventDefault();\n      void save();\n    }\n  };\n\n  const inputStyle: React.CSSProperties = {\n    font: \"inherit\",\n    fontSize: s.font,\n    color: p.text,\n    background: p.inputBg,\n    border: `1px solid ${error ? p.error : accentColor}`,\n    borderRadius: s.radius,\n    padding: `${s.padY}px ${s.padX}px`,\n    outline: \"none\",\n    minWidth: 40,\n    maxWidth: \"100%\",\n  };\n\n  if (editing) {\n    return (\n      <motion.div\n        className={cn(\"inline-flex flex-col\", className)}\n        style={{ gap: 5, fontFamily: \"Inter, sans-serif\" }}\n        animate={shakeControls}\n      >\n        <div className=\"flex items-start\" style={{ gap: 6 }}>\n          {multiline ? (\n            <textarea\n              ref={inputRef as React.RefObject<HTMLTextAreaElement>}\n              value={draft}\n              maxLength={maxLength}\n              onChange={(e) => {\n                setDraft(e.target.value);\n                if (error) setError(null);\n              }}\n              onKeyDown={onKeyDown}\n              onBlur={() => !saving && void save()}\n              disabled={saving}\n              rows={3}\n              aria-invalid={Boolean(error)}\n              aria-describedby={error ? `${uid}-error` : undefined}\n              className=\"w-full resize-y\"\n              style={inputStyle}\n            />\n          ) : (\n            <>\n              <input\n                ref={inputRef as React.RefObject<HTMLInputElement>}\n                value={draft}\n                maxLength={maxLength}\n                placeholder={placeholder}\n                onChange={(e) => {\n                  setDraft(e.target.value);\n                  if (error) setError(null);\n                }}\n                onKeyDown={onKeyDown}\n                onBlur={() => !saving && void save()}\n                disabled={saving}\n                aria-invalid={Boolean(error)}\n                aria-describedby={error ? `${uid}-error` : undefined}\n                style={{ ...inputStyle, width: inputWidth ?? undefined }}\n              />\n              <span ref={measureRef} aria-hidden=\"true\" style={{ position: \"fixed\", top: -9999, left: -9999, whiteSpace: \"pre\", fontSize: s.font, padding: `0 ${s.padX}px`, visibility: \"hidden\" }}>\n                {draft || placeholder}\n              </span>\n            </>\n          )}\n          {showButtons && (\n            <div className=\"flex shrink-0 items-center\" style={{ gap: 4, paddingTop: multiline ? 2 : 0 }}>\n              <button\n                type=\"button\"\n                aria-label=\"Save\"\n                disabled={saving}\n                onMouseDown={(e) => e.preventDefault()}\n                onClick={() => void save()}\n                className=\"flex cursor-pointer items-center justify-center rounded-md border-none outline-none focus-visible:ring-2 disabled:cursor-default\"\n                style={{ width: s.font + 14, height: s.font + 14, background: p.hover, color: saving ? p.muted : p.success, [\"--tw-ring-color\" as string]: accentColor }}\n              >\n                {saving ? <Spinner size={s.icon - 1} /> : <CheckIcon size={s.icon} />}\n              </button>\n              <button\n                type=\"button\"\n                aria-label=\"Cancel\"\n                disabled={saving}\n                onMouseDown={(e) => e.preventDefault()}\n                onClick={cancel}\n                className=\"flex cursor-pointer items-center justify-center rounded-md border-none outline-none focus-visible:ring-2 disabled:cursor-default\"\n                style={{ width: s.font + 14, height: s.font + 14, background: p.hover, color: p.muted, [\"--tw-ring-color\" as string]: accentColor }}\n              >\n                <XIcon size={s.icon} />\n              </button>\n            </div>\n          )}\n        </div>\n        <AnimatePresence initial={false}>\n          {error && (\n            <motion.span\n              id={`${uid}-error`}\n              role=\"alert\"\n              initial={{ opacity: 0, height: 0 }}\n              animate={{ opacity: 1, height: \"auto\" }}\n              exit={{ opacity: 0, height: 0 }}\n              transition={{ duration: 0.15 }}\n              className=\"overflow-hidden\"\n              style={{ color: p.error, fontSize: s.font - 2.5 }}\n            >\n              {error}\n            </motion.span>\n          )}\n        </AnimatePresence>\n      </motion.div>\n    );\n  }\n\n  const isEmpty = committed.length === 0;\n  const pencil = (\n    <span className=\"flex shrink-0 items-center justify-center opacity-0 transition-opacity duration-150 group-hover/inline-edit:opacity-100 group-focus-visible/inline-edit:opacity-100\" style={{ color: p.muted }}>\n      <PencilIcon size={s.icon - 2} />\n    </span>\n  );\n  const sharedStyle: React.CSSProperties = { gap: 6, padding: `${s.padY}px ${s.padX}px`, margin: `0 -${s.padX}px`, borderRadius: s.radius, fontSize: s.font, color: isEmpty ? p.muted : p.text, fontFamily: \"Inter, sans-serif\", fontStyle: isEmpty ? \"italic\" : undefined };\n  const hoverHandlers = {\n    onMouseEnter: (e: React.MouseEvent<HTMLElement>) => (e.currentTarget.style.background = p.hover),\n    onMouseLeave: (e: React.MouseEvent<HTMLElement>) => (e.currentTarget.style.background = \"transparent\"),\n  };\n\n  // A pencil icon that's the *only* way to start editing is a real nested\n  // <button>, so the whole row has to be a plain (non-interactive) <span>\n  // instead of a <button> itself — an interactive element can't contain\n  // another one without breaking HTML/ARIA nesting rules.\n  if (!editOnClick) {\n    return (\n      <span className={cn(\"group/inline-edit inline-flex max-w-full items-center\", className)} style={sharedStyle} {...hoverHandlers}>\n        <span className=\"min-w-0 truncate whitespace-pre-wrap\">{isEmpty ? emptyText : committed}</span>\n        <button\n          type=\"button\"\n          aria-label=\"Edit\"\n          onClick={startEdit}\n          className=\"border-none bg-transparent p-0 outline-none focus-visible:ring-2\"\n          style={{ [\"--tw-ring-color\" as string]: accentColor }}\n        >\n          {pencil}\n        </button>\n      </span>\n    );\n  }\n\n  return (\n    <button\n      type=\"button\"\n      onClick={startEdit}\n      className={cn(\"group/inline-edit inline-flex max-w-full cursor-pointer items-center border-none bg-transparent text-left outline-none focus-visible:ring-2\", className)}\n      style={{ ...sharedStyle, [\"--tw-ring-color\" as string]: accentColor }}\n      {...hoverHandlers}\n    >\n      <span className=\"min-w-0 truncate whitespace-pre-wrap\">{isEmpty ? emptyText : committed}</span>\n      {pencil}\n    </button>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}