qrdx 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/README.md +414 -0
  2. package/dist/index.js +11 -0
  3. package/dist/index.js.map +1 -0
  4. package/dist/index.mjs +11 -0
  5. package/dist/index.mjs.map +1 -0
  6. package/dist/src/api.d.ts +3 -0
  7. package/dist/src/api.d.ts.map +1 -0
  8. package/dist/src/codegen.d.ts +101 -0
  9. package/dist/src/codegen.d.ts.map +1 -0
  10. package/dist/src/constants.d.ts +15 -0
  11. package/dist/src/constants.d.ts.map +1 -0
  12. package/dist/src/corner-dot.d.ts +4 -0
  13. package/dist/src/corner-dot.d.ts.map +1 -0
  14. package/dist/src/corner-eye.d.ts +3 -0
  15. package/dist/src/corner-eye.d.ts.map +1 -0
  16. package/dist/src/detection.d.ts +13 -0
  17. package/dist/src/detection.d.ts.map +1 -0
  18. package/dist/src/download.d.ts +36 -0
  19. package/dist/src/download.d.ts.map +1 -0
  20. package/dist/src/helpers.d.ts +36 -0
  21. package/dist/src/helpers.d.ts.map +1 -0
  22. package/dist/src/index.d.ts +25 -0
  23. package/dist/src/index.d.ts.map +1 -0
  24. package/dist/src/qr-code.d.ts +20 -0
  25. package/dist/src/qr-code.d.ts.map +1 -0
  26. package/dist/src/template/arrow.d.ts +3 -0
  27. package/dist/src/template/arrow.d.ts.map +1 -0
  28. package/dist/src/template/default.d.ts +3 -0
  29. package/dist/src/template/default.d.ts.map +1 -0
  30. package/dist/src/template/halloween.d.ts +3 -0
  31. package/dist/src/template/halloween.d.ts.map +1 -0
  32. package/dist/src/template/index.d.ts +7 -0
  33. package/dist/src/template/index.d.ts.map +1 -0
  34. package/dist/src/template/square-border.d.ts +3 -0
  35. package/dist/src/template/square-border.d.ts.map +1 -0
  36. package/dist/src/template/standard-box.d.ts +3 -0
  37. package/dist/src/template/standard-box.d.ts.map +1 -0
  38. package/dist/src/template/striked-box.d.ts +3 -0
  39. package/dist/src/template/striked-box.d.ts.map +1 -0
  40. package/dist/src/templates.d.ts +8 -0
  41. package/dist/src/templates.d.ts.map +1 -0
  42. package/dist/src/utils.d.ts +38 -0
  43. package/dist/src/utils.d.ts.map +1 -0
  44. package/dist/types/common.d.ts +9 -0
  45. package/dist/types/common.d.ts.map +1 -0
  46. package/dist/types/corner-dot.d.ts +3 -0
  47. package/dist/types/corner-dot.d.ts.map +1 -0
  48. package/dist/types/corner-eye.d.ts +3 -0
  49. package/dist/types/corner-eye.d.ts.map +1 -0
  50. package/dist/types/image-pattern.d.ts +3 -0
  51. package/dist/types/image-pattern.d.ts.map +1 -0
  52. package/dist/types/index.d.ts +10 -0
  53. package/dist/types/index.d.ts.map +1 -0
  54. package/dist/types/qr.d.ts +35 -0
  55. package/dist/types/qr.d.ts.map +1 -0
  56. package/dist/types/template.d.ts +34 -0
  57. package/dist/types/template.d.ts.map +1 -0
  58. package/package.json +51 -0
  59. package/src/api.tsx +85 -0
  60. package/src/codegen.ts +1043 -0
  61. package/src/constants.ts +26 -0
  62. package/src/corner-dot.tsx +101 -0
  63. package/src/corner-eye.ts +165 -0
  64. package/src/detection.ts +266 -0
  65. package/src/download.tsx +235 -0
  66. package/src/helpers.ts +59 -0
  67. package/src/index.tsx +306 -0
  68. package/src/qr-code.tsx +116 -0
  69. package/src/template/arrow.tsx +39 -0
  70. package/src/template/default.tsx +26 -0
  71. package/src/template/halloween.tsx +146 -0
  72. package/src/template/index.ts +6 -0
  73. package/src/template/square-border.tsx +42 -0
  74. package/src/template/standard-box.tsx +42 -0
  75. package/src/template/striked-box.tsx +41 -0
  76. package/src/templates.tsx +32 -0
  77. package/src/utils.tsx +750 -0
  78. package/types/common.ts +4 -0
  79. package/types/corner-dot.ts +13 -0
  80. package/types/corner-eye.ts +13 -0
  81. package/types/image-pattern.ts +12 -0
  82. package/types/index.ts +16 -0
  83. package/types/qr.ts +41 -0
  84. package/types/template.ts +41 -0
