Document extraction
Split document-to-schema extraction view where hovering or focusing any parsed field highlights its source region on the preview.
pnpm dlx shadcn@latest add @haze/document-extract---
import { CheckCircle2, FileScan, Sparkles } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";
interface ExtractedField {
key: string;
label: string;
value: string;
confidence: string;
}
interface Props {
id?: string;
filename?: string;
fields?: ExtractedField[];
class?: string;
}
const {
id = "haze-document-extract",
filename = "INV-2026-0419.pdf",
fields = [
{ key: "vendor", label: "Vendor", value: "Tidewell Cloud Infrastructure Ltd.", confidence: "99%" },
{ key: "invoice", label: "Invoice number", value: "INV-2026-0419", confidence: "99%" },
{ key: "due", label: "Due date", value: "2026-12-15", confidence: "97%" },
{ key: "total", label: "Total due", value: "$4,820.00 USD", confidence: "98%" },
],
class: className,
} = Astro.props;
---
<section aria-labelledby={`${id}-title`} data-haze-doc-extract class={cn("bg-background px-4 py-10 sm:px-6 sm:py-14", className)}>
<div class="mx-auto max-w-5xl overflow-hidden rounded-2xl border border-border bg-card">
<header class="flex flex-wrap items-center justify-between gap-2 border-b border-border px-5 py-3.5">
<div class="flex items-center gap-2">
<FileScan class="size-4 text-muted-foreground" aria-hidden="true" />
<h2 id={`${id}-title`} class="text-sm/5 font-semibold text-foreground">Document extraction · {filename}</h2>
</div>
<span class="inline-flex items-center gap-1.5 rounded-full border border-frame-success bg-success/10 px-2.5 py-0.5 text-xs/5 font-medium text-success">
<CheckCircle2 class="size-3.5" aria-hidden="true" />
4 / 4 fields parsed
</span>
</header>
<div class="grid divide-y divide-border lg:grid-cols-2 lg:divide-x lg:divide-y-0">
<div class="bg-muted/40 p-5">
<p class="text-[0.6875rem]/4 font-medium tracking-wider text-muted-foreground uppercase">Source document preview</p>
<div class="mt-3 space-y-4 rounded-xl border border-border bg-card p-5 font-mono text-xs/6 text-muted-foreground">
<div class="flex flex-wrap items-start justify-between gap-3 border-b border-border pb-3">
<div data-doc-region="vendor" class="rounded-md border border-transparent px-2 py-1 transition-colors duration-150 data-[lit=true]:border-border-strong data-[lit=true]:bg-accent data-[lit=true]:text-foreground">
<p class="font-semibold text-foreground">Tidewell Cloud Infrastructure Ltd.</p>
<p>410 Market St, San Francisco, CA</p>
</div>
<div data-doc-region="invoice" class="rounded-md border border-transparent px-2 py-1 text-right transition-colors duration-150 data-[lit=true]:border-border-strong data-[lit=true]:bg-accent data-[lit=true]:text-foreground">
<p class="text-[0.6875rem] uppercase">Invoice</p>
<p class="font-semibold text-foreground">INV-2026-0419</p>
</div>
</div>
<div class="flex items-center justify-between">
<span>Issued: 2026-11-15</span>
<span data-doc-region="due" class="rounded-md border border-transparent px-2 py-0.5 transition-colors duration-150 data-[lit=true]:border-border-strong data-[lit=true]:bg-accent data-[lit=true]:text-foreground">
Due: <strong class="text-foreground">2026-12-15</strong>
</span>
</div>
<div class="space-y-1.5 border-y border-border py-3">
<div class="flex justify-between"><span>Dedicated compute cluster (Nov)</span><span>$3,900.00</span></div>
<div class="flex justify-between"><span>Global edge bandwidth (18 TB)</span><span>$920.00</span></div>
</div>
<div class="flex justify-end">
<div data-doc-region="total" class="rounded-md border border-transparent px-2.5 py-1 text-right transition-colors duration-150 data-[lit=true]:border-border-strong data-[lit=true]:bg-accent data-[lit=true]:text-foreground">
<span class="text-[0.6875rem] uppercase">Total due </span>
<strong class="text-sm text-foreground">$4,820.00 USD</strong>
</div>
</div>
</div>
</div>
<div class="p-5">
<div class="flex items-center justify-between gap-2">
<p class="text-[0.6875rem]/4 font-medium tracking-wider text-muted-foreground uppercase">Extracted schema</p>
<span class="inline-flex items-center gap-1 text-xs text-muted-foreground">
<Sparkles class="size-3 text-muted-foreground" aria-hidden="true" />
Hover or focus a field to inspect
</span>
</div>
<div class="mt-3 grid gap-3">
{
fields.map((f) => (
<div
data-doc-field={f.key}
class="rounded-xl border border-border bg-muted/40 p-3 transition-colors duration-150 hover:border-border-strong"
>
<div class="flex items-center justify-between gap-2">
<label for={`${id}-${f.key}`} class="text-xs font-medium text-muted-foreground">{f.label}</label>
<span class="font-mono text-[0.6875rem] text-foreground tabular-nums">{f.confidence}</span>
</div>
<input
id={`${id}-${f.key}`}
type="text"
value={f.value}
class="mt-1.5 h-8 w-full rounded-lg border border-frame-input bg-field px-2.5 text-sm font-medium text-foreground focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/24 focus-visible:outline-hidden transition-[color,background-color,border-color,box-shadow] duration-150 hover:border-frame-foreground"
/>
</div>
))
}
</div>
<p data-doc-status role="status" class="mt-3 min-h-4 text-xs/4 text-muted-foreground"></p>
</div>
</div>
</div>
</section>
<script>
function initDocExtractors() {
document.querySelectorAll<HTMLElement>("[data-haze-doc-extract]").forEach((root) => {
if (root.hasAttribute("data-enhanced")) return;
root.setAttribute("data-enhanced", "");
const status = root.querySelector<HTMLElement>("[data-doc-status]");
const highlight = (key: string | null) => {
root.querySelectorAll<HTMLElement>("[data-doc-region]").forEach((r) => r.setAttribute("data-lit", String(r.dataset.docRegion === key)));
};
root.querySelectorAll<HTMLElement>("[data-doc-field]").forEach((field) => {
const key = field.dataset.docField ?? null;
field.addEventListener("mouseenter", () => highlight(key));
field.addEventListener("mouseleave", () => highlight(null));
field.addEventListener("focusin", () => {
highlight(key);
if (status && key) status.textContent = `Highlighting ${key} in source document.`;
});
field.addEventListener("focusout", () => highlight(null));
});
});
}
initDocExtractors();
document.addEventListener("astro:page-load", initDocExtractors);
</script>When to use
- Reviewing AI-extracted fields from an invoice, contract or receipt alongside the source document
- Letting reviewers verify confidence scores and correct parsed values before saving
When not to use
- Showing a plain key-value table with no source document linkage; use table or data-grid
Installation
Add the Haze registry to components.json once, then install Document extraction with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/document-extractnpx shadcn@latest add @haze/document-extractyarn dlx shadcn@latest add @haze/document-extractbunx --bun shadcn@latest add @haze/document-extractFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id | string | "haze-document-extract" | Unique prefix for form labels and heading IDs. |
| filename | string | "INV-2026-0419.pdf" | Source filename shown in the header. |
| fields | { key: string; label: string; value: string; confidence: string }[] | 4 invoice fields | Structured fields linked by key to highlighted document regions. |
| class | string | – | Extra classes on the outer section. |
Keyboard interactions
- Tab
- Move through extracted field inputs and highlight each linked region on the document preview
Accessibility
- Highlighting triggers on both pointer hover and keyboard focusin so keyboard users get the same source-region feedback.
- Every extracted input has an associated label and announces its highlighted source region via role="status".
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI block "Document extraction" with `npx shadcn@latest add @haze/document-extract`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import DocumentExtract from "@/components/haze/blocks/document-extract.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/blocks/document-extract.md
Customize with real content
Adapt the Haze document-extract block for our product. Replace the demo values of these props with our real content: id, filename, fields, class. Keep the layout, the heading level rules and these accessibility details: Highlighting triggers on both pointer hover and keyboard focusin so keyboard users get the same source-region feedback. Every extracted input has an associated label and announces its highlighted source region via role="status". Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using document-extract, check it fits: use it when reviewing ai-extracted fields from an invoice, contract or receipt alongside the source document; letting reviewers verify confidence scores and correct parsed values before saving. Avoid it when showing a plain key-value table with no source document linkage; use table or data-grid. Alternatives: @haze/table, @haze/code-block.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.