Skip to content

Commit 285b947

Browse files
committed
feat: add reusable copy button component
1 parent 91ba44a commit 285b947

4 files changed

Lines changed: 166 additions & 2 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{#
2+
Reusable copy button component.
3+
Available props:
4+
- ariaLabel: label for better accessibility
5+
- class: additional class for the button
6+
- textToCopy: text to be copied to the clipboard (use '#' for anchor links)
7+
8+
Usage examples:
9+
{% include 'components/copy-button.twig' with { textToCopy: 'Lorem ipsum dolor' } %}
10+
{% include 'components/copy-button.twig' with { textToCopy: '#anchor-link-dolor' } %}
11+
#}
12+
13+
{% set attrNameForTextToCopy = 'data-text-to-copy' %}
14+
15+
{% set ariaLabel = ariaLabel ?? 'Copy to the Clipboard' %}
16+
17+
{% set mainTag = 'button' %}
18+
{% set mainClass = 'copy-button' %}
19+
20+
<{{ mainTag }} class="{{ mainClass }} {{ class ?? '' }}" aria-label="{{ ariaLabel }}" {{ attrNameForTextToCopy }}="{{ textToCopy }}">
21+
<div class="{{ mainClass }}__inner">
22+
<div class="{{ mainClass }}__icon--initial">{{ svg('copy') }}</div>
23+
<div class="{{ mainClass }}__icon--success">{{ svg('check') }}</div>
24+
</div>
25+
</{{ mainTag }}>

src/frontend/js/modules/page.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export default class Page {
2020
*/
2121
static get CSS() {
2222
return {
23-
page: 'page'
23+
copyButton: 'copy-button',
24+
copyButtonCopied: 'copy-button__copied',
25+
page: 'page',
2426
};
2527
}
2628

@@ -36,7 +38,11 @@ export default class Page {
3638
*/
3739
const page = document.querySelector(`.${Page.CSS.page}`);
3840

39-
page.addEventListener('click', (event) => { });
41+
page.addEventListener('click', (event) => {
42+
if (event.target.classList.contains(Page.CSS.copyButton)) {
43+
this.handleCopyButtonClickEvent(event);
44+
}
45+
});
4046
}
4147

4248
/**
@@ -73,4 +79,33 @@ export default class Page {
7379
console.error(error); // @todo send to Hawk
7480
}
7581
}
82+
83+
/**
84+
* Handles copy button click events
85+
*
86+
* @param {Event} e - Event Object.
87+
* @returns {Promise<void>}
88+
*/
89+
async handleCopyButtonClickEvent({ target }) {
90+
if (target.classList.contains(Page.CSS.copyButtonCopied)) return;
91+
92+
let textToCopy = target.getAttribute('data-text-to-copy');
93+
if (!textToCopy) return;
94+
95+
// Check if text to copy is an anchor link
96+
if (/^#\S*$/.test(textToCopy))
97+
textToCopy = window.location.origin + window.location.pathname + textToCopy;
98+
99+
try {
100+
await copyToClipboard(textToCopy);
101+
102+
target.classList.add(Page.CSS.copyButtonCopied);
103+
target.addEventListener('mouseleave', () => {
104+
setTimeout(() => target.classList.remove(Page.CSS.copyButtonCopied), 5e2);
105+
}, { once: true });
106+
107+
} catch (error) {
108+
console.error(error); // @todo send to Hawk
109+
}
110+
}
76111
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
.copy-button {
2+
position: relative;
3+
width: 28px;
4+
height: 28px;
5+
padding: 0;
6+
border: none;
7+
background: none;
8+
cursor: pointer;
9+
transition: opacity 200ms;
10+
11+
@media (--can-hover) {
12+
&:hover .copy-button__inner {
13+
background: var(--color-link-hover);
14+
}
15+
}
16+
17+
&::before {
18+
content: '';
19+
position: absolute;
20+
top: 0;
21+
right: 0;
22+
bottom: 0;
23+
left: 0;
24+
border-radius: 100%;
25+
background-color: var(--color-success);
26+
visibility: hidden;
27+
pointer-events: none;
28+
transform: scale(1);
29+
transition: transform 400ms ease-out, opacity 400ms;
30+
}
31+
32+
&__inner {
33+
@apply --squircle;
34+
35+
display: flex;
36+
justify-content: center;
37+
align-items: center;
38+
width: 100%;
39+
height: 100%;
40+
background: white;
41+
pointer-events: none;
42+
}
43+
44+
&__icon--initial {
45+
display: flex;
46+
transform: translateZ(0);
47+
}
48+
49+
&__icon--success {
50+
display: none;
51+
width: 24px;
52+
height: 24px;
53+
color: white;
54+
}
55+
56+
&__copied {
57+
58+
&::before {
59+
opacity: 0;
60+
visibility: visible;
61+
transform: scale(3.5);
62+
}
63+
64+
.copy-button__inner,
65+
.copy-button__inner:hover {
66+
background: var(--color-success) !important;
67+
animation: check-square-in 250ms ease-in;
68+
}
69+
70+
.copy-button__icon--initial {
71+
display: none;
72+
}
73+
74+
.copy-button__icon--success {
75+
display: flex;
76+
animation: check-sign-in 350ms ease-in forwards;
77+
}
78+
}
79+
80+
@keyframes check-sign-in {
81+
from {
82+
transform: scale(.7);
83+
}
84+
80% {
85+
transform: scale(1.1);
86+
}
87+
to {
88+
transform: none;
89+
}
90+
}
91+
92+
@keyframes check-square-in {
93+
from {
94+
transform: scale(1.05);
95+
}
96+
80% {
97+
transform: scale(.96);
98+
}
99+
to {
100+
transform: none;
101+
}
102+
}
103+
}

src/frontend/styles/main.pcss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
@import './carbon.pcss';
66
@import './components/header.pcss';
77
@import './components/writing.pcss';
8+
@import './components/copy-button.pcss';
89
@import './components/page.pcss';
910
@import './components/greeting.pcss';
1011
@import './components/auth.pcss';

0 commit comments

Comments
 (0)