aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Qr.svelte
blob: 988faf25bc542cae2ccc9d6b109e8f211f5ad966 (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
<script>
	import screenfull from 'screenfull';
	import { browser } from '$app/environment';
	import { onMount } from 'svelte';
	import { copy } from '$lib/utils';

	export let image, text, qr, disabled;

	let toggle = () => screenfull.toggle(qr.firstChild);

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

		const qrCode = new QRCodeStyling({
			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
			}
		});

		while (qr.firstChild) qr.removeChild(qr.lastChild);
		qrCode.append(qr);
	};
</script>

<div bind:this={qr} on:click={toggle} />

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