Skip to content

armgit / pdfcn

Public Imported from GitHub
0

Beautiful pdf components, built on Takumi and Forme. 100% Free, Zero config, one command setup.

feat: rewrite home-pdf-showcase with real PdfPreview rendering and metadata extraction

Aniket-5080c6786f6101f
diff --git a/components/home-pdf-preview.tsx b/components/home-pdf-preview.tsx
index d56e8b7..c105583 100644
--- a/components/home-pdf-preview.tsx
+++ b/components/home-pdf-preview.tsx
@@ -1,11 +1,5 @@
"use client";
-import { BoxIcon } from "lucide-react";
-
-import { LogoMark } from "@/components/logo";
-import { Badge } from "@/components/ui/badge";
-import { cn } from "@/lib/utils";
-
export const homePdfComponentCatalog = {
alert: {
codeName: "Alert",
@@ -115,7 +109,7 @@ export const homePdfComponentCatalog = {
} as const;
export type ComponentPartId = keyof typeof homePdfComponentCatalog;
-export type CodeOutput = "react" | "html";
+export type CodeOutput = "react" | "metadata";
export type HomePdfBase = "takumi" | "forme";
export const homePdfBases: {
@@ -128,7 +122,7 @@ export const homePdfBases: {
export const homePdfCodeOutputs: { id: CodeOutput; label: string }[] = [
{ id: "react", label: "React" },
- { id: "html", label: "HTML" },
+ { id: "metadata", label: "Metadata" },
];
export interface PdfRecipe {
@@ -242,419 +236,10 @@ ${imports}
export function ${functionName}Document() {
return (
<${shell.open}>
- <${shell.open === "Document" ? "Page" : "Page"}>
+ <Page>
${components}
- </${shell.open === "Document" ? "Page" : "Page"}>
+ </Page>
</${shell.close}>
);
}`;
};
-
-const SelectablePdfPart = ({
- children,
- id,
- selectedId,
- onSelect,
- className,
-}: {
- children: React.ReactNode;
- id: ComponentPartId;
- selectedId: ComponentPartId;
- onSelect: (id: ComponentPartId) => void;
- className?: string;
-}) => {
- const isSelected = selectedId === id;
-
- return (
- <button
- type="button"
- aria-label={`Inspect ${homePdfComponentCatalog[id].label}`}
- aria-pressed={isSelected}
- className={cn(
- "group/pdf-part relative block w-full cursor-pointer text-left outline-none transition-[box-shadow] duration-200",
- "hover:z-10 hover:shadow-[inset_0_0_0_2px_rgb(96_165_250)]",
- "focus-visible:z-10 focus-visible:shadow-[inset_0_0_0_3px_rgb(59_130_246)]",
- isSelected && "z-10 shadow-[inset_0_0_0_2px_rgb(59_130_246)]",
- className
- )}
- onClick={() => onSelect(id)}
- >
- {children}
- <Badge
- className={cn(
- "pointer-events-none absolute top-0 right-3 z-20 -translate-y-1/2 bg-blue-600 py-1 text-[9px] leading-none font-semibold tracking-wide text-white uppercase shadow-sm transition-opacity",
- isSelected
- ? "opacity-100"
- : "opacity-0 group-hover/pdf-part:opacity-100 group-focus-visible/pdf-part:opacity-100"
- )}
- >
- <BoxIcon className="size-2.5" aria-hidden="true" />
- {id}
- </Badge>
- </button>
- );
-};
-
-const CorporateInvoicePreview = ({
- selectedId,
- onSelect,
-}: PdfPreviewContentProps) => (
- <div className="w-full bg-white font-sans text-foreground">
- <SelectablePdfPart
- id="page-header"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-background px-6 py-5 sm:px-8"
- >
- <div className="flex items-center justify-between">
- <div className="flex items-center gap-2 text-sm font-semibold">
- <span className="flex size-7 items-center justify-center rounded-lg bg-primary text-primary-foreground">
- <LogoMark className="size-4" />
- </span>
- PDFx Inc.
- </div>
- <div className="text-right">
- <p className="text-[9px] font-bold tracking-[0.18em] text-muted-foreground uppercase">
- Invoice
- </p>
- <p className="mt-0.5 text-[10px] font-medium">INV-2026-004</p>
- </div>
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="key-value"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-background px-6 py-5 sm:px-8"
- >
- <div className="grid grid-cols-2 gap-4 text-[10px]">
- <div>
- <p className="font-semibold text-muted-foreground uppercase">
- Bill To
- </p>
- <p className="mt-1 font-medium">Global Industries Ltd.</p>
- <p className="text-muted-foreground">[email protected]</p>
- <p className="text-muted-foreground">+1 (555) 888-9999</p>
- </div>
- <div>
- <p className="font-semibold text-muted-foreground uppercase">
- Details
- </p>
- <p className="mt-1 text-muted-foreground">Date: Feb 22, 2026</p>
- <p className="text-muted-foreground">Due: Mar 24, 2026</p>
- <p className="text-muted-foreground">Net 30 terms</p>
- </div>
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="table"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-background px-6 py-4 sm:px-8"
- >
- <div className="rounded-lg border">
- <table className="w-full text-[10px]">
- <thead>
- <tr className="border-b bg-muted/50">
- <th className="px-3 py-2 text-left font-semibold">Description</th>
- <th className="px-3 py-2 text-center font-semibold">Qty</th>
- <th className="px-3 py-2 text-right font-semibold">Price</th>
- <th className="px-3 py-2 text-right font-semibold">Total</th>
- </tr>
- </thead>
- <tbody className="divide-y">
- {[
- ["Enterprise Software License", "5", "$4,500", "$22,500"],
- ["Implementation Services", "1", "$18,000", "$18,000"],
- ["Training Workshop", "3", "$2,500", "$7,500"],
- ["Annual Support Package", "1", "$8,500", "$8,500"],
- ].map(([desc, qty, price, total]) => (
- <tr key={desc} className="divide-x">
- <td className="px-3 py-2 font-medium">{desc}</td>
- <td className="px-3 py-2 text-center">{qty}</td>
- <td className="px-3 py-2 text-right text-muted-foreground">
- {price}
- </td>
- <td className="px-3 py-2 text-right font-medium">{total}</td>
- </tr>
- ))}
- </tbody>
- </table>
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="section"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-muted/30 px-6 py-4 sm:px-8"
- >
- <div className="ml-auto w-48 text-[10px]">
- <div className="flex justify-between py-1">
- <span className="text-muted-foreground">Subtotal</span>
- <span>$56,500</span>
- </div>
- <div className="flex justify-between py-1">
- <span className="text-muted-foreground">Tax (8%)</span>
- <span>$4,520</span>
- </div>
- <div className="flex justify-between border-t py-1.5 font-semibold">
- <span>Total</span>
- <span>$61,020</span>
- </div>
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="text"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-background px-6 py-4 sm:px-8"
- >
- <p className="text-[9px] text-muted-foreground">
- Corporate billing — Net 30 terms apply. For inquiries, contact
- </p>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="page-footer"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-primary px-6 py-4 text-primary-foreground/60 sm:px-8"
- >
- <div className="flex items-center justify-between text-[9px]">
- <div className="flex items-center gap-1.5">
- <LogoMark className="size-3" />
- pdfcn
- </div>
- <span>Page 1 of 1</span>
- </div>
- </SelectablePdfPart>
- </div>
-);
-
-const FinancialReportPreview = ({
- selectedId,
- onSelect,
-}: PdfPreviewContentProps) => (
- <div className="w-full bg-white font-sans text-foreground">
- <SelectablePdfPart
- id="page-header"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-background px-6 py-5 sm:px-8"
- >
- <div className="flex items-center justify-between">
- <div className="flex items-center gap-2 text-sm font-semibold">
- <span className="flex size-7 items-center justify-center rounded-lg bg-emerald-600 text-white">
- <LogoMark className="size-4" />
- </span>
- PDFx Inc.
- </div>
- <span className="rounded-full bg-emerald-50 px-2.5 py-0.5 text-[9px] font-semibold text-emerald-700">
- Q4 2025
- </span>
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="text"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-background px-6 py-6 text-center sm:px-8"
- >
- <p className="text-[9px] font-bold tracking-[0.22em] text-emerald-600 uppercase">
- Financial Overview
- </p>
- <h3 className="mt-2 text-2xl font-semibold tracking-tight">
- Quarterly Revenue Report
- </h3>
- <p className="mx-auto mt-2 max-w-sm text-[11px] text-muted-foreground">
- Summary of financial performance for Q4 2025 across all business units.
- </p>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="graph"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-muted/30 px-6 py-5 sm:px-8"
- >
- <div className="flex items-end justify-center gap-2 h-24">
- {[65, 45, 80, 55, 70, 90].map((h, i) => (
- <div key={i} className="flex flex-col items-center gap-1">
- <div
- className="w-8 rounded-t bg-emerald-500/80"
- style={{ height: `${h}%` }}
- />
- <span className="text-[8px] text-muted-foreground">
- {["Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][i]}
- </span>
- </div>
- ))}
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="data-table"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-background px-6 py-4 sm:px-8"
- >
- <div className="rounded-lg border">
- <table className="w-full text-[10px]">
- <thead>
- <tr className="border-b bg-muted/50">
- <th className="px-3 py-2 text-left font-semibold">Segment</th>
- <th className="px-3 py-2 text-right font-semibold">Revenue</th>
- <th className="px-3 py-2 text-right font-semibold">Growth</th>
- </tr>
- </thead>
- <tbody className="divide-y">
- {[
- ["Enterprise", "$2.4M", "+18%"],
- ["SMB", "$1.1M", "+24%"],
- ["Consumer", "$890K", "+12%"],
- ].map(([segment, revenue, growth]) => (
- <tr key={segment} className="divide-x">
- <td className="px-3 py-2 font-medium">{segment}</td>
- <td className="px-3 py-2 text-right">{revenue}</td>
- <td className="px-3 py-2 text-right font-medium text-emerald-600">
- {growth}
- </td>
- </tr>
- ))}
- </tbody>
- </table>
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="page-footer"
- selectedId={selectedId}
- onSelect={onSelect}
- className="bg-emerald-600 px-6 py-4 text-emerald-100/60 sm:px-8"
- >
- <div className="flex items-center justify-between text-[9px]">
- <div className="flex items-center gap-1.5">
- <LogoMark className="size-3" />
- pdfcn
- </div>
- <span>Page 1 of 1</span>
- </div>
- </SelectablePdfPart>
- </div>
-);
-
-const MinimalInvoicePreview = ({
- selectedId,
- onSelect,
-}: PdfPreviewContentProps) => (
- <div className="w-full bg-white font-sans text-foreground">
- <SelectablePdfPart
- id="page-header"
- selectedId={selectedId}
- onSelect={onSelect}
- className="px-6 py-6 sm:px-10"
- >
- <div className="flex items-center justify-between">
- <div className="flex items-center gap-2 text-sm font-semibold">
- <span className="flex size-7 items-center justify-center rounded-lg bg-foreground text-background">
- <LogoMark className="size-4" />
- </span>
- pdfcn
- </div>
- <div className="text-right">
- <p className="text-[9px] font-bold tracking-[0.18em] text-muted-foreground uppercase">
- Invoice
- </p>
- <p className="mt-0.5 text-[10px] font-medium">INV-2026-042</p>
- </div>
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="text"
- selectedId={selectedId}
- onSelect={onSelect}
- className="px-6 py-4 sm:px-10"
- >
- <p className="text-lg font-semibold">Invoice for Design Services</p>
- <p className="mt-1 text-[11px] text-muted-foreground">
- Professional design consultation and brand identity package.
- </p>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="divider"
- selectedId={selectedId}
- onSelect={onSelect}
- className="px-6 sm:px-10"
- >
- <div className="h-px bg-border" />
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="key-value"
- selectedId={selectedId}
- onSelect={onSelect}
- className="px-6 py-4 sm:px-10"
- >
- <div className="grid grid-cols-2 gap-6 text-[10px]">
- <div>
- <p className="font-semibold text-muted-foreground uppercase">From</p>
- <p className="mt-1 font-medium">PDFx Inc.</p>
- <p className="text-muted-foreground">[email protected]</p>
- </div>
- <div>
- <p className="font-semibold text-muted-foreground uppercase">To</p>
- <p className="mt-1 font-medium">Acme Corp</p>
- <p className="text-muted-foreground">[email protected]</p>
- </div>
- </div>
- </SelectablePdfPart>
-
- <SelectablePdfPart
- id="page-footer"
- selectedId={selectedId}
- onSelect={onSelect}
- className="px-6 py-4 sm:px-10"
- >
- <div className="flex items-center justify-between text-[9px] text-muted-foreground">
- <div className="flex items-center gap-1.5">
- <LogoMark className="size-3" />
- pdfcn
- </div>
- <span>Page 1 of 1</span>
- </div>
- </SelectablePdfPart>
- </div>
-);
-
-interface PdfPreviewContentProps {
- selectedId: ComponentPartId;
- onSelect: (id: ComponentPartId) => void;
-}
-
-export const HomePdfPreview = ({
- pdfId,
- selectedId,
- onSelect,
-}: PdfPreviewContentProps & { pdfId: PdfRecipeId }) => {
- if (pdfId === "financial-report") {
- return (
- <FinancialReportPreview selectedId={selectedId} onSelect={onSelect} />
- );
- }
- if (pdfId === "minimal-invoice") {
- return (
- <MinimalInvoicePreview selectedId={selectedId} onSelect={onSelect} />
- );
- }
- return (
- <CorporateInvoicePreview selectedId={selectedId} onSelect={onSelect} />
- );
-};
diff --git a/components/home-pdf-showcase.tsx b/components/home-pdf-showcase.tsx
index f20f4bb..3f3007b 100644
--- a/components/home-pdf-showcase.tsx
+++ b/components/home-pdf-showcase.tsx
@@ -8,13 +8,13 @@ import {
MonitorIcon,
SmartphoneIcon,
} from "lucide-react";
+import dynamic from "next/dynamic";
import Link from "next/link";
-import { useEffect, useState } from "react";
+import { createElement, useEffect, useState } from "react";
import { CodeBlockCommand } from "@/components/code-block-command";
import { CopyButton } from "@/components/copy-button";
import {
- HomePdfPreview,
getHomePdfSource,
homePdfBases,
homePdfCodeOutputs,
@@ -40,6 +40,15 @@ import { Separator } from "@/components/ui/separator";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Toggle } from "@/components/ui/toggle";
import { cn } from "@/lib/utils";
+import type { BaseName } from "@/registry/bases";
+
+const PdfPreview = dynamic(
+ async () => {
+ const mod = await import("@/components/pdf-preview");
+ return mod.PdfPreview;
+ },
+ { ssr: false }
+);
type PreviewViewport = "desktop" | "mobile";
type WorkspaceTab = "preview" | "code";
@@ -49,7 +58,19 @@ interface HomePdfCodeOutput {
highlightedCode: string;
}
+interface PdfMetadata {
+ title?: string;
+ author?: string;
+ subject?: string;
+ creator?: string;
+ producer?: string;
+ creationDate?: string;
+ modDate?: string;
+ keywords?: string;
+}
+
const codeCache = new Map<string, HomePdfCodeOutput>();
+const metadataCache = new Map<string, PdfMetadata>();
const homePdfBaseIcons: Record<HomePdfBase, typeof TakumiIcon> = {
forme: FormeIcon,
takumi: TakumiIcon,
@@ -66,12 +87,50 @@ const PdfBaseIcon = ({
return <Icon className={className} aria-hidden="true" />;
};
+const MetadataViewer = ({ metadata }: { metadata: PdfMetadata | null }) => {
+ if (!metadata) {
+ return (
+ <div className="flex h-full min-h-72 items-center justify-center text-sm text-muted-foreground">
+ No metadata available
+ </div>
+ );
+ }
+
+ const entries = Object.entries(metadata).filter(
+ ([, value]) => value !== undefined && value !== ""
+ );
+
+ if (entries.length === 0) {
+ return (
+ <div className="flex h-full min-h-72 items-center justify-center text-sm text-muted-foreground">
+ No metadata found in this document
+ </div>
+ );
+ }
+
+ return (
+ <div className="h-full overflow-auto p-4">
+ <div className="mx-auto max-w-lg space-y-3">
+ {entries.map(([key, value]) => (
+ <div key={key} className="rounded-lg border bg-card p-3">
+ <dt className="text-[10px] font-semibold tracking-wide text-muted-foreground uppercase">
+ {key.replaceAll(/([A-Z])/g, " $1").trim()}
+ </dt>
+ <dd className="mt-1 text-sm">{value}</dd>
+ </div>
+ ))}
+ </div>
+ </div>
+ );
+};
+
const CodeViewer = ({
availableOutputs,
error,
isLoading,
output,
selectedOutput,
+ metadata,
onOutputChange,
}: {
availableOutputs: { id: CodeOutput; label: string }[];
@@ -79,6 +138,7 @@ const CodeViewer = ({
isLoading: boolean;
output: CodeOutput;
selectedOutput: HomePdfCodeOutput | null;
+ metadata: PdfMetadata | null;
onOutputChange: (output: CodeOutput) => void;
}) => (
<div className="flex h-full min-h-[560px] flex-col bg-code text-code-foreground lg:min-h-0">
@@ -101,52 +161,127 @@ const CodeViewer = ({
))}
</TabsList>
</Tabs>
- <CopyButton
- className="static ml-auto bg-transparent"
- value={selectedOutput?.code ?? ""}
- event="copy_block_code"
- />
+ {output === "react" ? (
+ <CopyButton
+ className="static ml-auto bg-transparent"
+ value={selectedOutput?.code ?? ""}
+ event="copy_block_code"
+ />
+ ) : null}
</div>
<Separator className="bg-border/50" />
- <div className="no-scrollbar min-h-0 flex-1 overflow-auto">
- {isLoading ? (
- <div
- className="flex h-full min-h-72 items-center justify-center text-sm text-code-foreground/60"
- role="status"
- >
- Rendering document…
- </div>
- ) : null}
- {error ? (
- <div
- className="flex h-full min-h-72 items-center justify-center px-6 text-center text-sm text-red-400"
- role="alert"
- >
- {error}
- </div>
- ) : null}
- {selectedOutput && !isLoading ? (
- <figure
- data-rehype-pretty-code-figure=""
- className="!m-0 min-h-full !rounded-none text-sm [&>div]:min-h-full [&_pre]:min-h-full"
- >
+ {output === "metadata" ? (
+ <MetadataViewer metadata={metadata} />
+ ) : (
+ <div className="no-scrollbar min-h-0 flex-1 overflow-auto">
+ {isLoading ? (
<div
- // Shiki returns trusted HTML generated from the provided source code.
- // eslint-disable-next-line react/no-danger
- dangerouslySetInnerHTML={{
- __html: selectedOutput.highlightedCode,
- }}
- />
- </figure>
- ) : null}
- </div>
+ className="flex h-full min-h-72 items-center justify-center text-sm text-code-foreground/60"
+ role="status"
+ >
+ Rendering source…
+ </div>
+ ) : null}
+ {error ? (
+ <div
+ className="flex h-full min-h-72 items-center justify-center px-6 text-center text-sm text-red-400"
+ role="alert"
+ >
+ {error}
+ </div>
+ ) : null}
+ {selectedOutput && !isLoading ? (
+ <figure
+ data-rehype-pretty-code-figure=""
+ className="!m-0 min-h-full !rounded-none text-sm [&>div]:min-h-full [&_pre]:min-h-full"
+ >
+ <div
+ // Shiki returns trusted HTML generated from the provided source code.
+ // eslint-disable-next-line react/no-danger
+ dangerouslySetInnerHTML={{
+ __html: selectedOutput.highlightedCode,
+ }}
+ />
+ </figure>
+ ) : null}
+ </div>
+ )}
</div>
);
const findPdf = (id: PdfRecipeId): PdfRecipe =>
homePdfPreviews.find((pdf) => pdf.id === id) ?? homePdfPreviews[0];
+const extractMetadata = async (base: BaseName, name: string) => {
+ const cacheKey = `${base}:${name}`;
+ const cached = metadataCache.get(cacheKey);
+ if (cached) {
+ return cached;
+ }
+
+ let pdfBytes: Uint8Array;
+
+ if (base === "takumi") {
+ const [{ demos }, { default: initialize, render }, { fromJsx }] =
+ await Promise.all([
+ import("@/examples/__takumi__"),
+ import("takumi-pdf"),
+ import("@takumi-rs/helpers/jsx"),
+ ]);
+ const Demo = demos[name];
+ if (!Demo) {
+ throw new Error(`Unknown Takumi demo: ${name}`);
+ }
+
+ await initialize({ module_or_path: "/takumi_pdf_wasm_bg.wasm" });
+ const { node, stylesheets } = await fromJsx(createElement(Demo));
+ const buffer = await render(node, {
+ margin: { bottom: 0, left: 0, right: 0, top: 0 },
+ size: "a4",
+ stylesheets,
+ });
+ pdfBytes = new Uint8Array(buffer);
+ } else {
+ const [{ demos }, { renderSerializedDoc }, { serialize }] =
+ await Promise.all([
+ import("@/examples/__forme__"),
+ import("@formepdf/core/browser"),
+ import("@formepdf/react"),
+ ]);
+ const Demo = demos[name];
+ if (!Demo) {
+ throw new Error(`Unknown Forme demo: ${name}`);
+ }
+
+ const document = serialize(createElement(Demo));
+ const buffer = await renderSerializedDoc(
+ document as unknown as Record<string, unknown>
+ );
+ pdfBytes = new Uint8Array(buffer);
+ }
+
+ const { getDocument } = await import("pdfjs-dist");
+ const loadingTask = getDocument({ data: pdfBytes });
+ const pdfDoc = await loadingTask.promise;
+ const metadataResult = await pdfDoc.getMetadata();
+
+ const info = metadataResult.info as Record<string, string>;
+ const metadata: PdfMetadata = {
+ author: info.Author || undefined,
+ creationDate: info.CreationDate || undefined,
+ creator: info.Creator || undefined,
+ keywords: info.Keywords || undefined,
+ modDate: info.ModDate || undefined,
+ producer: info.Producer || undefined,
+ subject: info.Subject || undefined,
+ title: info.Title || undefined,
+ };
+
+ metadataCache.set(cacheKey, metadata);
+ return metadata;
+};
+
export const HomePdfShowcase = () => {
const [selectedPdfId, setSelectedPdfId] =
useState<PdfRecipeId>("corporate-invoice");
@@ -167,17 +302,18 @@ export const HomePdfShowcase = () => {
isLoading: false,
key: "",
});
+ const [pdfMetadata, setPdfMetadata] = useState<PdfMetadata | null>(null);
+ const [_metadataLoading, setMetadataLoading] = useState(false);
const selectedPdf = findPdf(selectedPdfId);
const selectedComponent = homePdfComponentCatalog[selectedComponentId];
const registryItem = `@pdfcn/${pdfBase}/${selectedComponentId}`;
- const codeRequestKey = `${pdfBase}:${selectedPdf.id}:${codeOutput}`;
+ const codeRequestKey = `${pdfBase}:${selectedPdf.id}:react`;
const selectedCodeOutput =
codeRequest.key === codeRequestKey ? codeRequest.data : null;
- const availableCodeOutputs = homePdfCodeOutputs;
useEffect(() => {
- if (workspaceTab !== "code") {
+ if (workspaceTab !== "code" || codeOutput !== "react") {
return;
}
@@ -202,28 +338,11 @@ export const HomePdfShowcase = () => {
});
try {
- let code: string | null;
- let language: string;
-
- if (codeOutput === "react") {
- code = getHomePdfSource(selectedPdf, pdfBase);
- language = "tsx";
- } else {
- throw new Error(
- "HTML output is not available for the selected base."
- );
- }
-
- if (!code) {
- throw new Error(
- "This format is not available for the selected base."
- );
- }
-
+ const code = getHomePdfSource(selectedPdf, pdfBase);
const { highlightCode } = await import("@/lib/highlight-code");
const output = {
code,
- highlightedCode: await highlightCode(code, language),
+ highlightedCode: await highlightCode(code, "tsx"),
};
codeCache.set(codeRequestKey, output);
@@ -246,7 +365,7 @@ export const HomePdfShowcase = () => {
error:
error instanceof Error
? error.message
- : "Could not render this document.",
+ : "Could not render this source.",
isLoading: false,
key: codeRequestKey,
});
@@ -259,14 +378,53 @@ export const HomePdfShowcase = () => {
};
}, [codeOutput, codeRequestKey, pdfBase, selectedPdf, workspaceTab]);
+ useEffect(() => {
+ if (workspaceTab !== "code" || codeOutput !== "metadata") {
+ return;
+ }
+
+ const metadataKey = `${pdfBase}:${selectedPdf.id}`;
+ const cached = metadataCache.get(metadataKey);
+ if (cached) {
+ setPdfMetadata(cached);
+ return;
+ }
+
+ let cancelled = false;
+ const loadMetadata = async () => {
+ setMetadataLoading(true);
+ try {
+ const metadata = await extractMetadata(pdfBase, selectedPdf.id);
+ if (!cancelled) {
+ setPdfMetadata(metadata);
+ }
+ } catch {
+ if (!cancelled) {
+ setPdfMetadata(null);
+ }
+ } finally {
+ if (!cancelled) {
+ setMetadataLoading(false);
+ }
+ }
+ };
+
+ void loadMetadata();
+ return () => {
+ cancelled = true;
+ };
+ }, [codeOutput, pdfBase, selectedPdf, workspaceTab]);
+
const handlePdfBaseChange = (base: HomePdfBase) => {
setPdfBase(base);
+ setPdfMetadata(null);
};
const handlePdfChange = (pdfId: PdfRecipeId) => {
const nextPdf = findPdf(pdfId);
setSelectedPdfId(pdfId);
setSelectedComponentId(nextPdf.defaultComponentId);
+ setPdfMetadata(null);
};
const handleComponentChange = (componentId: ComponentPartId) => {
@@ -470,16 +628,16 @@ export const HomePdfShowcase = () => {
<div className="h-[520px] overflow-auto p-2 sm:h-[620px] sm:p-4 lg:h-[672px] lg:p-5">
<div
className={cn(
- "mx-auto w-full max-w-[375px] bg-background shadow-xl transition-[width] duration-200",
+ "mx-auto w-full max-w-[375px] transition-[width] duration-200",
viewport === "desktop"
? "lg:w-[600px] lg:max-w-full"
: "lg:w-[375px]"
)}
>
- <HomePdfPreview
- pdfId={selectedPdf.id}
- selectedId={selectedComponentId}
- onSelect={setSelectedComponentId}
+ <PdfPreview
+ base={pdfBase}
+ name={selectedPdf.id}
+ className="w-full"
/>
</div>
</div>
@@ -487,7 +645,7 @@ export const HomePdfShowcase = () => {
<TabsContent value="code" className="min-h-0">
<CodeViewer
- availableOutputs={availableCodeOutputs}
+ availableOutputs={homePdfCodeOutputs}
error={
codeRequest.key === codeRequestKey
? codeRequest.error
@@ -498,6 +656,7 @@ export const HomePdfShowcase = () => {
}
output={codeOutput}
selectedOutput={selectedCodeOutput}
+ metadata={pdfMetadata}
onOutputChange={setCodeOutput}
/>
</TabsContent>
diff --git a/lib/format-code.ts b/lib/format-code.ts
index 0f60511..45a2a6c 100644
--- a/lib/format-code.ts
+++ b/lib/format-code.ts
@@ -49,6 +49,36 @@ export const formatCode = async (code: string) => {
"@/components/"
);
+ formattedCode = formattedCode.replaceAll(
+ "@/registry/themes",
+ "@/components/pdf/lib/themes"
+ );
+
+ formattedCode = formattedCode.replaceAll(
+ '"../../lib/',
+ '"@/components/pdf/lib/'
+ );
+
+ formattedCode = formattedCode.replaceAll(
+ '"../../components"',
+ '"@/components/pdf/components"'
+ );
+
+ formattedCode = formattedCode.replaceAll(
+ '"../table"',
+ '"@/components/pdf/components/table"'
+ );
+
+ formattedCode = formattedCode.replaceAll(
+ '"../table/',
+ '"@/components/pdf/components/table/'
+ );
+
+ formattedCode = formattedCode.replaceAll(
+ '"../lib/',
+ '"@/components/pdf/lib/'
+ );
+
formattedCode = formattedCode.replaceAll("export default", "export");
try {
diff --git a/registry/bases/forme/lib/themes.ts b/registry/bases/forme/lib/themes.ts
new file mode 100644
index 0000000..b071809
--- /dev/null
+++ b/registry/bases/forme/lib/themes.ts
@@ -0,0 +1,30 @@
+export type { PDFComponentProps, Style } from "../../../themes/component-types";
+export type {
+ PdfcnTheme,
+ PrimitiveTokens,
+ ColorTokens,
+ TypographyTokens,
+ SpacingTokens,
+ PageTokens,
+ TypographyScale,
+ SpacingScale,
+ FontWeights,
+ LineHeights,
+ BorderRadiusScale,
+ LetterSpacingScale,
+} from "../../../themes/theme-types";
+
+export {
+ defaultPrimitives,
+ professionalTheme,
+ modernTheme,
+ minimalTheme,
+ executiveTheme,
+ corporateTheme,
+ elegantTheme,
+ vividTheme,
+ forestTheme,
+ blueprintTheme,
+ themePresets,
+ type ThemePresetName,
+} from "../../../themes/themes-barrel";
diff --git a/registry/bases/takumi/lib/themes.ts b/registry/bases/takumi/lib/themes.ts
new file mode 100644
index 0000000..b071809
--- /dev/null
+++ b/registry/bases/takumi/lib/themes.ts
@@ -0,0 +1,30 @@
+export type { PDFComponentProps, Style } from "../../../themes/component-types";
+export type {
+ PdfcnTheme,
+ PrimitiveTokens,
+ ColorTokens,
+ TypographyTokens,
+ SpacingTokens,
+ PageTokens,
+ TypographyScale,
+ SpacingScale,
+ FontWeights,
+ LineHeights,
+ BorderRadiusScale,
+ LetterSpacingScale,
+} from "../../../themes/theme-types";
+
+export {
+ defaultPrimitives,
+ professionalTheme,
+ modernTheme,
+ minimalTheme,
+ executiveTheme,
+ corporateTheme,
+ elegantTheme,
+ vividTheme,
+ forestTheme,
+ blueprintTheme,
+ themePresets,
+ type ThemePresetName,
+} from "../../../themes/themes-barrel";