mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-11-14 10:57:29 +00:00
36 lines
919 B
TypeScript
36 lines
919 B
TypeScript
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 };
|
|
}
|