A simple, managed QR toolset for browser frontends. It gives SPAs a small API to display, print, and scan QR codes with typed errors and zero framework lock-in.
- Runtimes: modern browsers (primary target); Node >= 18 for build/test workflows.
- Module format: ESM-only.
- Required browser APIs:
document,<dialog>,Blob,URL.createObjectURL, and camera/media APIs for scanning. - TypeScript: bundled types.
- Minimal API for common frontend QR workflows.
- Managed UI flows for display, print, and scan.
- Typed, explicit errors for predictable app-level handling.
- Works cleanly in SPA environments without framework-specific code.
npm install @z-base/qr
# or
pnpm add @z-base/qr
# or
yarn add @z-base/qrimport { QR, QRError } from '@z-base/qr'
QR.display('https://example.com')
QR.print('https://example.com')
try {
const value = await QR.scan()
console.log(value)
} catch (error) {
if (error instanceof QRError) {
console.error(error.code, error.message)
} else {
console.error(error)
}
}You can also import function-level APIs directly:
import { display, print, scan } from '@z-base/qr'- Opens a modal dialog with a generated QR image for
value. - Closes on user interaction (pointer/mouse/touch/key) after a short guard delay.
- Opens a new tab with an A4 print layout of ID-1 sized QR cards.
- Triggers the browser print dialog automatically.
- Opens a modal camera scanner.
- Resolves with decoded QR payload.
- Rejects with
QRErrorwhen scanning cannot proceed or is cancelled. - Uses the same short interaction guard delay pattern as
display()before close listeners activate.
The package throws QRError with semantic code values:
VALUE_IS_NOT_A_STRINGQR_ENCODE_FAILEDNO_CAMERA_AVAILABLECAMERA_CHECK_FAILEDSCAN_CANCELLEDSCAN_START_FAILED
- This package is browser-first and UI-driven.
scan()requires camera permission and a compatible device/browser.print()relies on popup/new-tab behavior and browser print support.- Dialog/content/backdrop fade transitions are best-effort and may vary slightly by engine capabilities.
- Suite: unit + integration (Node), E2E (Playwright)
- Matrix: Chromium / Firefox / WebKit + mobile emulation (Pixel 5, iPhone 12)
- Coverage: c8 - 100% statements/branches/functions/lines
Apache-2.0