{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "cover-cropper",
  "title": "Cover cropper",
  "author": "pqoqubbw <https://zriz.dev>",
  "description": "A wide cover image the visitor drags and zooms in place, then saves as a JPEG.",
  "dependencies": [
    "@zriz/react@^0.2.0",
    "@zriz/core@^0.2.0"
  ],
  "registryDependencies": [
    "button",
    "https://zriz.dev/r/cropper.json"
  ],
  "files": [
    {
      "path": "registry/new-york/blocks/cover-cropper.tsx",
      "content": "\"use client\";\n\nimport type { Cropper as CropperApi, CropValue } from \"@zriz/react\";\nimport { useRef, useState } from \"react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { Button } from \"@/registry/new-york/ui/button\";\nimport {\n  Cropper,\n  CropperViewport,\n  CropperZoom,\n  cropToFile,\n} from \"@/registry/new-york/ui/cropper\";\n\ntype CoverCropperProps = {\n  aspect?: number;\n  className?: string;\n  defaultImage?: Blob | string | null;\n  onCrop?: (file: File) => void;\n  width?: number;\n};\n\ntype Mode = \"edit\" | \"loading\" | \"view\";\n\ntype Problem = \"open\" | \"save\" | null;\n\ntype Saved = {\n  image: Blob | string | null;\n  value: CropValue | null;\n};\n\nconst PROBLEMS = {\n  open: \"This image did not open. Try a JPEG, PNG or WebP.\",\n  save: \"This cover did not save. Try again.\",\n} as const;\n\nconst CoverCropper = ({\n  aspect = 3,\n  className,\n  defaultImage = null,\n  onCrop,\n  width = 1500,\n}: CoverCropperProps) => {\n  const [image, setImage] = useState<Blob | string | null>(defaultImage);\n  const [mode, setMode] = useState<Mode>(\"loading\");\n  const [problem, setProblem] = useState<Problem>(null);\n  const [picks, setPicks] = useState(0);\n  const [saving, setSaving] = useState(false);\n  const actions = useRef<CropperApi>(null);\n  const before = useRef<CropValue | null>(null);\n  const [saved, setSaved] = useState<Saved>({\n    image: defaultImage,\n    value: null,\n  });\n  const picked = useRef(false);\n  const root = useRef<HTMLDivElement>(null);\n  const input = useRef<HTMLInputElement>(null);\n  const editing = mode === \"edit\";\n\n  const refocus = (): void => {\n    requestAnimationFrame(() => {\n      root.current?.querySelector<HTMLElement>(\"[data-primary]\")?.focus();\n    });\n  };\n\n  const show = (next: Mode): void => {\n    setMode(next);\n    refocus();\n  };\n\n  const restore = (): void => {\n    picked.current = false;\n    setMode(\"loading\");\n    setImage(saved.image);\n    setPicks((count) => count + 1);\n  };\n\n  const edit = (): void => {\n    before.current = actions.current?.getValue() ?? null;\n    setProblem(null);\n    show(\"edit\");\n  };\n\n  const cancel = (): void => {\n    setProblem(null);\n\n    if (image !== saved.image) {\n      restore();\n      refocus();\n\n      return;\n    }\n\n    if (before.current) {\n      actions.current?.setValue(before.current);\n    }\n\n    show(\"view\");\n  };\n\n  const save = async (): Promise<void> => {\n    const cropper = actions.current;\n\n    if (!cropper) {\n      return;\n    }\n\n    setSaving(true);\n    const file = await cropToFile(cropper, {\n      maxSize: { height: Math.round(width / aspect), width },\n      name: \"cover\",\n      quality: 0.9,\n      type: \"image/jpeg\",\n    }).catch(() => null);\n\n    setSaving(false);\n\n    if (file === null) {\n      setProblem(\"save\");\n\n      return;\n    }\n\n    setSaved({ image, value: cropper.getValue() });\n    setProblem(null);\n    show(\"view\");\n    onCrop?.(file);\n  };\n\n  const value = image === saved.image ? saved.value : null;\n\n  return (\n    <div\n      className={cn(\n        \"relative w-full overflow-hidden rounded-xl bg-muted ring-1 ring-foreground/10\",\n        className\n      )}\n      data-slot=\"cover-cropper\"\n      ref={root}\n      style={{ aspectRatio: aspect }}\n    >\n      {image === null ? null : (\n        <Cropper\n          actionsRef={actions}\n          aspect={aspect}\n          className=\"absolute inset-0 gap-0 [--zriz-frame:transparent]\"\n          disabled={!editing}\n          key={picks}\n          maxZoom={4}\n          onError={() => {\n            setProblem(\"open\");\n            restore();\n          }}\n          onLoad={() => {\n            setMode(picked.current ? \"edit\" : \"view\");\n            picked.current = false;\n          }}\n          src={image}\n          svg={false}\n          {...(value === null ? null : { defaultValue: value })}\n        >\n          <CropperViewport\n            className=\"aspect-auto size-full rounded-none p-0\"\n            grid={false}\n            handles={false}\n            label=\"Cover position\"\n            wheel=\"zoom\"\n          />\n          {editing ? (\n            <div className=\"absolute bottom-3 left-3 flex h-8 w-36 items-center rounded-lg bg-background px-2 shadow-sm\">\n              <CropperZoom />\n            </div>\n          ) : null}\n        </Cropper>\n      )}\n      {editing ? null : <div className=\"absolute inset-0\" />}\n      {image === null && problem === null ? (\n        <p className=\"absolute inset-0 flex items-center justify-center px-4 text-center text-sm text-muted-foreground\">\n          No cover yet\n        </p>\n      ) : null}\n      {problem === null ? null : (\n        <p\n          className=\"absolute top-3 left-1/2 max-w-[calc(100%-1.5rem)] -translate-x-1/2 rounded-md bg-background px-2 py-1 text-xs font-medium text-destructive shadow-sm\"\n          role=\"alert\"\n        >\n          {PROBLEMS[problem]}\n        </p>\n      )}\n      {editing && problem === null ? (\n        <p className=\"pointer-events-none absolute top-3 left-1/2 -translate-x-1/2 rounded-md bg-background px-2 py-1 text-xs font-medium whitespace-nowrap shadow-sm\">\n          Drag to reposition\n        </p>\n      ) : null}\n      <div className=\"absolute right-3 bottom-3 flex gap-2\">\n        {editing ? (\n          <>\n            <Button\n              className=\"shadow-sm\"\n              disabled={saving}\n              key=\"cancel\"\n              onClick={cancel}\n              size=\"sm\"\n              type=\"button\"\n              variant=\"secondary\"\n            >\n              Cancel\n            </Button>\n            <Button\n              className=\"shadow-sm\"\n              data-primary=\"\"\n              disabled={saving}\n              key=\"save\"\n              onClick={() => {\n                void save();\n              }}\n              size=\"sm\"\n              type=\"button\"\n            >\n              Save\n            </Button>\n          </>\n        ) : (\n          <>\n            <Button\n              className=\"shadow-sm\"\n              key=\"change\"\n              onClick={() => {\n                input.current?.click();\n              }}\n              size=\"sm\"\n              type=\"button\"\n              variant=\"secondary\"\n            >\n              {image === null ? \"Add cover\" : \"Change\"}\n            </Button>\n            {mode === \"view\" && image !== null ? (\n              <Button\n                className=\"shadow-sm\"\n                data-primary=\"\"\n                key=\"reposition\"\n                onClick={edit}\n                size=\"sm\"\n                type=\"button\"\n                variant=\"secondary\"\n              >\n                Reposition\n              </Button>\n            ) : null}\n          </>\n        )}\n      </div>\n      <input\n        accept=\"image/jpeg,image/png,image/webp\"\n        aria-label=\"Choose a cover\"\n        className=\"sr-only\"\n        onChange={(event) => {\n          const file = event.currentTarget.files?.[0];\n\n          event.currentTarget.value = \"\";\n\n          if (file) {\n            before.current = null;\n            picked.current = true;\n            setProblem(null);\n            setMode(\"loading\");\n            setImage(file);\n            setPicks((count) => count + 1);\n          }\n        }}\n        ref={input}\n        tabIndex={-1}\n        type=\"file\"\n      />\n    </div>\n  );\n};\n\nexport type { CoverCropperProps };\nexport { CoverCropper };\n",
      "type": "registry:component"
    }
  ],
  "docs": "Needs Tailwind CSS v4, React 19 and shadcn/ui components on Radix or Base UI. Docs: https://zriz.dev/docs/shadcn",
  "categories": [
    "image",
    "cropper"
  ],
  "type": "registry:block"
}