{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "textarea-16",
  "title": "Textarea with character limit",
  "description": "A counter of characters left that warns near the limit and turns red at it.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/label.json",
    "https://ui.ndekocode.dev/r/textarea.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-character-limit.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/textarea/textarea-16.tsx",
      "content": "'use client';\n\nimport {\n  AnimatePresence,\n  motion,\n  type Transition,\n  type Variants,\n} from 'motion/react';\nimport { useId, useState, type ChangeEvent } from 'react';\n\nimport { useCharacterLimit } from '@/registry/ndeko/hooks/use-character-limit';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { Label } from '@/registry/ndeko/ui/label';\nimport { Textarea } from '@/registry/ndeko/ui/textarea';\n\nconst MAX_LENGTH = 180;\nconst NEAR_LIMIT = 20;\nconst DIGIT_SHIFT_PX = 8;\n\nconst DIGIT_TRANSITION: Transition = { duration: 0.18, ease: [0.2, 0, 0, 1] };\n\nconst DIGIT_VARIANTS: Variants = {\n  enter: (direction: number) => {\n    const offset = direction * DIGIT_SHIFT_PX;\n    return { opacity: 0, y: offset };\n  },\n  visible: { opacity: 1, y: 0 },\n  exit: (direction: number) => {\n    const offset = direction * DIGIT_SHIFT_PX;\n    return { opacity: 0, y: -offset };\n  },\n};\n\nconst FADE_VARIANTS: Variants = {\n  enter: { opacity: 0 },\n  visible: { opacity: 1 },\n  exit: { opacity: 0 },\n};\n\nfunction digitVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return DIGIT_VARIANTS;\n}\n\nfunction directionOf(previous: number, next: number) {\n  if (next < previous) {\n    return -1;\n  }\n  return 1;\n}\n\nfunction counterToneOf(remaining: number) {\n  if (remaining === 0) {\n    return 'text-destructive-ink';\n  }\n  if (remaining <= NEAR_LIMIT) {\n    return 'text-warning-foreground';\n  }\n  return 'text-muted-foreground';\n}\n\nfunction remainingLabelOf(remaining: number) {\n  if (remaining === 0) {\n    return 'No characters left';\n  }\n  if (remaining === 1) {\n    return '1 character left';\n  }\n  return `${remaining} characters left`;\n}\n\nfunction remainingFigureOf(remaining: number) {\n  if (remaining === 0) {\n    return 'No';\n  }\n  return String(remaining);\n}\n\nfunction remainingWordsOf(remaining: number) {\n  if (remaining === 1) {\n    return 'character left';\n  }\n  return 'characters left';\n}\n\nexport default function Textarea16() {\n  const id = useId();\n  const counterId = `${id}-counter`;\n  const [direction, setDirection] = useState(-1);\n  const { value, characterCount, handleChange, maxLength } = useCharacterLimit({\n    maxLength: MAX_LENGTH,\n  });\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const digitVariants = digitVariantsOf(shouldReduceMotion);\n  const remaining = maxLength - characterCount;\n\n  function changeBio(event: ChangeEvent<HTMLTextAreaElement>) {\n    const nextRemaining = maxLength - event.currentTarget.value.length;\n    setDirection(directionOf(remaining, nextRemaining));\n    handleChange(event);\n  }\n\n  return (\n    <div className='flex w-full max-w-xs flex-col gap-2'>\n      <Label htmlFor={id}>Bio</Label>\n      <Textarea\n        id={id}\n        value={value}\n        maxLength={maxLength}\n        onChange={changeBio}\n        placeholder='A few words about you'\n        aria-describedby={counterId}\n      />\n      <p\n        id={counterId}\n        className={cn(\n          'text-end text-xs tabular-nums transition-colors duration-150',\n          counterToneOf(remaining)\n        )}\n      >\n        <span className='sr-only'>{remainingLabelOf(remaining)}</span>\n        <span aria-hidden='true'>\n          <span className='relative inline-flex'>\n            <AnimatePresence\n              mode='popLayout'\n              initial={false}\n              custom={direction}\n            >\n              <motion.span\n                key={remaining}\n                className='inline-flex'\n                custom={direction}\n                variants={digitVariants}\n                initial='enter'\n                animate='visible'\n                exit='exit'\n                transition={DIGIT_TRANSITION}\n              >\n                {remainingFigureOf(remaining)}\n              </motion.span>\n            </AnimatePresence>\n          </span>{' '}\n          {remainingWordsOf(remaining)}\n        </span>\n      </p>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "textarea"
  ],
  "type": "registry:component"
}