25
25
<button
26
26
v-if =" copyToClipboard"
27
27
class =" copy-button"
28
- :class =" { copied: isCopied } "
28
+ :class =" copyState "
29
29
@click =" copyCodeToClipboard"
30
30
:aria-label =" $t('icons.copy')"
31
31
:title =" $t('icons.copy')"
32
32
>
33
- <CopyIcon v-if =" !isCopied" class =" copy-icon" />
34
- <CheckmarkIcon v-if =" isCopied" class =" checkmark-icon" />
33
+ <CopyIcon v-if =" copyState === 'idle'" class =" copy-icon" />
34
+ <CheckmarkIcon v-if =" copyState === 'success'" class =" checkmark-icon" />
35
+ <CrossIcon v-if =" copyState === 'failure'" class =" cross-icon" />
35
36
</button >
36
37
<!-- Do not add newlines in <pre>, as they'll appear in the rendered HTML. -->
37
38
<pre ><CodeBlock ><template
@@ -58,6 +59,7 @@ import Language from 'docc-render/constants/Language';
58
59
import CodeBlock from ' docc-render/components/CodeBlock.vue' ;
59
60
import CopyIcon from ' docc-render/components/Icons/CopyIcon.vue' ;
60
61
import CheckmarkIcon from ' docc-render/components/Icons/CheckmarkIcon.vue' ;
62
+ import CrossIcon from ' docc-render/components/Icons/CrossIcon.vue' ;
61
63
import { highlightContent , registerHighlightLanguage } from ' docc-render/utils/syntax-highlight' ;
62
64
63
65
import CodeListingFilename from ' ./CodeListingFilename.vue' ;
@@ -69,11 +71,12 @@ export default {
69
71
CodeBlock,
70
72
CopyIcon,
71
73
CheckmarkIcon,
74
+ CrossIcon,
72
75
},
73
76
data () {
74
77
return {
75
78
syntaxHighlightedLines: [],
76
- isCopied : false ,
79
+ copyState : ' idle ' ,
77
80
};
78
81
},
79
82
props: {
@@ -151,14 +154,17 @@ export default {
151
154
copyCodeToClipboard () {
152
155
navigator .clipboard .writeText (this .copyableText )
153
156
.then (() => {
154
- this .isCopied = true ;
157
+ this .copyState = ' success' ;
158
+ })
159
+ .catch ((err ) => {
160
+ console .error (' Failed to copy text: ' , err);
161
+ this .copyState = ' failure' ;
162
+ })
163
+ .finally (() => {
155
164
setTimeout (() => {
156
- this .isCopied = false ;
165
+ this .copyState = ' idle ' ;
157
166
}, 1000 );
158
- })
159
- .catch (err => (
160
- console .error (' Failed to copy text: ' , err)
161
- ));
167
+ });
162
168
},
163
169
},
164
170
};
@@ -289,9 +295,14 @@ pre {
289
295
fill : var (--color-figure-gray );
290
296
}
291
297
292
- .copy-button.copied .checkmark-icon {
298
+ .copy-button.success .checkmark-icon {
293
299
color : var (--color-figure-blue );
294
300
fill : currentColor ;
295
301
}
296
302
303
+ .copy-button.failure .cross-icon {
304
+ color : var (--color-figure-red );
305
+ fill : currentColor ;
306
+ }
307
+
297
308
</style >
0 commit comments