{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "export-menu",
  "type": "registry:component",
  "title": "Export menu",
  "description": "Export a TanStack table to Excel, PDF or CSV the way it looks on screen: groups, filters, sort, hidden and pinned columns.",
  "dependencies": [
    "rowpine",
    "lucide-react"
  ],
  "registryDependencies": [
    "button",
    "dropdown-menu"
  ],
  "files": [
    {
      "path": "registry/rowpine/export-menu.tsx",
      "type": "registry:component",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { ChevronDown, Download, FileSpreadsheet, FileText, Sheet } from \"lucide-react\"\nimport {\n  exportCsv,\n  exportPdf,\n  exportThemeFromPage,\n  exportXlsx,\n  type CellStyle,\n  type PdfTableExportOptions,\n  type RowScope,\n  type TableLike,\n  type XlsxExportOptions,\n} from \"rowpine\"\n\nimport { cn } from \"@/lib/utils\"\nimport { buttonVariants } from \"@/components/ui/button\"\nimport {\n  DropdownMenu,\n  DropdownMenuCheckboxItem,\n  DropdownMenuContent,\n  DropdownMenuGroup,\n  DropdownMenuItem,\n  DropdownMenuLabel,\n  DropdownMenuSeparator,\n  DropdownMenuTrigger,\n} from \"@/components/ui/dropdown-menu\"\n\ntype Format = \"csv\" | \"xlsx\" | \"pdf\"\n\n/** Props of `ExportMenu`. */\nexport type ExportMenuProps = {\n  /** A TanStack table (v8 or v9), or `pivot.table` from createPivot. */\n  table: TableLike\n  /** File name without extension. */\n  fileName?: string\n  /** Title printed at the top of the PDF. */\n  pdfTitle?: string\n  /** Which formats to offer. Default all three. */\n  formats?: Format[]\n  /** Style whole rows, e.g. `pivotRowStyle` for pivots. */\n  rowStyle?: (original: unknown) => CellStyle | undefined\n  /** Offer \"Selected rows only\". Default: when the table has a selection. */\n  allowSelectedOnly?: boolean\n  /** Called when an export fails. Default: console.error. */\n  onError?: (error: unknown) => void\n  /** Button text. Default 'Export'. */\n  label?: string\n  /** Classes added to the trigger button. */\n  className?: string\n  /**\n   * Colors for the Excel and PDF files. \"page\" (default) reads your shadcn/ui\n   * theme, so the files look like the table; \"none\" keeps rowpine's defaults.\n   */\n  theme?: \"page\" | \"none\"\n  /** More PDF options, such as `pageSize` or `kpis`. They win over the theme colors. */\n  pdfOptions?: PdfTableExportOptions\n  /** More Excel options, such as `sheetName`. They win over the theme colors. */\n  xlsxOptions?: XlsxExportOptions\n}\n\nconst ITEMS: Record<Format, { label: string; icon: React.ComponentType<{ className?: string }> }> = {\n  xlsx: { label: \"Excel (.xlsx)\", icon: FileSpreadsheet },\n  pdf: { label: \"PDF\", icon: FileText },\n  csv: { label: \"CSV\", icon: Sheet },\n}\n\n/**\n * A dropdown button that exports the table to Excel, PDF or CSV and saves the\n * file. While an export runs the button reads \"Exporting...\" and further picks\n * are ignored. A \"Selected rows only\" checkbox appears when the table has\n * selected rows, or as `allowSelectedOnly` says.\n * @param props See `ExportMenuProps`.\n */\nexport function ExportMenu({\n  table,\n  fileName = \"export\",\n  pdfTitle,\n  formats = [\"xlsx\", \"pdf\", \"csv\"],\n  rowStyle,\n  allowSelectedOnly,\n  onError = (e) => console.error(e),\n  label = \"Export\",\n  className,\n  theme = \"page\",\n  pdfOptions,\n  xlsxOptions,\n}: ExportMenuProps) {\n  const [busy, setBusy] = React.useState<Format | null>(null)\n  const [selectedOnly, setSelectedOnly] = React.useState(false)\n  const selectedCount = table.getSelectedRowModel?.().rows.length ?? 0\n  const showSelected = allowSelectedOnly ?? selectedCount > 0\n  const rows: RowScope = showSelected && selectedOnly && selectedCount > 0 ? \"selected\" : \"all\"\n\n  async function run(format: Format) {\n    if (busy) return\n    setBusy(format)\n    try {\n      const colors = theme === \"page\" ? exportThemeFromPage() : { pdf: {}, xlsx: {} }\n      if (format === \"xlsx\") await exportXlsx(table, { ...colors.xlsx, fileName, rows, rowStyle, ...xlsxOptions })\n      else if (format === \"pdf\") await exportPdf(table, { ...colors.pdf, fileName, rows, rowStyle, title: pdfTitle, ...pdfOptions })\n      else await exportCsv(table, { fileName, rows })\n    } catch (error) {\n      onError(error)\n    } finally {\n      setBusy(null)\n    }\n  }\n\n  return (\n    <DropdownMenu>\n      {/* No asChild: the trigger is the button, which works with both the Radix and the Base UI menus. */}\n      <DropdownMenuTrigger\n        className={cn(buttonVariants({ variant: \"outline\", size: \"sm\" }), className)}\n        // Not disabled while busy: the menu returns focus to its trigger on close, and a disabled one cannot take it.\n        aria-busy={busy !== null}\n      >\n        <Download />\n        {busy ? \"Exporting...\" : label}\n        <ChevronDown className=\"opacity-60\" />\n      </DropdownMenuTrigger>\n      <DropdownMenuContent align=\"end\" className=\"w-52\">\n        <DropdownMenuGroup>\n          <DropdownMenuLabel>Export as</DropdownMenuLabel>\n          {formats.map((f) => {\n            const { label, icon: Icon } = ITEMS[f]\n            return (\n              <DropdownMenuItem key={f} onClick={() => void run(f)}>\n                <Icon />\n                {label}\n              </DropdownMenuItem>\n            )\n          })}\n        </DropdownMenuGroup>\n        {showSelected && (\n          <>\n            <DropdownMenuSeparator />\n            <DropdownMenuCheckboxItem checked={selectedOnly} onCheckedChange={(v) => setSelectedOnly(v === true)}>\n              Selected rows only ({selectedCount})\n            </DropdownMenuCheckboxItem>\n          </>\n        )}\n      </DropdownMenuContent>\n    </DropdownMenu>\n  )\n}\n"
    }
  ]
}
