Skip to content

Commit af047ac

Browse files
authored
Copy link to clipboard when clicking the QR code (#42)
1 parent 3515163 commit af047ac

File tree

1 file changed

+54
-4
lines changed

1 file changed

+54
-4
lines changed

front-end/src/views/Share.svelte

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,18 @@
1010
1111
let { question }: Props = $props();
1212
13+
let copied = $state(false);
14+
15+
const url = `${window.location.origin}/${question.key}`;
16+
1317
$effect(() => {
1418
var style = window.getComputedStyle(document.documentElement);
1519
const light = style.getPropertyValue("--color-bg");
1620
const dark = style.getPropertyValue("--color-fg");
1721
const rem = parseFloat(style.fontSize);
1822
1923
const canvas = document.getElementById("qrcode") as HTMLCanvasElement;
20-
QRCode.toCanvas(canvas, `${window.location.origin}/${question.key}`, {
24+
QRCode.toCanvas(canvas, url, {
2125
color: {
2226
dark,
2327
light,
@@ -26,20 +30,66 @@
2630
width: 12 * rem,
2731
});
2832
});
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+
};
2945
</script>
3046

3147
<Summary {question} />
3248
<div class="divider"></div>
3349
<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>
3556

3657
<style>
58+
p {
59+
margin: 1rem 0 2rem;
60+
}
61+
3762
.divider {
3863
border: 1px solid var(--color-border);
3964
margin: 3rem 0 2rem;
4065
}
4166
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;
4494
}
4595
</style>

0 commit comments

Comments
 (0)