aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Qr.svelte
blob: 099d5fa5981dab00b484c630b190fefcd1ceb9b0 (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
<script>
	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 = {
		height: 300,
		width: 300,
		type: 'canvas',
		data: text,
		backgroundOptions: {
			color: 'rgba(0, 0, 0, 0)'
		},
		cornersSquareOptions: {
			type: 'extra-rounded'
		},
		dotsOptions: {
			type: 'extra-rounded'
		},

		image,
		imageOptions: {
			hideBackgroundDots: false
		}
	};

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

		const qrCode = new QRCodeStyling(opts);

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

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

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

<div bind:this={qr} on:click={toggle} class:hidden={full} />
<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>