Skip to content

Commit dae86bc

Browse files
authored
refactor: clipboard checkmarks option (#571)
1 parent 37bdb86 commit dae86bc

File tree

2 files changed

+57
-11
lines changed

2 files changed

+57
-11
lines changed

resources/assets/js/clipboard.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
window.clipboard = () => {
1+
window.clipboard = (withCheckmarks) => {
22
return {
33
copying: false,
44
notSupported: false,
5+
showCheckmarks: false,
56

67
copy(value) {
78
this.copying = true;
89

9-
const clipboard = window.navigator.clipboard;
10+
if (withCheckmarks) {
11+
this.showCheckmarks = true;
12+
}
1013

14+
const clipboard = window.navigator.clipboard;
1115
if (clipboard && window.isSecureContext) {
1216
clipboard.writeText(value).then(
1317
() => (this.copying = false),
@@ -29,6 +33,12 @@ window.clipboard = () => {
2933

3034
// fallback to execCommand for older browsers and non-https
3135
this.copyUsingExec(value);
36+
37+
if (withCheckmarks) {
38+
setTimeout(() => {
39+
this.showCheckmarks = false;
40+
}, 5000);
41+
}
3242
},
3343

3444
copyUsingExec(value) {
Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,63 @@
11
@props ([
22
'value',
3-
'class' => 'h-10 w-12',
4-
'copyInput' => false,
5-
'noStyling' => false,
6-
'tooltipContent' => trans('tooltips.copied'),
7-
'wrapperClass' => '',
3+
'class' => 'h-10 w-12',
4+
'copyInput' => false,
5+
'noStyling' => false,
6+
'tooltipContent' => trans('tooltips.copied'),
7+
'wrapperClass' => '',
8+
'withCheckmarks' => false,
9+
'checkmarksClass' => null,
810
])
911

1012
<div
11-
x-data="clipboard()"
13+
x-data="clipboard({{ $withCheckmarks ? 'true' : 'false' }})"
1214
x-init="initClipboard()"
13-
class="{{ $wrapperClass }}"
15+
@class($wrapperClass)
1416
>
1517
<button
1618
type="button"
17-
class="clipboard @unless($noStyling) button-icon @endif {{ $class }}"
19+
@class([
20+
'clipboard relative',
21+
'button-icon' => ! $noStyling,
22+
$class,
23+
])
1824
tooltip-content="{{ $tooltipContent }}"
1925
@if($copyInput)
2026
x-on:click="copyFromInput('{{ $value }}')"
2127
@else
2228
x-on:click="copy('{{ $value }}')"
2329
@endif
2430
>
25-
<x-ark-icon name="copy" size="sm" />
31+
@unless ($withCheckmarks)
32+
<x-ark-icon
33+
name="copy"
34+
size="sm"
35+
/>
36+
@else
37+
<div
38+
:class="{ 'opacity-0': showCheckmarks }"
39+
class="transition-default"
40+
>
41+
<x-ark-icon
42+
name="copy"
43+
size="sm"
44+
/>
45+
</div>
46+
47+
<div
48+
x-show="showCheckmarks"
49+
@class([
50+
'absolute m-auto',
51+
$checkmarksClass,
52+
])
53+
x-cloak
54+
x-transition
55+
>
56+
<x-ark-icon
57+
name="double-check-mark"
58+
size="sm"
59+
/>
60+
</div>
61+
@endif
2662
</button>
2763
</div>

0 commit comments

Comments
 (0)