mirror of
				https://github.com/CorentinTh/it-tools.git
				synced 2025-11-03 21:43:21 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			37 lines
		
	
	
		
			1005 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			1005 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { get, type MaybeRef } from '@vueuse/core';
 | 
						|
import QRCode, { type QRCodeErrorCorrectionLevel, type QRCodeToDataURLOptions } from 'qrcode';
 | 
						|
import { ref, watch, isRef } from 'vue';
 | 
						|
 | 
						|
export function useQRCode({
 | 
						|
  text,
 | 
						|
  color: { background, foreground },
 | 
						|
  errorCorrectionLevel,
 | 
						|
  options,
 | 
						|
}: {
 | 
						|
  text: MaybeRef<string>;
 | 
						|
  color: { foreground: MaybeRef<string>; background: MaybeRef<string> };
 | 
						|
  errorCorrectionLevel?: MaybeRef<QRCodeErrorCorrectionLevel>;
 | 
						|
  options?: QRCodeToDataURLOptions;
 | 
						|
}) {
 | 
						|
  const qrcode = ref('');
 | 
						|
 | 
						|
  watch(
 | 
						|
    [text, background, foreground, errorCorrectionLevel].filter(isRef),
 | 
						|
    async () => {
 | 
						|
      if (get(text))
 | 
						|
        qrcode.value = await QRCode.toDataURL(get(text), {
 | 
						|
          color: {
 | 
						|
            dark: get(foreground),
 | 
						|
            light: get(background),
 | 
						|
            ...options?.color,
 | 
						|
          },
 | 
						|
          errorCorrectionLevel: get(errorCorrectionLevel) ?? 'M',
 | 
						|
          ...options,
 | 
						|
        });
 | 
						|
    },
 | 
						|
    { immediate: true },
 | 
						|
  );
 | 
						|
 | 
						|
  return { qrcode };
 | 
						|
}
 |