aboutsummaryrefslogtreecommitdiff
path: root/src/tools/qr-code-generator/useQRCode.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/qr-code-generator/useQRCode.ts')
-rw-r--r--src/tools/qr-code-generator/useQRCode.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/tools/qr-code-generator/useQRCode.ts b/src/tools/qr-code-generator/useQRCode.ts
new file mode 100644
index 0000000..c706d0d
--- /dev/null
+++ b/src/tools/qr-code-generator/useQRCode.ts
@@ -0,0 +1,35 @@
+import QRCode, { type QRCodeErrorCorrectionLevel, type QRCodeToDataURLOptions } from 'qrcode';
+import { ref, watch, type Ref } from 'vue';
+
+export function useQRCode({
+ text,
+ color: { background, foreground },
+ errorCorrectionLevel,
+ options,
+}: {
+ text: Ref<string>;
+ color: { foreground: Ref<string>; background: Ref<string> };
+ errorCorrectionLevel: Ref<QRCodeErrorCorrectionLevel>;
+ options?: QRCodeToDataURLOptions;
+}) {
+ const qrcode = ref('');
+
+ watch(
+ [text, background, foreground, errorCorrectionLevel],
+ async () => {
+ if (text.value)
+ qrcode.value = await QRCode.toDataURL(text.value, {
+ color: {
+ dark: foreground.value,
+ light: background.value,
+ ...options?.color,
+ },
+ errorCorrectionLevel: errorCorrectionLevel.value,
+ ...options,
+ });
+ },
+ { immediate: true }
+ );
+
+ return { qrcode };
+}