Nasaq

Barcode

One-dimensional barcodes drawn as SVG with jsbarcode (Code 128, EAN-13, EAN-8, UPC-A, Code 39, ITF, Codabar, Pharmacode), with input validation that explains the problem, SVG and PNG download and a generator card.

PreviewOpen ↗

Code

import { Barcode } from "@fadymondy/nasaq/web";export function Label({ sku }: { sku: string }) {  return <Barcode value={sku} format="CODE128" downloadable downloadName={sku} />;}

Utilities · beta

Live examples and controls: Barcode in the lab.

Install

npx shadcn@latest add https://docs.nasaqui.com/r/barcode.json

A barcode as inline SVG. jsbarcode draws the bars; Nasaq checks the value first, so a bad EAN shows why ("needs 13 digits", "check digit should be 1") instead of a blank or broken image.

When to use

  • Product, asset and order labels.
  • A generator for staff who print their own codes.

When not to use

  • Links or free text for phones: use QrCode.
  • Anything secret: a barcode is plain data.

Import

import { Barcode, BarcodeGenerator } from "@fadymondy/nasaq/web";
// inside this monorepo: "@nasaq/web"

Quick start

import { Barcode } from "@fadymondy/nasaq/web";

export function Label({ sku }: { sku: string }) {
  return <Barcode value={sku} format="CODE128" downloadable downloadName={sku} />;
}

Anatomy

Barcode                         data-slot="barcode"
├─ error message                role="alert" (when the value is invalid)
├─ svg                          data-slot="barcode-svg" (role="img")
└─ actions (downloadable)       data-slot="barcode-actions": Download SVG, Download PNG
BarcodeGenerator                data-slot="barcode-generator" (Card): value, format, preview

API

Barcode: every div prop except children, plus:

PropTypeDefaultDescription
valuestringrequiredWhat the bars encode.
formatBarcodeFormat"CODE128"CODE128, EAN13, EAN8, UPC, CODE39, ITF14, ITF, codabar, pharmacode.
showValuebooleantruePrint the value under the bars.
height, barWidth, marginnumber80, 2, 10Geometry in px.
fg, bgstringblack, whiteAny CSS colour or token.
downloadablebooleanfalseShow Download SVG and PNG.
downloadName, pngSizestring, number"barcode", 1200File name and PNG width.
onValidate(problem | null) => voidCalled after each draw.
labelsPartial<BarcodeLabels>Override any string.

BarcodeGenerator: defaultValue, defaultFormat, formats, downloadName, labels.

Helpers (pure, tested): validateBarcode(format, value) returns { kind, ... } or null; gtinCheckDigit(digitsWithoutCheck).

Examples

Validate before saving

import { validateBarcode } from "@fadymondy/nasaq/web";

export const ok = validateBarcode("EAN13", "4006381333931") === null;

Only retail formats in the generator

import { BarcodeGenerator } from "@fadymondy/nasaq/web";

export const Retail = () => <BarcodeGenerator formats={["EAN13", "EAN8", "UPC"]} defaultFormat="EAN13" defaultValue="4006381333931" />;

Accessibility

  • The SVG is role="img" with a name that includes the format and value. Errors are role="alert".
  • Bars need contrast and a quiet zone: keep the default margin.

RTL & i18n

  • Bars and digits are always left-to-right (dir="ltr"), also in Arabic. The generator UI mirrors and has Arabic strings.

Styling & tokens

  • Black on white by default so scanners read it. Tokens resolve to concrete colours at download time.
  • Target [data-slot="barcode"], [data-slot="barcode-svg"].

Do / Don't

  • Do let the check digit be computed for you with gtinCheckDigit when you generate EANs.
  • Do print at least the size the format standard asks for.
  • Don't stretch the SVG unevenly: it changes the bar ratio.

Lab

https://docs.nasaqui.com/?path=/docs/components-utilities-barcode--docs

On this page