@@ -0,0 +1,235 @@
1
+ import type { QRProps } from "../types";
2
+ import { DEFAULT_SIZE } from "./constants";
3
+ import { getQRAsCanvas, getQRAsSVGDataUri } from "./index";
4
+
5
+ export type DownloadFormat = "png" | "jpg" | "svg";
6
+
7
+ export type DownloadSize = {
8
+ width: number;
9
+ height: number;
10
+ };
11
+
12
+ export const PRESET_SIZES: Record<string, DownloadSize> = {
13
+ small: { width: 200, height: 200 },
14
+ medium: { width: 400, height: 400 },
15
+ large: { width: 800, height: 800 },
16
+ xlarge: { width: 1200, height: 1200 },
17
+ "2xl": { width: 1600, height: 1600 },
18
+ "3xl": { width: 2000, height: 2000 },
19
+ };
20
+
21
+ export type DownloadOptions = {
22
+ format: DownloadFormat;
23
+ size: DownloadSize;
24
+ filename?: string;
25
+ };
26
+
27
+ /**
28
+ * Validates if a size value is within acceptable bounds
29
+ */
30
+ export function validateSize(
31
+ width: number,
32
+ height: number
33
+ ): {
34
+ isValid: boolean;
35
+ error?: string;
36
+ } {
37
+ const MIN_SIZE = 50;
38
+ const MAX_SIZE = 5000;
39
+
40
+ if (width < MIN_SIZE || height < MIN_SIZE) {
41
+ return {
42
+ isValid: false,
43
+ error: `Size must be at least ${MIN_SIZE}x${MIN_SIZE} pixels`,
44
+ };
45
+ }
46
+
47
+ if (width > MAX_SIZE || height > MAX_SIZE) {
48
+ return {
49
+ isValid: false,
50
+ error: `Size must not exceed ${MAX_SIZE}x${MAX_SIZE} pixels`,
51
+ };
52
+ }
53
+
54
+ if (width !== height) {
55
+ return {
56
+ isValid: false,
57
+ error: "Width and height must be equal for QR codes",
58
+ };
59
+ }
60
+
61
+ return { isValid: true };
62
+ }
63
+
64
+ /**
65
+ * Downloads a file to the user's device
66
+ */
67
+ function downloadFile(dataUrl: string, filename: string) {
68
+ const link = document.createElement("a");
69
+ link.href = dataUrl;
70
+ link.download = filename;
71
+ document.body.appendChild(link);
72
+ link.click();
73
+ document.body.removeChild(link);
74
+ }
75
+
76
+ /**
77
+ * Get the MIME type for a given format
78
+ */
79
+ function getMimeType(format: DownloadFormat): string {
80
+ switch (format) {
81
+ case "png":
82
+ return "image/png";
83
+ case "jpg":
84
+ return "image/jpeg";
85
+ case "svg":
86
+ return "image/svg+xml";
87
+ default:
88
+ return "image/png";
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Get the file extension for a given format
94
+ */
95
+ function getFileExtension(format: DownloadFormat): string {
96
+ return format === "jpg" ? "jpg" : format;
97
+ }
98
+
99
+ /**
100
+ * Downloads a QR code with the specified options
101
+ */
102
+ export async function downloadQRCode(
103
+ qrProps: QRProps,
104
+ options: DownloadOptions
105
+ ): Promise<void> {
106
+ const { format, size, filename } = options;
107
+
108
+ // Validate size
109
+ const validation = validateSize(size.width, size.height);
110
+ if (!validation.isValid) {
111
+ throw new Error(validation.error);
112
+ }
113
+
114
+ // Create props with the custom size
115
+ const propsWithSize = {
116
+ ...qrProps,
117
+ size: size.width, // QR codes are square, so we just use width
118
+ };
119
+
120
+ const defaultFilename = `qr-code-${size.width}x${size.height}.${getFileExtension(format)}`;
121
+ const finalFilename = filename || defaultFilename;
122
+
123
+ try {
124
+ if (format === "svg") {
125
+ // Download as SVG
126
+ const svgDataUri = await getQRAsSVGDataUri(propsWithSize);
127
+ downloadFile(svgDataUri, finalFilename);
128
+ } else {
129
+ // Download as PNG or JPG
130
+ const mimeType = getMimeType(format);
131
+ const dataUrl = await getQRAsCanvas(propsWithSize, mimeType);
132
+ if (typeof dataUrl === "string") {
133
+ downloadFile(dataUrl, finalFilename);
134
+ } else {
135
+ throw new Error("Failed to generate image data URL");
136
+ }
137
+ }
138
+ } catch (error) {
139
+ console.error(`Error downloading ${format.toUpperCase()}:`, error);
140
+ throw error;
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Gets a QR code as a data URL for preview or other purposes
146
+ */
147
+ export async function getQRCodeDataUrl(
148
+ qrProps: QRProps,
149
+ format: DownloadFormat,
150
+ size: DownloadSize
151
+ ): Promise<string> {
152
+ // Validate size
153
+ const validation = validateSize(size.width, size.height);
154
+ if (!validation.isValid) {
155
+ throw new Error(validation.error);
156
+ }
157
+
158
+ // Create props with the custom size
159
+ const propsWithSize = {
160
+ ...qrProps,
161
+ size: size.width,
162
+ };
163
+
164
+ if (format === "svg") {
165
+ return await getQRAsSVGDataUri(propsWithSize);
166
+ }
167
+
168
+ const mimeType = getMimeType(format);
169
+ const result = await getQRAsCanvas(propsWithSize, mimeType);
170
+
171
+ if (typeof result === "string") {
172
+ return result;
173
+ }
174
+
175
+ throw new Error("Failed to generate QR code data URL");
176
+ }
177
+
178
+ /**
179
+ * Copies QR code to clipboard (works best with PNG format)
180
+ */
181
+ export async function copyQRCodeToClipboard(
182
+ qrProps: QRProps,
183
+ size: DownloadSize = { width: DEFAULT_SIZE, height: DEFAULT_SIZE }
184
+ ): Promise<void> {
185
+ try {
186
+ // Validate size
187
+ const validation = validateSize(size.width, size.height);
188
+ if (!validation.isValid) {
189
+ throw new Error(validation.error);
190
+ }
191
+
192
+ const propsWithSize = {
193
+ ...qrProps,
194
+ size: size.width,
195
+ };
196
+
197
+ // Get canvas
198
+ const canvas = (await getQRAsCanvas(
199
+ propsWithSize,
200
+ "image/png",
201
+ true
202
+ )) as HTMLCanvasElement;
203
+
204
+ // Convert canvas to blob
205
+ const imageBlob = await new Promise<Blob>((resolve, reject) => {
206
+ canvas.toBlob((canvasBlob) => {
207
+ if (canvasBlob) {
208
+ resolve(canvasBlob);
209
+ } else {
210
+ reject(new Error("Failed to create blob from canvas"));
211
+ }
212
+ }, "image/png");
213
+ });
214
+
215
+ // Copy to clipboard
216
+ await navigator.clipboard.write([
217
+ new ClipboardItem({
218
+ "image/png": imageBlob,
219
+ }),
220
+ ]);
221
+
222
+ canvas.remove();
223
+ } catch (error) {
224
+ console.error("Error copying to clipboard:", error);
225
+ throw error;
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Gets SVG content as string (useful for copying SVG code)
231
+ */
232
+ export async function getSVGString(qrProps: QRProps): Promise<string> {
233
+ const svgDataUri = await getQRAsSVGDataUri(qrProps);
234
+ return decodeURIComponent(svgDataUri.replace("data:image/svg+xml,", ""));
235
+ }
package/src/helpers.ts ADDED
@@ -0,0 +1,59 @@
1
+ import type {
2
+ BodyPattern,
3
+ CornerEyeDotPattern,
4
+ CornerEyePattern,
5
+ } from "@/types";
6
+ import { FLAM_QR_LOGO } from "./constants";
7
+
8
+ export function getQRData({
9
+ url,
10
+ fgColor,
11
+ bgColor,
12
+ eyeColor,
13
+ dotColor,
14
+ bodyPattern,
15
+ hideLogo,
16
+ logo,
17
+ margin,
18
+ cornerEyePattern,
19
+ cornerEyeDotPattern,
20
+ level,
21
+ templateId,
22
+ }: {
23
+ url: string;
24
+ fgColor?: string;
25
+ bgColor?: string;
26
+ eyeColor?: string;
27
+ dotColor?: string;
28
+ bodyPattern?: BodyPattern;
29
+ hideLogo?: boolean;
30
+ logo?: string;
31
+ margin?: number;
32
+ cornerEyePattern?: CornerEyePattern;
33
+ cornerEyeDotPattern?: CornerEyeDotPattern;
34
+ level?: "L" | "M" | "Q" | "H";
35
+ templateId?: string;
36
+ }) {
37
+ return {
38
+ value: `${url}?qr=1`,
39
+ bgColor,
40
+ fgColor,
41
+ eyeColor,
42
+ dotColor,
43
+ bodyPattern,
44
+ cornerEyePattern,
45
+ cornerEyeDotPattern,
46
+ level,
47
+ templateId,
48
+ size: 1024,
49
+ margin,
50
+ ...(!hideLogo && {
51
+ imageSettings: {
52
+ src: logo || FLAM_QR_LOGO,
53
+ height: 256,
54
+ width: 256,
55
+ excavate: true,
56
+ },
57
+ }),
58
+ };
59
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,306 @@
1
+ /** biome-ignore-all lint/performance/noBarrelFile: false positive */
2
+ /** biome-ignore-all lint/style/noMagicNumbers: false positive */
3
+ /** biome-ignore-all lint/correctness/useImageSize: false positive */
4
+ /** biome-ignore-all lint/a11y/noNoninteractiveElementInteractions: false positive */
5
+ /** biome-ignore-all lint/performance/noImgElement: false positive */
6
+ /** biome-ignore-all lint/complexity/noExcessiveCognitiveComplexity: false positive */
7
+
8
+ /**
9
+ * @license qrcode.react
10
+ * Copyright (c) Paul O'Shannessy
11
+ * SPDX-License-Identifier: ISC
12
+ */
13
+ import { type JSX, useEffect, useRef, useState } from "react";
14
+ import type { QRProps, QRPropsCanvas } from "../types";
15
+ import qrcodegen from "./codegen";
16
+ import {
17
+ DEFAULT_BGCOLOR,
18
+ DEFAULT_FGCOLOR,
19
+ DEFAULT_LEVEL,
20
+ DEFAULT_MARGIN,
21
+ DEFAULT_SIZE,
22
+ ERROR_LEVEL_MAP,
23
+ } from "./constants";
24
+ import {
25
+ excavateModules,
26
+ generatePath,
27
+ getImageSettings,
28
+ SUPPORTS_PATH2D,
29
+ } from "./utils";
30
+
31
+ export * from "../types";
32
+ export * from "./api";
33
+ export * from "./detection";
34
+ export * from "./download";
35
+ export * from "./qr-code";
36
+ export * from "./templates";
37
+ export * from "./utils";
38
+
39
+ export function QRCodeCanvas(props: QRPropsCanvas) {
40
+ const {
41
+ value,
42
+ size = DEFAULT_SIZE,
43
+ level = DEFAULT_LEVEL,
44
+ bgColor = DEFAULT_BGCOLOR,
45
+ fgColor = DEFAULT_FGCOLOR,
46
+ margin = DEFAULT_MARGIN,
47
+ style,
48
+ imageSettings,
49
+ ...otherProps
50
+ } = props;
51
+ const imgSrc = imageSettings?.src;
52
+ const _canvas = useRef<HTMLCanvasElement>(null);
53
+ const _image = useRef<HTMLImageElement>(null);
54
+
55
+ // We're just using this state to trigger rerenders when images load. We
56
+ // Don't actually read the value anywhere. A smarter use of useEffect would
57
+ // depend on this value.
58
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
59
+ const [_isImgLoaded, setIsImageLoaded] = useState(false);
60
+
61
+ useEffect(() => {
62
+ // Always update the canvas. It's cheap enough and we want to be correct
63
+ // with the current state.
64
+ if (_canvas.current != null) {
65
+ const canvas = _canvas.current;
66
+
67
+ const ctx = canvas.getContext("2d");
68
+ if (!ctx) {
69
+ return;
70
+ }
71
+
72
+ let cells = qrcodegen.QrCode.encodeText(
73
+ value,
74
+ ERROR_LEVEL_MAP[level]
75
+ ).getModules();
76
+
77
+ const numCells = cells.length + margin * 2;
78
+ const calculatedImageSettings = getImageSettings(
79
+ cells,
80
+ size,
81
+ margin,
82
+ imageSettings
83
+ );
84
+
85
+ const image = _image.current;
86
+ const haveImageToRender =
87
+ calculatedImageSettings != null &&
88
+ image !== null &&
89
+ image.complete &&
90
+ image.naturalHeight !== 0 &&
91
+ image.naturalWidth !== 0;
92
+
93
+ if (haveImageToRender && calculatedImageSettings.excavation != null) {
94
+ cells = excavateModules(cells, calculatedImageSettings.excavation);
95
+ }
96
+
97
+ // We're going to scale this so that the number of drawable units
98
+ // matches the number of cells. This avoids rounding issues, but does
99
+ // result in some potentially unwanted single pixel issues between
100
+ // blocks, only in environments that don't support Path2D.
101
+ const pixelRatio = window.devicePixelRatio || 1;
102
+ canvas.height = canvas.width = size * pixelRatio;
103
+ const scale = (size / numCells) * pixelRatio;
104
+ ctx.scale(scale, scale);
105
+
106
+ // Draw solid background, only paint dark modules.
107
+ ctx.fillStyle = bgColor;
108
+ ctx.fillRect(0, 0, numCells, numCells);
109
+
110
+ ctx.fillStyle = fgColor;
111
+ if (SUPPORTS_PATH2D) {
112
+ // $FlowFixMe: Path2D c'tor doesn't support args yet.
113
+ ctx.fill(new Path2D(generatePath(cells, margin)));
114
+ } else {
115
+ cells.forEach((row, rdx) => {
116
+ row.forEach((cell, cdx) => {
117
+ if (cell) {
118
+ ctx.fillRect(cdx + margin, rdx + margin, 1, 1);
119
+ }
120
+ });
121
+ });
122
+ }
123
+
124
+ if (haveImageToRender) {
125
+ ctx.drawImage(
126
+ image,
127
+ calculatedImageSettings.x + margin,
128
+ calculatedImageSettings.y + margin,
129
+ calculatedImageSettings.w,
130
+ calculatedImageSettings.h
131
+ );
132
+ }
133
+ }
134
+ });
135
+
136
+ const canvasStyle = { height: size, width: size, ...style };
137
+ let img: JSX.Element | null = null;
138
+ if (imgSrc != null) {
139
+ img = (
140
+ <img
141
+ alt="QR code"
142
+ key={imgSrc}
143
+ onLoad={() => {
144
+ setIsImageLoaded(true);
145
+ }}
146
+ ref={_image}
147
+ src={imgSrc}
148
+ style={{ display: "none" }}
149
+ />
150
+ );
151
+ }
152
+ return (
153
+ <>
154
+ <canvas
155
+ height={size}
156
+ ref={_canvas}
157
+ style={canvasStyle}
158
+ width={size}
159
+ {...otherProps}
160
+ />
161
+ {img}
162
+ </>
163
+ );
164
+ }
165
+
166
+ export async function getQRAsSVGDataUri(props: QRProps) {
167
+ // Import QRCodeSVG and render it to string
168
+ const { QRCodeSVG } = await import("./utils");
169
+ const { renderToStaticMarkup } = await import("react-dom/server");
170
+ const React = await import("react");
171
+
172
+ // If there's an image in imageSettings, convert it to base64 first
173
+ let updatedProps = { ...props };
174
+ if (props.imageSettings?.src) {
175
+ try {
176
+ const base64Image = (await getBase64Image(
177
+ props.imageSettings.src
178
+ )) as string;
179
+ updatedProps = {
180
+ ...props,
181
+ imageSettings: {
182
+ ...props.imageSettings,
183
+ src: base64Image,
184
+ },
185
+ };
186
+ } catch (error) {
187
+ console.error("Failed to load image for QR code:", error);
188
+ // Continue without the image if it fails to load
189
+ }
190
+ }
191
+
192
+ // Create the SVG element with all props including templateId
193
+ // Filter out props that aren't valid for QRCodeSVG (like hideLogo, url, etc.)
194
+ const {
195
+ hideLogo: _hideLogo,
196
+ url: _url,
197
+ logo: _logo,
198
+ scale: _scale,
199
+ ...validSVGProps
200
+ } = updatedProps as any;
201
+
202
+ const svgElement = React.createElement(QRCodeSVG, {
203
+ ...validSVGProps,
204
+ templateId: updatedProps.templateId, // Ensure templateId is passed
205
+ customTemplate: updatedProps.customTemplate, // Ensure customTemplate is passed
206
+ });
207
+ const svgString = renderToStaticMarkup(svgElement);
208
+
209
+ return `data:image/svg+xml,${encodeURIComponent(svgString)}`;
210
+ }
211
+
212
+ const getBase64Image = (imgUrl: string) =>
213
+ new Promise((resolve, reject) => {
214
+ const img = new Image();
215
+ img.src = imgUrl;
216
+ img.setAttribute("crossOrigin", "anonymous");
217
+
218
+ img.onload = () => {
219
+ const canvas = document.createElement("canvas");
220
+
221
+ canvas.width = img.width;
222
+ canvas.height = img.height;
223
+
224
+ const ctx = canvas.getContext("2d");
225
+ ctx?.drawImage(img, 0, 0);
226
+
227
+ const dataURL = canvas.toDataURL("image/png");
228
+ resolve(dataURL);
229
+ };
230
+
231
+ img.onerror = () => {
232
+ reject("The image could not be loaded.");
233
+ };
234
+ });
235
+
236
+ // function waitUntilImageLoaded(img: HTMLImageElement, src: string) {
237
+ // return new Promise((resolve) => {
238
+ // function onFinish() {
239
+ // img.onload = null;
240
+ // img.onerror = null;
241
+ // resolve(true);
242
+ // }
243
+ // img.onload = onFinish;
244
+ // img.onerror = onFinish;
245
+ // img.src = src;
246
+ // img.loading = "eager";
247
+ // });
248
+ // }
249
+
250
+ export async function getQRAsCanvas(
251
+ props: QRProps,
252
+ type: string,
253
+ getCanvas?: boolean
254
+ ): Promise<HTMLCanvasElement | string> {
255
+ // First get the SVG using the same logic as displayed
256
+ const svgDataUri = await getQRAsSVGDataUri(props);
257
+ const svgString = decodeURIComponent(
258
+ svgDataUri.replace("data:image/svg+xml,", "")
259
+ );
260
+
261
+ return new Promise((resolve, reject) => {
262
+ const canvas = document.createElement("canvas");
263
+ const ctx = canvas.getContext("2d");
264
+ const img = new Image();
265
+
266
+ // Set canvas size to match the QR code
267
+ canvas.width = props.size || DEFAULT_SIZE;
268
+ canvas.height = props.size || DEFAULT_SIZE;
269
+
270
+ img.onload = () => {
271
+ URL.revokeObjectURL(url);
272
+ if (ctx) {
273
+ // Fill white background for JPG
274
+ if (type === "image/jpeg") {
275
+ ctx.fillStyle = "white";
276
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
277
+ }
278
+
279
+ ctx.drawImage(img, 0, 0);
280
+
281
+ if (getCanvas) {
282
+ resolve(canvas);
283
+ } else {
284
+ const dataUrl = canvas.toDataURL(type, 1.0);
285
+ canvas.remove();
286
+ resolve(dataUrl);
287
+ }
288
+ } else {
289
+ reject(new Error("Could not get canvas context"));
290
+ }
291
+ };
292
+
293
+ img.onerror = () => {
294
+ URL.revokeObjectURL(url);
295
+ reject(new Error("Could not load SVG"));
296
+ };
297
+
298
+ const svgBlob = new Blob([svgString], {
299
+ type: "image/svg+xml;charset=utf-8",
300
+ });
301
+ const url = URL.createObjectURL(svgBlob);
302
+ img.src = url;
303
+ });
304
+ }
305
+
306
+ export { getQRData } from "./helpers";
@@ -0,0 +1,116 @@
1
+ "use client";
2
+
3
+ import { memo, useMemo } from "react";
4
+ import type {
5
+ BodyPattern,
6
+ CornerEyeDotPattern,
7
+ CornerEyePattern,
8
+ TemplateDefinition,
9
+ } from "@/types";
10
+ import { DEFAULT_MARGIN } from "./constants";
11
+ import { getQRData } from "./helpers";
12
+ import { QRCodeSVG } from "./utils";
13
+
14
+ export const QRCode = memo(
15
+ ({
16
+ url,
17
+ fgColor,
18
+ hideLogo,
19
+ logo,
20
+ bgColor,
21
+ eyeColor,
22
+ dotColor,
23
+ bodyPattern,
24
+ cornerEyePattern,
25
+ cornerEyeDotPattern,
26
+ level,
27
+ scale = 1,
28
+ margin = DEFAULT_MARGIN,
29
+ templateId,
30
+ customTemplate,
31
+ customProps,
32
+ }: {
33
+ url: string;
34
+ fgColor?: string;
35
+ hideLogo?: boolean;
36
+ logo?: string;
37
+ bgColor?: string;
38
+ eyeColor?: string;
39
+ dotColor?: string;
40
+ bodyPattern?: BodyPattern;
41
+ cornerEyePattern?: CornerEyePattern;
42
+ cornerEyeDotPattern?: CornerEyeDotPattern;
43
+ level?: "L" | "M" | "Q" | "H";
44
+ scale?: number;
45
+ margin?: number;
46
+ templateId?: string;
47
+ customTemplate?: TemplateDefinition<any>;
48
+ customProps?: Record<string, any>;
49
+ }) => {
50
+ const qrData = useMemo(
51
+ () =>
52
+ getQRData({
53
+ url,
54
+ fgColor,
55
+ hideLogo,
56
+ bgColor,
57
+ eyeColor,
58
+ dotColor,
59
+ bodyPattern,
60
+ cornerEyePattern,
61
+ cornerEyeDotPattern,
62
+ level,
63
+ templateId,
64
+ logo,
65
+ margin,
66
+ }),
67
+ [
68
+ url,
69
+ fgColor,
70
+ hideLogo,
71
+ logo,
72
+ margin,
73
+ bgColor,
74
+ eyeColor,
75
+ dotColor,
76
+ bodyPattern,
77
+ cornerEyePattern,
78
+ cornerEyeDotPattern,
79
+ level,
80
+ templateId,
81
+ ]
82
+ );
83
+
84
+ return (
85
+ <QRCodeSVG
86
+ bgColor={qrData.bgColor}
87
+ bodyPattern={qrData.bodyPattern}
88
+ cornerEyeDotPattern={qrData.cornerEyeDotPattern}
89
+ cornerEyePattern={qrData.cornerEyePattern}
90
+ customProps={customProps}
91
+ dotColor={qrData.dotColor}
92
+ eyeColor={qrData.eyeColor}
93
+ fgColor={qrData.fgColor}
94
+ level={level || qrData.level}
95
+ margin={qrData.margin}
96
+ size={(qrData.size / 8) * scale}
97
+ templateId={qrData.templateId}
98
+ customTemplate={customTemplate}
99
+ value={qrData.value}
100
+ {...(qrData.imageSettings && {
101
+ imageSettings: {
102
+ ...qrData.imageSettings,
103
+ height: qrData.imageSettings
104
+ ? (qrData.imageSettings.height / 8) * scale
105
+ : 0,
106
+ width: qrData.imageSettings
107
+ ? (qrData.imageSettings.width / 8) * scale
108
+ : 0,
109
+ },
110
+ })}
111
+ />
112
+ );
113
+ }
114
+ );
115
+
116
+ QRCode.displayName = "QRCode";