Skip to content

Commit f56cf73

Browse files
committed
web: Improve UX for copyCode animation
This commit introduces a new toast animation instead of the previous modal that would have to be manually closed. The new toast is less intrusive to the user's workflow and closes by itself. Fixes issue: 48950. Signed-off-by: Apoorva Pendse <[email protected]>
1 parent ce1d8e2 commit f56cf73

File tree

7 files changed

+71
-14
lines changed

7 files changed

+71
-14
lines changed

assets/js/sweetalert-2.1.2.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

assets/js/toastr-2.1.4.min.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/scss/_custom.scss

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1947,14 +1947,24 @@ body.td-search #search {
19471947

19481948
.code-sample > .copy-code-icon {
19491949
cursor: pointer;
1950-
text-align: right;
1950+
display: flex;
1951+
gap:1rem;
1952+
justify-content: right;
19511953
padding: 0.2rem;
19521954
}
19531955

19541956
.includecode .highlight {
19551957
margin-top: 0;
19561958
margin-bottom: 0;
19571959
}
1960+
#toast-container > *{
1961+
background: $primary;
1962+
}
1963+
1964+
.toast-success {
1965+
background-color: $primary !important;
1966+
color: #ffffff !important;
1967+
}
19581968

19591969
// handle main page features on narrow viewports
19601970
@media screen and (max-width: 768px) {

data/i18n/en/en.toml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ other = "Show More Posts..."
1818
[banner_acknowledgement]
1919
other = "Hide this notice"
2020

21+
[browser_clipboard_support_warning]
22+
other = "Sorry, your browser doesn't support copying this example to your clipboard."
23+
2124
[case_study_prefix]
2225
other = "Case study:"
2326

@@ -158,6 +161,9 @@ other = "Environment variables"
158161
[error_404_were_you_looking_for]
159162
other = "Were you looking for:"
160163

164+
[error_copy_to_clipboard]
165+
other = "Error"
166+
161167
[examples_heading]
162168
other = "Examples"
163169

@@ -600,6 +606,9 @@ other = "See Also"
600606
[subscribe_button]
601607
other = "Subscribe"
602608

609+
[success_copy_to_clipboard]
610+
other = "Success"
611+
603612
[synopsis_heading]
604613
other = "Synopsis"
605614

@@ -618,6 +627,12 @@ other = """<p>Items on this page refer to third party products or projects that
618627
[thirdparty_message_vendor]
619628
other = """Items on this page refer to vendors external to Kubernetes. The Kubernetes project authors aren't responsible for those third-party products or projects. To add a vendor, product or project to this list, read the <a href="/docs/contribute/style/content-guide/#third-party-content">content guide</a> before submitting a change. <a href="#third-party-content-disclaimer">More information.</a>"""
620629

630+
[toast_failure_copycode]
631+
other="Failed to copy to clipboard: "
632+
633+
[toast_success_copycode]
634+
other = "Copied to clipboard: "
635+
621636
[translated_by]
622637
other = "Translated By"
623638

@@ -639,7 +654,8 @@ other = "Versions"
639654
[warning]
640655
other = "Warning:"
641656

642-
[whatsnext_heading]
643-
other = "What's next"
644-
657+
[warning_copy_to_clipboard]
658+
other = "Warning"
645659

660+
[whatsnext_heading]
661+
other = "What's next"

layouts/partials/hooks/body-end.html

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,54 @@
66
{{ end }}
77
{{/* copy-and-paste helper for codenew shortcode */}}
88
{{- if or (.HasShortcode "code_sample") (.HasShortcode "code") (.HasShortcode "codenew") -}}
9+
{{- $toastrJs := resources.Get "js/toastr-2.1.4.min.js" | minify | fingerprint -}}
910
{{- if hugo.IsProduction -}}
10-
{{- $sweetAlert := resources.Get "js/sweetalert-2.1.2.min.js" | minify | fingerprint -}}
11-
<script async src="{{ $sweetAlert.RelPermalink }}" integrity="{{ $sweetAlert.Data.Integrity }}" crossorigin="anonymous"></script>
11+
<script async src="{{ $toastrJs.RelPermalink }}"></script>
1212
{{- else -}}
13-
{{- $sweetAlert := resources.Get "js/sweetalert-2.1.2.min.js" -}}
14-
<script async src="{{ $sweetAlert.RelPermalink }}"></script>
13+
<script async src="{{ $toastrJs.RelPermalink }}"></script>
1514
{{- end -}}
15+
1616
<script type="text/javascript">
17-
function copyCode(elem){
17+
function copyCode(elem) {
1818
if (document.getElementById(elem)) {
1919
if (navigator.clipboard) {
2020
navigator.clipboard.writeText(document.getElementById(elem).textContent).then(
2121
function () {
22-
swal("Copied to clipboard: ",elem);
22+
toastr.options = {
23+
closeButton: true,
24+
progressBar: true,
25+
positionClass: "toast-bottom-center",
26+
timeOut: 2000,
27+
preventDuplicates: true,
28+
newestOnTop: true
29+
};
30+
toastr.success("{{i18n "toast_success_copycode"}}" + elem, "{{i18n "success_copy_to_clipboard"}}");
2331
},
2432
function () {
25-
swal("Oh, no…","Failed to copy to clipboard: ",elem);
26-
},
33+
toastr.options = {
34+
closeButton: true,
35+
progressBar: true,
36+
positionClass: "toast-bottom-center",
37+
timeOut: 2000,
38+
preventDuplicates: true,
39+
newestOnTop: true
40+
};
41+
toastr.error("{{i18n "toast_failure_copycode"}}" + elem, "{{i18n "error_copy_to_clipboard"}}");
42+
}
2743
);
2844
} else {
29-
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
45+
toastr.options = {
46+
closeButton: true,
47+
progressBar: true,
48+
positionClass: "toast-bottom-center",
49+
timeOut: 2000,
50+
preventDuplicates: true,
51+
newestOnTop: true
52+
};
53+
toastr.warning("{{i18n "browser_clipboard_support_warning"}}", "{{i18n "warning_copy_to_clipboard"}}");
3054
}
3155
}
3256
}
3357
</script>
58+
3459
{{- end -}}

layouts/partials/hooks/head-end.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,7 @@
6161
{{- $bannerDismissJs := resources.Get "js/banner-dismiss.js" -}}
6262
<script defer src="{{ $bannerDismissJs.RelPermalink }}"></script>
6363
{{- end -}}
64+
65+
{{- if or (.HasShortcode "code_sample") (.HasShortcode "code") (.HasShortcode "codenew") -}}
66+
<link rel="stylesheet" href="/css/toastr-2.1.4.min.css">
67+
{{- end -}}

static/css/toastr-2.1.4.min.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)