{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "file-model",
  "type": "registry:ui",
  "title": "File Model",
  "description": "The rules a file picker applies before anything is uploaded: does the file match what the field accepts, is it small enough, and is there room for another. Pure, so a server can apply the same rules to the same files and a reader is never told \"yes\" in the…",
  "author": "ofri-peretz <https://github.com/ofri-peretz>",
  "categories": [
    "form",
    "primitive"
  ],
  "dependencies": [],
  "registryDependencies": [
    "https://ds.interlace.tools/r/interlace-base.json"
  ],
  "files": [
    {
      "path": "registry/interlace-ui/file-model.ts",
      "target": "components/ui/file-model.ts",
      "type": "registry:ui",
      "content": "export interface FileLike {\n  name: string;\n  size: number;\n  type: string;\n}\n\nexport type FileRejectionReason = 'type' | 'size' | 'count';\n\nexport interface FileRejection<F extends FileLike = FileLike> {\n  file: F;\n  reason: FileRejectionReason;\n}\n\nexport interface FileRules {\n  /** The `accept` attribute's value. Empty or missing takes every file. */\n  accept?: string;\n  /** The largest file, in bytes. Missing means no limit. */\n  maxSize?: number;\n  /** How many files the field holds in all. Missing means no limit. */\n  maxFiles?: number;\n}\n\nexport interface FileCheck<F extends FileLike = FileLike> {\n  accepted: F[];\n  rejected: FileRejection<F>[];\n}\n\n/** Whether `file` matches the `accept` attribute value. */\nexport function acceptsFile(file: FileLike, accept: string | undefined): boolean {\n  const patterns = (accept ?? '')\n    .split(',')\n    .map((p) => p.trim().toLowerCase())\n    .filter(Boolean);\n  if (patterns.length === 0) return true;\n  const name = file.name.toLowerCase();\n  const type = file.type.toLowerCase();\n  return patterns.some((pattern) => {\n    if (pattern.startsWith('.')) return name.endsWith(pattern);\n    if (pattern.endsWith('/*')) return type.startsWith(pattern.slice(0, -1));\n    return type === pattern;\n  });\n}\n\n/**\n * Sorts new files into those the field takes and those it turns away, and\n * why. `held` is how many files the field already holds, so `maxFiles`\n * counts them too. Files are taken in order until the field is full.\n */\nexport function checkFiles<F extends FileLike>(files: readonly F[], rules: FileRules = {}, held = 0): FileCheck<F> {\n  const accepted: F[] = [];\n  const rejected: FileRejection<F>[] = [];\n  for (const file of files) {\n    if (!acceptsFile(file, rules.accept)) rejected.push({ file, reason: 'type' });\n    else if (rules.maxSize !== undefined && file.size > rules.maxSize) rejected.push({ file, reason: 'size' });\n    else if (rules.maxFiles !== undefined && held + accepted.length >= rules.maxFiles)\n      rejected.push({ file, reason: 'count' });\n    else accepted.push(file);\n  }\n  return { accepted, rejected };\n}\n\nconst UNITS = ['byte', 'kilobyte', 'megabyte', 'gigabyte'] as const;\n\n/** A size as a person reads it: `512 bytes`, `2.4 MB`, in decimal units. */\nexport function formatBytes(bytes: number, locale?: string): string {\n  let value = Math.max(0, bytes);\n  let unit = 0;\n  while (value >= 1000 && unit < UNITS.length - 1) {\n    value /= 1000;\n    unit += 1;\n  }\n  return new Intl.NumberFormat(locale, {\n    style: 'unit',\n    unit: UNITS[unit],\n    unitDisplay: unit === 0 ? 'long' : 'short',\n    maximumFractionDigits: unit === 0 || value >= 100 ? 0 : 1,\n  }).format(value);\n}\n\n/**\n * @interlace/ui — file-model\n *\n * The rules a file picker applies before anything is uploaded: does the file\n * match what the field accepts, is it small enough, and is there room for\n * another. Pure, so a server can apply the same rules to the same files and\n * a reader is never told \"yes\" in the browser and \"no\" by the server.\n *\n * ## accept\n *\n * The same string the `accept` attribute takes, so one value serves both the\n * native picker and this check: comma-separated extensions (`.png`), exact\n * types (`image/png`) and wildcards (`image/*`). The native picker only\n * suggests; a drop, or a picker set to \"All files\", can hand over anything,\n * which is why the check runs again here. An empty `accept` takes every file.\n *\n * ## Sizes\n *\n * `formatBytes` writes a size the way a person reads one (`2.4 MB`), in\n * decimal units like the operating systems' own file browsers, in the\n * reader's locale.\n */\n\n/** What a file has that the rules read. A browser `File` fits. */\n\n// @interlace/file-model v1.0.0 — Interlace design system.\n// Docs, props and live preview: https://ds.interlace.tools/c/file-model\n// What changed since: https://ds.interlace.tools/c/file-model#history\n// Generated banner — keep it, the upgrade diff reads this version.\n"
    }
  ],
  "meta": {
    "tier": "primitive",
    "client": false,
    "minViewport": null,
    "loading": false,
    "version": "1.0.0",
    "since": null
  },
  "docs": "## @interlace/file-model\n\nInstalled to `components/ui/file-model.ts`.\n\n```tsx\nimport { /* … */ } from '@/components/ui/file-model';\n```\n\nProps, a11y contract, live preview and source: https://ds.interlace.tools/c/file-model\n\nRequires the `@interlace/interlace-base` CSS baseline (installed automatically as a registry dependency). For the Interlace look, also add `@interlace/theme`."
}
