{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "file-upload-01",
  "title": "Image upload button",
  "description": "Picks an image, previews it and lets you remove it.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/avatar.json",
    "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-file-upload.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/file-upload/file-upload-01.tsx",
      "content": "'use client';\n\nimport {\n  AnimatePresence,\n  motion,\n  type Transition,\n  type Variants,\n} from 'motion/react';\nimport { useEffect, useState } from 'react';\n\nimport { useFileUpload } from '@/registry/ndeko/hooks/use-file-upload';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { CircleUserRoundIcon } from '@/registry/ndeko/lib/icons';\nimport {\n  Avatar,\n  AvatarFallback,\n  AvatarImage,\n} from '@/registry/ndeko/ui/avatar';\nimport { Button } from '@/registry/ndeko/ui/button';\n\nconst SWAP_TRANSITION: Transition = { duration: 0.15, ease: [0.2, 0, 0, 1] };\nconst RESIZE_TRANSITION: Transition = {\n  type: 'spring',\n  bounce: 0,\n  duration: 0.3,\n};\nconst INSTANT_TRANSITION: Transition = { duration: 0 };\n\nconst LABEL_VARIANTS: Variants = {\n  enter: { opacity: 0, filter: 'blur(2px)' },\n  visible: { opacity: 1, filter: 'blur(0px)' },\n  exit: { opacity: 0, filter: 'blur(2px)' },\n};\n\nconst FADE_VARIANTS: Variants = {\n  enter: { opacity: 0 },\n  visible: { opacity: 1 },\n  exit: { opacity: 0 },\n};\n\nfunction labelVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return FADE_VARIANTS;\n  }\n  return LABEL_VARIANTS;\n}\n\nfunction resizeTransitionOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return INSTANT_TRANSITION;\n  }\n  return RESIZE_TRANSITION;\n}\n\nfunction uploadLabelOf(hasImage: boolean) {\n  if (hasImage) {\n    return 'Change image';\n  }\n  return 'Upload image';\n}\n\nfunction useLabelWidth() {\n  const [label, setLabel] = useState<HTMLSpanElement | null>(null);\n  const [labelWidth, setLabelWidth] = useState<number>();\n\n  useEffect(() => {\n    if (!label) {\n      return;\n    }\n    const observer = new ResizeObserver((entries) => {\n      const [entry] = entries;\n      setLabelWidth(entry.contentRect.width);\n    });\n    observer.observe(label);\n    return () => observer.disconnect();\n  }, [label]);\n\n  return { measureLabel: setLabel, labelWidth };\n}\n\nexport default function FileUpload01() {\n  const [{ files }, { removeFile, openFileDialog, getInputProps }] =\n    useFileUpload({\n      accept: 'image/*',\n    });\n  const { measureLabel, labelWidth } = useLabelWidth();\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const labelVariants = labelVariantsOf(shouldReduceMotion);\n  const resizeTransition = resizeTransitionOf(shouldReduceMotion);\n\n  const image = files[0];\n  const previewUrl = image?.preview;\n  const fileName = image?.file.name;\n  const hasImage = Boolean(image);\n  const label = uploadLabelOf(hasImage);\n\n  function removeImage() {\n    if (!image) {\n      return;\n    }\n    removeFile(image.id);\n  }\n\n  return (\n    <div className='flex flex-col items-center gap-2'>\n      <div className='inline-flex items-center gap-2 align-top'>\n        <Avatar className='rounded-md'>\n          {previewUrl && (\n            <AvatarImage src={previewUrl} alt='Preview of uploaded image' />\n          )}\n          <AvatarFallback>\n            <CircleUserRoundIcon className='opacity-60' />\n          </AvatarFallback>\n        </Avatar>\n        <div className='relative inline-block'>\n          <Button onClick={openFileDialog} aria-haspopup='dialog'>\n            <motion.span\n              className='flex overflow-hidden'\n              initial={false}\n              animate={{ width: labelWidth }}\n              transition={resizeTransition}\n            >\n              <span ref={measureLabel} className='relative flex w-max shrink-0'>\n                <AnimatePresence mode='popLayout' initial={false}>\n                  <motion.span\n                    key={label}\n                    className='flex'\n                    variants={labelVariants}\n                    initial='enter'\n                    animate='visible'\n                    exit='exit'\n                    transition={SWAP_TRANSITION}\n                  >\n                    {label}\n                  </motion.span>\n                </AnimatePresence>\n              </span>\n            </motion.span>\n          </Button>\n          <input\n            {...getInputProps()}\n            className='sr-only'\n            aria-label='Upload image file'\n            tabIndex={-1}\n          />\n        </div>\n      </div>\n      {fileName && (\n        <div className='inline-flex gap-2 text-xs'>\n          <p className='text-muted-foreground truncate' aria-live='polite'>\n            {fileName}\n          </p>\n          <Button\n            variant='link'\n            size='sm'\n            onClick={removeImage}\n            aria-label={`Remove ${fileName}`}\n            className='text-destructive-ink h-auto px-0'\n          >\n            Remove\n          </Button>\n        </div>\n      )}\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "file-upload"
  ],
  "type": "registry:component"
}