@taprel/react
v0.1.0
Published
Taprel for React: hooks for silent printing from web apps, through the desktop agent
Maintainers
Readme
Taprel for React
Silent printing from React apps: your page calls these hooks, and the Taprel desktop agent on the customer's PC prints with no print dialog. Works with Vite, Next.js and React 18 or newer.
Install
npm install @taprel/reactThis brings @taprel/sdk with it, and everything in the SDK can be imported from here. Printing also needs the Taprel agent installed on the PC with the printer. It's for Windows for now.
Print a receipt
import { PrintHelp, TaprelProvider, receipt, usePrint } from "@taprel/react";
function PrintButton() {
const { print, isPrinting, error } = usePrint();
return (
<>
<PrintHelp />
<button
disabled={isPrinting}
onClick={() =>
print(
receipt([
{ text: "Café Central", align: "center", bold: true, size: 2 },
{ line: true },
{ row: ["2 x Coffee", "6.00"] },
{ row: ["Total", "6.00"], bold: true },
{ cut: true },
]),
)
}
>
Print
</button>
{error && <p>{error.message}</p>}
</>
);
}
export default function App() {
return (
<TaprelProvider client={{ name: "my-pos" }}>
<PrintButton />
</TaprelProvider>
);
}The agent picks the customer's receipt printer, so no printer name ends up in your code. The SDK's README explains how to write documents and lists every error code.
When the customer gets asked
The first connection can show the customer two questions: the browser's and the agent's. Nothing here connects until you ask, so opening a page never shows them. They appear on the first print, on connect(), or on mount if you choose autoConnect.
PrintHelp tells the customer what to do while they wait, and when something is wrong. It shows nothing when all is well.
What's included
| Name | What it does |
|---|---|
| TaprelProvider | Holds the connection. Takes the SDK client's options, autoConnect, or your own printClient. Read once, when it mounts |
| usePrint() | print, printPdf, printRaw, isPrinting, error, result, warnings, reset |
| useTaprel() | status, error, agent, isConnected, connect, disconnect, client |
| usePrinters({ kind }) | printers, isLoading, error, refresh. Loads once connected |
| usePreview(document, { printer, printerKind, delayMs }) | src for an <img>, preview, isLoading, error. Asks once connected, when the content changes |
| PrintHelp | The customer's next step, with no styles of its own. Takes texts, downloadUrl, downloadText, className |
| usePrintHelp(texts) | The same message as { kind, message }, for your own UI |
Good to know
- Failed prints:
printgivesnulland setserror, so a click handler needs notry. Each call sends once and never retries, because a retry could print twice. AfterCONNECTION_LOSTorREQUEST_TIMEOUTthe job may still print, so let the person decide. - Double clicks: a second click sends a second print, so disable your button with
isPrinting. - "Printed": a
resultmeans "handed to the print queue", not "came out on paper". A job for an offline printer waits in the queue. - Printer list: check each printer's
status. The list doesn't update by itself, so callrefreshfrom a click. - Previews:
usePreviewasks nothing until connected, so callconnect()from a click if you want a preview before the first print. - Next.js: build documents with
receipt()andlabel()anywhere, including server components. The hooks andPrintHelprun on the client, and they're already marked for it. - Other languages: pass your own
textstoPrintHelp. The defaults are in English.
Development
npm test: builds and runs the tests in a pretend browser against a pretend agent
License
This package is MIT. The desktop agent it talks to is a separate product with its own terms.
