Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/barcode/license.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Plus Five Five, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
62 changes: 62 additions & 0 deletions packages/barcode/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"name": "@react-email/barcode",
"version": "0.0.1",
"description": "Generate barcodes as pure HTML tables for email — no images needed.",
"sideEffects": false,
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"files": [
"dist/**"
],
"exports": {
".": {
"import": {
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"require": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"scripts": {
"build": "tsdown src/index.ts --format esm,cjs --dts --external react",
"build:watch": "tsdown src/index.ts --format esm,cjs --dts --external react --watch",
"clean": "rm -rf dist",
"test": "vitest run",
"test:watch": "vitest"
},
"keywords": [
"react",
"email",
"barcode",
"qrcode",
"html-table"
],
"repository": {
"type": "git",
"url": "https://github.com/resend/react-email.git",
"directory": "packages/barcode"
},
"engines": {
"node": ">=20.0.0"
},
"publishConfig": {
"access": "public"
},
"license": "MIT",
"dependencies": {
"bwip-js": "^4.5.1",
"qrcode-generator": "^1.4.4"
},
"peerDependencies": {
"react": "^18.0 || ^19.0 || ^19.0.0-rc"
},
"devDependencies": {
"@react-email/render": "workspace:*",
"tsconfig": "workspace:*",
"typescript": "5.8.3"
}
}
44 changes: 44 additions & 0 deletions packages/barcode/src/align-pos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/** QR alignment pattern center positions by version (1-40). Index 0 is unused. */
export const ALIGN_POS: (number[] | null)[] = [
null,
[],
[6, 18],
[6, 22],
[6, 26],
[6, 30],
[6, 34],
[6, 22, 38],
[6, 24, 42],
[6, 26, 46],
[6, 28, 50],
[6, 30, 54],
[6, 32, 58],
[6, 34, 62],
[6, 26, 46, 66],
[6, 26, 48, 70],
[6, 26, 50, 74],
[6, 30, 54, 78],
[6, 30, 56, 82],
[6, 30, 58, 86],
[6, 34, 62, 90],
[6, 28, 50, 72, 94],
[6, 26, 50, 74, 98],
[6, 30, 54, 78, 102],
[6, 28, 54, 80, 106],
[6, 32, 58, 84, 110],
[6, 30, 58, 86, 114],
[6, 34, 62, 90, 118],
[6, 26, 50, 74, 98, 122],
[6, 30, 54, 78, 102, 126],
[6, 26, 52, 78, 104, 130],
[6, 30, 56, 82, 108, 134],
[6, 34, 60, 86, 112, 138],
[6, 30, 58, 86, 114, 142],
[6, 34, 62, 90, 118, 146],
[6, 30, 54, 78, 102, 126, 150],
[6, 24, 50, 76, 102, 128, 154],
[6, 28, 54, 80, 106, 132, 158],
[6, 32, 58, 84, 110, 136, 162],
[6, 26, 54, 82, 110, 138, 166],
[6, 30, 58, 86, 114, 142, 170],
];
97 changes: 97 additions & 0 deletions packages/barcode/src/barcode.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { render } from '@react-email/render';
import { Barcode } from './index';

describe('<Barcode> component', () => {
it('renders a QR code as an HTML table', async () => {
const html = await render(
<Barcode value="https://example.com" type="qrcode" />,
);
expect(html).toContain('data-id="react-email-barcode"');
expect(html).toContain('<table');
expect(html).toContain('role="presentation"');
expect(html).toContain('border-collapse:collapse');
});

it('renders a Code128 barcode', async () => {
const html = await render(
<Barcode value="https://example.com" type="code128" />,
);
expect(html).toContain('<table');
expect(html).toContain('data-id="react-email-barcode"');
});

it('renders an Aztec code', async () => {
const html = await render(
<Barcode value="https://example.com" type="azteccode" />,
);
expect(html).toContain('<table');
expect(html).toContain('data-id="react-email-barcode"');
});

it('applies custom foreground and background colors', async () => {
const html = await render(
<Barcode
value="test"
type="qrcode"
foregroundColor="#ff0000"
backgroundColor="#00ff00"
/>,
);
expect(html).toContain('background:#f00');
expect(html).toContain('background:#0f0');
});

it('respects custom cellSize', async () => {
const html = await render(
<Barcode value="test" type="qrcode" cellSize={8} />,
);
// Cell size should appear in width/height styles
expect(html).toContain('height:8px');
});

it('renders without quiet zone when disabled', async () => {
const withQuiet = await render(
<Barcode value="test" type="qrcode" quietZone={true} />,
);
const withoutQuiet = await render(
<Barcode value="test" type="qrcode" quietZone={false} />,
);
// Without quiet zone should have fewer rows (less HTML)
expect(withoutQuiet.length).toBeLessThan(withQuiet.length);
});

it('renders with lossy compression enabled', async () => {
const html = await render(
<Barcode
value="https://example.com"
type="qrcode"
lossy={true}
lossyBudget={0.5}
errorCorrection="H"
/>,
);
expect(html).toContain('<table');
expect(html).toContain('data-id="react-email-barcode"');
});

it('includes Safari sizing cell in every row', async () => {
const html = await render(
<Barcode value="test" type="qrcode" cellSize={4} />,
);
// Each <tr> should have a zero-width sizing cell
expect(html).toContain('width:0;height:4px;padding:0');
});

it('passes through extra props', async () => {
const html = await render(
<Barcode
value="test"
type="qrcode"
data-testid="barcode-test"
style={{ padding: '16px' }}
/>,
);
expect(html).toContain('data-testid="barcode-test"');
expect(html).toContain('padding:16px');
});
});
122 changes: 122 additions & 0 deletions packages/barcode/src/barcode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import * as React from 'react';
import {
applyLossyCompression,
buildProtectionMask,
buildQuietZoneMask,
enforceColumnUniformity,
} from './compression';
import { generateBwip } from './generate-bwip';
import { generateQr } from './generate-qr';
import { packBestOrientation } from './packing';
import { renderTable } from './table-renderer';
import {
BARCODE_TYPES,
type BarcodeProps,
type ErrorCorrectionLevel,
} from './types';

const EC_RATES: Record<ErrorCorrectionLevel, number> = {
L: 0.07,
M: 0.15,
Q: 0.25,
H: 0.3,
};

const BWIP_EC_RATES: Record<ErrorCorrectionLevel, number> = {
L: 0.1,
M: 0.23,
Q: 0.36,
H: 0.5,
};

export const Barcode = React.forwardRef<HTMLDivElement, BarcodeProps>(
(
{
value,
type = 'qrcode',
foregroundColor = '#000000',
backgroundColor = '#ffffff',
cellSize = 4,
quietZone = true,
errorCorrection = 'M',
lossy = false,
lossyBudget = 0.2,
style,
...props
},
ref,
) => {
const cfg = BARCODE_TYPES[type];
const pad = quietZone ? 4 : 0;

let grid: boolean[][];
let moduleRows: number;
let moduleCols: number;

if (cfg.lib === 'qr') {
const result = generateQr(value, errorCorrection, pad);
grid = result.grid;
moduleRows = result.moduleRows;
moduleCols = result.moduleCols;
} else {
const result = generateBwip(type, value, cfg.hasEc, errorCorrection, pad);
grid = result.grid;
moduleRows = result.moduleRows;
moduleCols = result.moduleCols;
}

const totalRows = moduleRows + pad * 2;
const totalCols = moduleCols + pad * 2;

// Apply lossy compression
if (cfg.hasLossy && lossy) {
let protect: Uint8Array[];
let ecPct: number;
if (type === 'qrcode') {
protect = buildProtectionMask(moduleRows, pad, totalRows);
ecPct = EC_RATES[errorCorrection];
} else {
protect = buildQuietZoneMask(pad, totalRows, totalCols);
ecPct = cfg.hasEc
? BWIP_EC_RATES[errorCorrection]
: (cfg.ecRate ?? 0.02);
}
const flipped = applyLossyCompression(
grid,
pad,
totalRows,
totalCols,
protect,
ecPct,
lossyBudget,
);

// 1D: enforce column uniformity
if (cfg.type === '1d' && flipped > 0) {
enforceColumnUniformity(grid, pad, totalRows, totalCols, moduleRows);
}
}

const { spans } = packBestOrientation(grid, totalRows, totalCols);
const tableHTML = renderTable(
spans,
totalRows,
totalCols,
cellSize,
foregroundColor,
backgroundColor,
);

return (
<div
{...props}
dangerouslySetInnerHTML={{ __html: tableHTML }}
data-id="react-email-barcode"
ref={ref}
style={style}
/>
);
},
);

Barcode.displayName = 'Barcode';
Loading
Loading