|
10 | 10 |
|
11 | 11 | let { question }: Props = $props(); |
12 | 12 |
|
| 13 | + let copied = $state(false); |
| 14 | +
|
| 15 | + const url = `${window.location.origin}/${question.key}`; |
| 16 | +
|
13 | 17 | $effect(() => { |
14 | 18 | var style = window.getComputedStyle(document.documentElement); |
15 | 19 | const light = style.getPropertyValue("--color-bg"); |
16 | 20 | const dark = style.getPropertyValue("--color-fg"); |
17 | 21 | const rem = parseFloat(style.fontSize); |
18 | 22 |
|
19 | 23 | const canvas = document.getElementById("qrcode") as HTMLCanvasElement; |
20 | | - QRCode.toCanvas(canvas, `${window.location.origin}/${question.key}`, { |
| 24 | + QRCode.toCanvas(canvas, url, { |
21 | 25 | color: { |
22 | 26 | dark, |
23 | 27 | light, |
|
26 | 30 | width: 12 * rem, |
27 | 31 | }); |
28 | 32 | }); |
| 33 | +
|
| 34 | + const copyLink = async () => { |
| 35 | + try { |
| 36 | + await navigator.clipboard.writeText(url); |
| 37 | + copied = true; |
| 38 | + setTimeout(() => { |
| 39 | + copied = false; |
| 40 | + }, 1500); |
| 41 | + } catch { |
| 42 | + /* Ignore error */ |
| 43 | + } |
| 44 | + }; |
29 | 45 | </script> |
30 | 46 |
|
31 | 47 | <Summary {question} /> |
32 | 48 | <div class="divider"></div> |
33 | 49 | <p>Scan the QR code to open the feedback form.</p> |
34 | | -<canvas id="qrcode"></canvas> |
| 50 | +<button class="qrcode-container" onclick={copyLink}> |
| 51 | + <canvas id="qrcode" title={url}></canvas> |
| 52 | + <div class="copy-message"> |
| 53 | + {#if copied}Link copied!{:else}Click to copy the link.{/if} |
| 54 | + </div> |
| 55 | +</button> |
35 | 56 |
|
36 | 57 | <style> |
| 58 | + p { |
| 59 | + margin: 1rem 0 2rem; |
| 60 | + } |
| 61 | +
|
37 | 62 | .divider { |
38 | 63 | border: 1px solid var(--color-border); |
39 | 64 | margin: 3rem 0 2rem; |
40 | 65 | } |
41 | 66 |
|
42 | | - canvas { |
43 | | - margin: 2rem 0 0; |
| 67 | + .qrcode-container { |
| 68 | + display: inline-block; |
| 69 | +
|
| 70 | + appearance: none; |
| 71 | + background: none; |
| 72 | + border: none; |
| 73 | + border-radius: var(--border-radius); |
| 74 | + color: inherit; |
| 75 | + margin: calc(var(--border-radius) * -1); |
| 76 | + padding: var(--border-radius); |
| 77 | + } |
| 78 | +
|
| 79 | + .qrcode-container:focus-visible { |
| 80 | + outline: 2px solid var(--color-fg); |
| 81 | + outline-offset: 0.25rem; |
| 82 | + } |
| 83 | +
|
| 84 | + .copy-message { |
| 85 | + font-size: 0.8rem; |
| 86 | + line-height: 1rem; |
| 87 | + text-align: center; |
| 88 | + opacity: 0; |
| 89 | + } |
| 90 | +
|
| 91 | + .qrcode-container:hover .copy-message, |
| 92 | + .qrcode-container:focus-visible .copy-message { |
| 93 | + opacity: 1; |
44 | 94 | } |
45 | 95 | </style> |
0 commit comments