{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "file-upload-15",
  "title": "Folder that takes files",
  "description": "A drawn folder whose flap tips open while files are dragged over it; dropped files fall in as sheets and a list under it shows each upload with its progress.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/demonstration.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/live-region.json",
    "https://ui.ndekocode.dev/r/motion-presets.json",
    "https://ui.ndekocode.dev/r/progress.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/use-autoplay.json",
    "https://ui.ndekocode.dev/r/use-kept-height.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json",
    "https://ui.ndekocode.dev/r/utils.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/file-upload/file-upload-15.tsx",
      "content": "'use client';\n\nimport { AnimatePresence, motion } from 'motion/react';\nimport { useEffect, useRef, useState, type DragEvent } from 'react';\n\nimport {\n  useAutoplay,\n  type AutoplayStep,\n} from '@/registry/ndeko/hooks/use-autoplay';\nimport { useKeptHeight } from '@/registry/ndeko/hooks/use-kept-height';\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport { hold } from '@/registry/ndeko/lib/demonstration';\nimport {\n  CheckIcon,\n  FileSpreadsheetIcon,\n  FileTextIcon,\n  ImageIcon,\n  UploadIcon,\n  XIcon,\n} from '@/registry/ndeko/lib/icons';\nimport { rowPresenceOf, springOf } from '@/registry/ndeko/lib/motion-presets';\nimport { cn } from '@/registry/ndeko/lib/utils';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport { LiveRegion } from '@/registry/ndeko/ui/live-region';\nimport { Progress } from '@/registry/ndeko/ui/progress';\n\ntype UploadEntry = {\n  id: string;\n  name: string;\n  bytes: number;\n  startsDone: boolean;\n};\n\nconst MAX_ENTRIES = 6;\nconst VISIBLE_SHEETS = 3;\nconst TICK_MS = 200;\n\nconst DEMO_REPORT = { name: 'Q3 report.pdf', bytes: 1_240_000 };\nconst DEMO_PHOTO = { name: 'Team photo.png', bytes: 3_410_000 };\nconst DEMO_BUDGET = { name: 'Budget.xlsx', bytes: 310_000 };\n\nfunction entryOf(\n  name: string,\n  bytes: number,\n  startsDone: boolean,\n  id = name\n): UploadEntry {\n  return { id, name, bytes, startsDone };\n}\n\nconst RESTING_ENTRIES: UploadEntry[] = [\n  entryOf(DEMO_REPORT.name, DEMO_REPORT.bytes, true),\n  entryOf(DEMO_PHOTO.name, DEMO_PHOTO.bytes, true),\n  entryOf(DEMO_BUDGET.name, DEMO_BUDGET.bytes, true),\n];\n\nconst SHEET_POSES = [\n  { x: 0, y: -10, rotate: 0 },\n  { x: -12, y: -6, rotate: -5 },\n  { x: 12, y: -4, rotate: 5 },\n];\n\nconst FALL_FROM = { x: 0, y: -96, rotate: -10, opacity: 0 };\n\nconst BACK_FILL = 'bg-[color-mix(in_oklab,var(--primary)_32%,var(--card))]';\n\nconst TAB = cn('absolute start-0 top-0 h-5 w-16 rounded-t-md', BACK_FILL);\n\nconst BACK = cn(\n  'absolute inset-x-0 top-3 bottom-0 rounded-lg rounded-ss-none',\n  BACK_FILL\n);\n\nconst SHEET =\n  'bg-card hairline-ring ring-border absolute inset-x-6 bottom-3 flex h-[4.5rem] origin-bottom flex-col gap-1.5 rounded-md p-2 shadow-[0_1px_2px_rgb(0_0_0/0.04)]';\n\nconst FLAP = [\n  'hairline-ring ring-primary/30 absolute inset-x-0 bottom-0 z-20 flex h-[4.75rem] origin-bottom flex-col items-start justify-end rounded-lg p-3 text-start',\n  'bg-[color-mix(in_oklab,var(--primary)_14%,var(--card))] transition-transform duration-300 ease-(--ease-out-strong)',\n  '[transform:perspective(500px)_rotateX(0deg)] group-data-[tipped=true]/folder:[transform:perspective(500px)_rotateX(-24deg)]',\n].join(' ');\n\nconst DROP_ZONE = [\n  'hairline-border border-foreground/20 relative flex flex-col items-center gap-3 rounded-xl border-dashed px-4 pt-12 pb-4',\n  'transition-[background-color,border-color] duration-200 ease-(--ease-out-strong)',\n  'data-[over=true]:border-primary/50 data-[over=true]:bg-primary/[0.04]',\n].join(' ');\n\nfunction fileCountOf(count: number) {\n  if (count === 0) {\n    return 'No files yet';\n  }\n  if (count === 1) {\n    return '1 file';\n  }\n  return `${count} files`;\n}\n\nfunction sizeLabelOf(bytes: number) {\n  const megabytes = bytes / 1_000_000;\n  if (megabytes >= 1) {\n    return `${megabytes.toFixed(1)} MB`;\n  }\n  const kilobytes = Math.round(bytes / 1000);\n  return `${kilobytes} KB`;\n}\n\nfunction kindIconOf(name: string) {\n  const lower = name.toLowerCase();\n  if (lower.endsWith('.xlsx') || lower.endsWith('.csv')) {\n    return <FileSpreadsheetIcon className='size-4' />;\n  }\n  const isPicture = /\\.(png|jpe?g|gif|webp|svg)$/.test(lower);\n  if (isPicture) {\n    return <ImageIcon className='size-4' />;\n  }\n  return <FileTextIcon className='size-4' />;\n}\n\nfunction tipOf(isTipped: boolean) {\n  if (isTipped) {\n    return 'true';\n  }\n  return 'false';\n}\n\nfunction overOf(isOver: boolean) {\n  if (isOver) {\n    return 'true';\n  }\n  return 'false';\n}\n\nfunction isFileDrag(event: DragEvent<HTMLElement>) {\n  const types = Array.from(event.dataTransfer.types);\n  return types.includes('Files');\n}\n\ntype SheetProps = {\n  entry: UploadEntry;\n  slot: number;\n};\n\nfunction Sheet({ entry, slot }: SheetProps) {\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const transition = springOf(shouldReduceMotion);\n  const pose = SHEET_POSES[slot];\n\n  return (\n    <motion.span\n      aria-hidden='true'\n      className={SHEET}\n      style={{ zIndex: 10 - slot }}\n      initial={FALL_FROM}\n      animate={{ ...pose, opacity: 1 }}\n      exit={{ opacity: 0, y: 10 }}\n      transition={transition}\n    >\n      <span className='truncate text-[10px] leading-none font-medium'>\n        {entry.name}\n      </span>\n      <span className='bg-foreground/10 h-1 w-3/4 rounded-full' />\n      <span className='bg-foreground/10 h-1 rounded-full' />\n    </motion.span>\n  );\n}\n\ntype FolderProps = {\n  entries: UploadEntry[];\n  isTipped: boolean;\n};\n\nfunction Folder({ entries, isTipped }: FolderProps) {\n  const sheetEntries = entries.slice(-VISIBLE_SHEETS);\n  const lastIndex = sheetEntries.length - 1;\n\n  return (\n    <div\n      aria-hidden='true'\n      data-tipped={tipOf(isTipped)}\n      className='group/folder relative h-28 w-40 shrink-0'\n    >\n      <span className={TAB} />\n      <span className={BACK} />\n      <AnimatePresence initial={false}>\n        {sheetEntries.map((entry, index) => (\n          <Sheet key={entry.id} entry={entry} slot={lastIndex - index} />\n        ))}\n      </AnimatePresence>\n      <span className={FLAP}>\n        <span className='text-[13px] font-medium'>Project files</span>\n        <span className='text-foreground/70 text-xs tabular-nums'>\n          {fileCountOf(entries.length)}\n        </span>\n      </span>\n    </div>\n  );\n}\n\ntype UploadRowProps = {\n  entry: UploadEntry;\n  isRunning: boolean;\n  onRemove: () => void;\n};\n\nfunction stepOf(name: string) {\n  return 7 + (name.length % 5);\n}\n\nfunction startProgressOf(startsDone: boolean) {\n  if (startsDone) {\n    return 100;\n  }\n  return 0;\n}\n\nfunction UploadRow({ entry, isRunning, onRemove }: UploadRowProps) {\n  const [progress, setProgress] = useState(startProgressOf(entry.startsDone));\n  const isDone = progress >= 100;\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const presence = rowPresenceOf(shouldReduceMotion);\n\n  useEffect(() => {\n    if (isDone || !isRunning) {\n      return;\n    }\n    const step = stepOf(entry.name);\n    const timer = window.setInterval(() => {\n      setProgress((current) => Math.min(100, current + step));\n    }, TICK_MS);\n    return () => window.clearInterval(timer);\n  }, [isDone, isRunning, entry.name]);\n\n  return (\n    <motion.li className='overflow-hidden' {...presence}>\n      <div className='bg-foreground/[0.04] my-0.5 flex items-center gap-3 rounded-xl p-2 pe-1'>\n        <span\n          aria-hidden='true'\n          className='keycap-3d text-muted-foreground flex size-9 shrink-0 items-center justify-center rounded-lg'\n        >\n          {kindIconOf(entry.name)}\n        </span>\n        <div className='flex min-w-0 flex-1 flex-col gap-1'>\n          <div className='flex items-baseline justify-between gap-2'>\n            <span className='truncate text-[13px] font-medium'>\n              {entry.name}\n            </span>\n            <span className='text-muted-foreground shrink-0 text-xs tabular-nums'>\n              {sizeLabelOf(entry.bytes)}\n            </span>\n          </div>\n          <Progress\n            value={progress}\n            aria-label={`Uploading ${entry.name}`}\n            className='gap-0'\n          />\n        </div>\n        <span className='text-muted-foreground flex w-8 shrink-0 justify-center text-xs tabular-nums'>\n          {isDone && <CheckIcon className='text-success-foreground size-4' />}\n          {!isDone && `${progress}%`}\n        </span>\n        <Button\n          variant='ghost'\n          size='icon-sm'\n          aria-label={`Remove ${entry.name}`}\n          onClick={onRemove}\n          className='text-muted-foreground hover:text-foreground'\n        >\n          <XIcon />\n        </Button>\n      </div>\n    </motion.li>\n  );\n}\n\nexport default function FileUpload15() {\n  const [entries, setEntries] = useState(RESTING_ENTRIES);\n  const [isOver, setIsOver] = useState(false);\n  const [isDemoTipped, setIsDemoTipped] = useState(false);\n  const [announcement, setAnnouncement] = useState('');\n  const inputRef = useRef<HTMLInputElement>(null);\n  const dragDepth = useRef(0);\n  const addedCount = useRef(0);\n  const isTipped = isOver || isDemoTipped;\n  const isFull = entries.length >= MAX_ENTRIES;\n\n  function addFiles(files: { name: string; bytes: number }[]) {\n    const added = files.map((file) => {\n      addedCount.current += 1;\n      const id = `${file.name}-${addedCount.current}`;\n      return entryOf(file.name, file.bytes, false, id);\n    });\n    setEntries((current) => [...current, ...added].slice(0, MAX_ENTRIES));\n  }\n\n  function addDemoFiles(files: { name: string; bytes: number }[]) {\n    const added = files.map((file) => entryOf(file.name, file.bytes, false));\n    setEntries((current) => [...current, ...added]);\n    setIsDemoTipped(false);\n  }\n\n  function addFromPerson(files: File[]) {\n    const mapped = files.map((file) => ({ name: file.name, bytes: file.size }));\n    addFiles(mapped);\n    setAnnouncement(`${fileCountOf(mapped.length)} added to the folder`);\n  }\n\n  function removeEntry(id: string) {\n    setEntries((current) => current.filter((entry) => entry.id !== id));\n  }\n\n  function enterZone(event: DragEvent<HTMLElement>) {\n    if (!isFileDrag(event)) {\n      return;\n    }\n    dragDepth.current += 1;\n    setIsOver(true);\n  }\n\n  function leaveZone() {\n    dragDepth.current = Math.max(0, dragDepth.current - 1);\n    if (dragDepth.current === 0) {\n      setIsOver(false);\n    }\n  }\n\n  function overZone(event: DragEvent<HTMLElement>) {\n    if (!isFileDrag(event)) {\n      return;\n    }\n    event.preventDefault();\n  }\n\n  function dropOnZone(event: DragEvent<HTMLElement>) {\n    event.preventDefault();\n    dragDepth.current = 0;\n    setIsOver(false);\n    const dropped = Array.from(event.dataTransfer.files);\n    addFromPerson(dropped);\n  }\n\n  function pickFiles(event: React.ChangeEvent<HTMLInputElement>) {\n    const chosen = Array.from(event.currentTarget.files ?? []);\n    event.currentTarget.value = '';\n    addFromPerson(chosen);\n  }\n\n  function startOver() {\n    setEntries(RESTING_ENTRIES);\n    setIsDemoTipped(false);\n  }\n\n  const steps: AutoplayStep[] = [\n    { after: 2600, run: () => setEntries([]) },\n    { after: 900, run: () => setIsDemoTipped(true) },\n    { after: 1000, run: () => addDemoFiles([DEMO_REPORT]) },\n    { after: 1500, run: () => setIsDemoTipped(true) },\n    { after: 1000, run: () => addDemoFiles([DEMO_PHOTO, DEMO_BUDGET]) },\n    { after: 5200, run: hold },\n  ];\n  const { ref, isInView } = useAutoplay(steps, {\n    loopAfter: 2600,\n    onReset: startOver,\n  });\n\n  useKeptHeight(ref);\n\n  return (\n    <div\n      ref={ref}\n      className='flex min-h-[27rem] w-full max-w-sm flex-col gap-3'\n    >\n      <div\n        data-over={overOf(isOver)}\n        onDragEnter={enterZone}\n        onDragLeave={leaveZone}\n        onDragOver={overZone}\n        onDrop={dropOnZone}\n        className={DROP_ZONE}\n      >\n        <input\n          ref={inputRef}\n          type='file'\n          multiple\n          tabIndex={-1}\n          aria-hidden='true'\n          onChange={pickFiles}\n          className='sr-only'\n        />\n        <Folder entries={entries} isTipped={isTipped} />\n        <div className='flex flex-col items-center gap-1 text-center'>\n          <p className='text-[13px] font-medium'>Drop files on the folder</p>\n          <p className='text-muted-foreground text-xs'>\n            Up to {MAX_ENTRIES} files\n          </p>\n        </div>\n        <Button\n          variant='outline'\n          size='sm'\n          disabled={isFull}\n          onClick={() => inputRef.current?.click()}\n        >\n          <UploadIcon className='size-3.5 opacity-60' />\n          Choose files\n        </Button>\n      </div>\n      <ul aria-label='Files in the folder' className='flex flex-col'>\n        <AnimatePresence initial={false}>\n          {entries.map((entry) => (\n            <UploadRow\n              key={entry.id}\n              entry={entry}\n              isRunning={isInView}\n              onRemove={() => removeEntry(entry.id)}\n            />\n          ))}\n        </AnimatePresence>\n      </ul>\n      <LiveRegion message={announcement} />\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "file-upload"
  ],
  "type": "registry:component"
}