aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Qr.svelte
blob: 0ae028b9c1920e10a8b61a54b59468b17ab9d5e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<script>
	import { fade } from 'svelte/transition';
	import screenfull from 'screenfull';
	import { browser } from '$app/environment';
	import { onMount, tick } from 'svelte';
	import { copy } from '$lib/utils';

	export let image, text, qr, fullQr, full;

	let toggle = async () => {
		full = !full;
		await tick();
		screenfull.toggle(fullQr.firstChild);
	};

	let opts = {
		type: 'canvas',
		backgroundOptions: {
			color: 'rgba(0, 0, 0, 0)'
		},
		cornersSquareOptions: {
			type: 'extra-rounded'
		},
		dotsOptions: {
			type: 'extra-rounded'
		},

		image,
		imageOptions: {
			hideBackgroundDots: false
		}
	};

	let mask = false;
	$: update(text);
	let update = async (text, fullscreen) => {
		mask = true;
		if (!(browser && text)) return;
		let { default: QRCodeStyling } = await import('qr-code-styling');

		opts.data = text;
		opts.height = opts.width = 300;
		const qrCode = new QRCodeStyling(opts);

		opts.height = opts.width = Math.min(window.innerWidth, window.innerHeight);
		const fullQrCode = new QRCodeStyling(opts);

		while (qr && qr.firstChild) qr.removeChild(qr.lastChild);
		qrCode.append(qr);

		while (fullQr && fullQr.firstChild) fullQr.removeChild(fullQr.lastChild);
		fullQrCode.append(fullQr);
		mask = false;
	};
</script>

{#if mask}
	<div class="absolute bg-white w-[300px] h-[300px] t-0 l-0 z-10" out:fade />
{/if}
<div bind:this={qr} on:click={toggle} class:hidden={full} class="relative overflow-visible" />
<div bind:this={fullQr} on:click={toggle} class:hidden={!full} />

<style>
	div {
		@apply w-[300px]
      h-[300px]
      rounded-3xl
      justify-center
      items-center
      cursor-pointer
      mx-auto;
	}
</style>