{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "avatar-upload",
  "title": "Avatar Upload",
  "description": "A profile photo uploader with round cropping and a progress ring.",
  "dependencies": [
    "@uploadcn/core@^0.1.0",
    "@uploadcn/react@^0.1.0",
    "cn"
  ],
  "registryDependencies": [
    "@uploadcn/image-cropper",
    "@uploadcn/upload",
    "@uploadcn/use-crop-queue"
  ],
  "files": [
    {
      "path": "registry/default/blocks/avatar-upload.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { type UploadAdapter, type UploadItem } from \"@uploadcn/core\"\nimport {\n  Upload as UploadPrimitive,\n  useUploadContext,\n  useUploadSelector,\n} from \"@uploadcn/react\"\nimport { cn } from \"cn\"\n\nimport { IconPlaceholder } from \"@/components/icon-placeholder\"\nimport { useCropQueue } from \"@/registry/default/hooks/use-crop-queue\"\nimport { ImageCropper } from \"@/registry/default/ui/image-cropper\"\nimport {\n  NativeButton,\n  Upload,\n  UploadTrigger,\n} from \"@/registry/default/ui/upload\"\n\ninterface AvatarUploadProps<TResult> {\n  adapter?: UploadAdapter<TResult>\n  /** Current avatar URL, shown until a new one is uploaded. */\n  src?: string | null\n  /** Output size in pixels (square). Default 512. */\n  size?: number\n  /** Called with the upload result, or `null` when the avatar is removed. */\n  onChange?: (item: UploadItem<TResult> | null) => void\n  name?: string\n  className?: string\n}\n\nfunction AvatarUpload<TResult = unknown>({\n  adapter,\n  src,\n  size = 512,\n  onChange,\n  name,\n  className,\n}: AvatarUploadProps<TResult>) {\n  const crops = useCropQueue()\n  const { request } = crops\n  const transform = React.useCallback((file: File) => request(file), [request])\n\n  return (\n    <Upload\n      adapter={adapter}\n      accept=\"image/*\"\n      maxSize={10 * 1000 * 1000}\n      multiple={false}\n      transform={transform}\n      onSuccess={onChange}\n      name={name}\n      className={cn(\"w-auto flex-row items-center gap-4\", className)}\n    >\n      <AvatarPreview fallbackSrc={src ?? null} />\n      <div className=\"flex flex-col gap-2\">\n        <div className=\"flex gap-2\">\n          <UploadTrigger>\n            <IconPlaceholder\n              lucide=\"CameraIcon\"\n              tabler=\"IconCamera\"\n              hugeicons=\"Camera01Icon\"\n              phosphor=\"CameraIcon\"\n              remixicon=\"RiCameraLine\"\n              data-icon=\"inline-start\"\n            />\n            {src ? \"Change photo\" : \"Upload photo\"}\n          </UploadTrigger>\n          <AvatarRemove onRemove={() => onChange?.(null)} />\n        </div>\n        <AvatarHint />\n      </div>\n      <ImageCropper\n        file={crops.current}\n        cropShape=\"round\"\n        aspect={1}\n        output={{\n          maxWidth: size,\n          maxHeight: size,\n          type: \"image/webp\",\n          quality: 0.9,\n        }}\n        onCrop={crops.complete}\n        onCancel={crops.skip}\n        title=\"Crop your photo\"\n      />\n    </Upload>\n  )\n}\n\nfunction useAvatarItem() {\n  const { uploader } = useUploadContext()\n  return useUploadSelector(uploader, (state) => state.items.at(-1) ?? null)\n}\n\nfunction AvatarPreview({ fallbackSrc }: { fallbackSrc: string | null }) {\n  const item = useAvatarItem()\n  const { openFileDialog } = useUploadContext()\n  const radius = 46\n  const circumference = 2 * Math.PI * radius\n  const uploading =\n    item &&\n    (item.status === \"uploading\" ||\n      item.status === \"queued\" ||\n      item.status === \"validating\")\n\n  return (\n    <button\n      type=\"button\"\n      onClick={openFileDialog}\n      aria-label=\"Change profile photo\"\n      className=\"group/avatar relative size-20 shrink-0 rounded-full outline-none focus-visible:ring-3 focus-visible:ring-ring/50\"\n    >\n      <span className=\"absolute inset-1 flex items-center justify-center overflow-hidden rounded-full bg-muted text-muted-foreground\">\n        {item && item.status !== \"rejected\" && item.status !== \"cancelled\" ? (\n          <UploadPrimitive.Item\n            item={item}\n            render={<span className=\"contents\" />}\n          >\n            <UploadPrimitive.Preview\n              thumbnailSize={256}\n              className=\"size-full animate-in object-cover duration-300 fade-in-0 motion-reduce:animate-none\"\n            />\n          </UploadPrimitive.Item>\n        ) : fallbackSrc ? (\n          // eslint-disable-next-line @next/next/no-img-element -- remote avatar URL of unknown origin\n          <img src={fallbackSrc} alt=\"\" className=\"size-full object-cover\" />\n        ) : (\n          <IconPlaceholder\n            lucide=\"UserIcon\"\n            tabler=\"IconUser\"\n            hugeicons=\"UserIcon\"\n            phosphor=\"UserIcon\"\n            remixicon=\"RiUserLine\"\n            className=\"size-8\"\n            aria-hidden\n          />\n        )}\n        <span className=\"absolute inset-0 flex items-center justify-center bg-background/60 opacity-0 transition-opacity group-hover/avatar:opacity-100 group-focus-visible/avatar:opacity-100\">\n          <IconPlaceholder\n            lucide=\"CameraIcon\"\n            tabler=\"IconCamera\"\n            hugeicons=\"Camera01Icon\"\n            phosphor=\"CameraIcon\"\n            remixicon=\"RiCameraLine\"\n            className=\"size-5\"\n            aria-hidden\n          />\n        </span>\n      </span>\n      {item ? (\n        <UploadPrimitive.Item\n          item={item}\n          render={<span className=\"contents\" />}\n        >\n          <UploadPrimitive.Progress\n            className={cn(\n              \"pointer-events-none absolute inset-0 transition-opacity\",\n              !uploading && \"opacity-0\"\n            )}\n            render={(props, state) => (\n              <svg\n                {...(props as React.ComponentProps<\"svg\">)}\n                viewBox=\"0 0 100 100\"\n                aria-hidden={!uploading}\n              >\n                <circle\n                  cx=\"50\"\n                  cy=\"50\"\n                  r={radius}\n                  fill=\"none\"\n                  strokeWidth=\"4\"\n                  className=\"stroke-muted\"\n                />\n                <circle\n                  cx=\"50\"\n                  cy=\"50\"\n                  r={radius}\n                  fill=\"none\"\n                  strokeWidth=\"4\"\n                  strokeLinecap=\"round\"\n                  strokeDasharray={circumference}\n                  strokeDashoffset={circumference * (1 - state.percent / 100)}\n                  className=\"origin-center -rotate-90 stroke-primary transition-[stroke-dashoffset] duration-300 motion-reduce:transition-none\"\n                />\n              </svg>\n            )}\n          />\n        </UploadPrimitive.Item>\n      ) : null}\n    </button>\n  )\n}\n\nfunction AvatarRemove({ onRemove }: { onRemove: () => void }) {\n  const item = useAvatarItem()\n  const { uploader } = useUploadContext()\n  if (!item) return null\n  return (\n    <NativeButton\n      variant=\"ghost\"\n      size=\"sm\"\n      onClick={() => {\n        uploader.remove()\n        onRemove()\n      }}\n    >\n      Remove\n    </NativeButton>\n  )\n}\n\nfunction AvatarHint() {\n  const item = useAvatarItem()\n  if (item?.status === \"error\" || item?.status === \"rejected\") {\n    return (\n      <p className=\"inline-flex items-center gap-1 text-xs text-destructive\">\n        <IconPlaceholder\n          lucide=\"CircleAlertIcon\"\n          tabler=\"IconExclamationCircle\"\n          hugeicons=\"AlertCircleIcon\"\n          phosphor=\"WarningCircleIcon\"\n          remixicon=\"RiErrorWarningLine\"\n          className=\"size-3\"\n          aria-hidden\n        />\n        {item.issues[0]?.message ?? item.error?.message}\n      </p>\n    )\n  }\n  return (\n    <p className=\"text-xs text-muted-foreground\">\n      JPG, PNG or WebP. Cropped to a square.\n    </p>\n  )\n}\n\nexport { AvatarUpload, type AvatarUploadProps }\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "upload",
    "image",
    "forms"
  ],
  "type": "registry:block"
}