App patterns
PrintArea
Prints one part of a page on A4, portrait or landscape, black on white in every theme. The header repeats at the top of every page, and the page numbers are at the bottom. Nothing else on the page prints, also not an open sheet. The area is hidden on screen. Render the rows before print() (with flushSync when they change in the same click). Over 500 rows, turn the print button off and show tooManyRowsToPrint: export the list, or filter it.
import { PrintArea, usePrint } from "@percentsa/ui";Examples
Code
export function PrintAreaExample() {
const t = useT();
const s = useStrings();
const area = useRef<HTMLDivElement>(null);
const { print, printing } = usePrint();
const customers = [t("Nakheel Co.", "شركة نخيل"), t("Masar Trading", "مسار التجارية"), t("Sadeem Est.", "مؤسسة سديم")];
const rows = Array.from({ length: 30 }, (_, i) => ({ number: `ORD-${1001 + i}`, customer: customers[i % 3], total: 450 + i * 125 }));
const tooMany = rows.length > 500;
return (
<div className="grid justify-items-start gap-2">
<Button variant="secondary" disabled={tooMany || printing} onClick={() => print(area.current)}>
<Printer aria-hidden />
{t("Print the list", "طباعة القائمة")}
</Button>
{tooMany && (
<Text size="sm" tone="muted">
{s.tooManyRowsToPrint(500)}
</Text>
)}
<PrintArea
ref={area}
orientation="landscape"
header={
<div className="flex items-baseline justify-between gap-4 border-b border-line pb-2">
<Heading level={2} size={5}>
{t("Orders", "الطلبات")}
</Heading>
<Text size="sm" tone="muted">
{t("30 orders", "30 طلبًا")}
</Text>
</div>
}
>
<table className="w-full border-collapse text-sm">
<TableHeader>
<TableRow>
<TableHead>{t("Number", "الرقم")}</TableHead>
<TableHead>{t("Customer", "العميل")}</TableHead>
<TableHead align="end">{t("Total", "الإجمالي")}</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{rows.map((r) => (
<TableRow key={r.number}>
<TableCell>{r.number}</TableCell>
<TableCell>{r.customer}</TableCell>
<TableCell align="end">
<Money amount={r.total} decimals={2} />
</TableCell>
</TableRow>
))}
</TableBody>
</table>
</PrintArea>
</div>
);
}
/** A driver app frame: no navigation, so no sidebar. The header row shows at every width; the page centres its content with Container. */