Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web: Improve UX for copyCode animation #48958

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion assets/js/sweetalert-2.1.2.min.js

This file was deleted.

2 changes: 2 additions & 0 deletions assets/js/toastr-2.1.4.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion assets/scss/_custom.scss
apoorvapendse marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -1950,10 +1950,20 @@ body.td-search #search {

.code-sample > .copy-code-icon {
cursor: pointer;
text-align: right;
display: flex;
gap:1rem;
justify-content: right;
apoorvapendse marked this conversation as resolved.
Show resolved Hide resolved
padding: 0.2rem;
}

#toast-container > *{
background: $primary;
}

.toast-success {
background-color: $primary !important;
color: #ffffff !important;
}

// handle main page features on narrow viewports
@media screen and (max-width: 768px) {
Expand Down
22 changes: 19 additions & 3 deletions data/i18n/en/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ other = "Show More Posts..."
[banner_acknowledgement]
other = "Hide this notice"

[browser_clipboard_support_warning]
other = "Sorry, your browser doesn't support copying this example to your clipboard."

[case_study_prefix]
other = "Case study:"

Expand Down Expand Up @@ -158,6 +161,9 @@ other = "Environment variables"
[error_404_were_you_looking_for]
other = "Were you looking for:"

[error_copy_to_clipboard]
other = "Error"

[examples_heading]
other = "Examples"

Expand Down Expand Up @@ -600,6 +606,9 @@ other = "See Also"
[subscribe_button]
other = "Subscribe"

[success_copy_to_clipboard]
other = "Success"

[synopsis_heading]
other = "Synopsis"

Expand All @@ -618,6 +627,12 @@ other = """<p>Items on this page refer to third party products or projects that
[thirdparty_message_vendor]
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>"""

[toast_failure_copycode]
other="Failed to copy to clipboard: "

[toast_success_copycode]
other = "Copied to clipboard: "

[translated_by]
other = "Translated By"

Expand All @@ -639,7 +654,8 @@ other = "Versions"
[warning]
other = "Warning:"

[whatsnext_heading]
other = "What's next"

[warning_copy_to_clipboard]
other = "Warning"

[whatsnext_heading]
other = "What's next"
43 changes: 34 additions & 9 deletions layouts/partials/hooks/body-end.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,54 @@
{{ end }}
{{/* copy-and-paste helper for codenew shortcode */}}
{{- if or (.HasShortcode "code_sample") (.HasShortcode "code") (.HasShortcode "codenew") -}}
{{- $toastrJs := resources.Get "js/toastr-2.1.4.min.js" | minify | fingerprint -}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: we'd typically only minify the code in production builds.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay I'll remove the pipe.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sftim, but js file on the server in already minified, so what should be the next step?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I add a non minified version just for development?

{{- if hugo.IsProduction -}}
{{- $sweetAlert := resources.Get "js/sweetalert-2.1.2.min.js" | minify | fingerprint -}}
<script async src="{{ $sweetAlert.RelPermalink }}" integrity="{{ $sweetAlert.Data.Integrity }}" crossorigin="anonymous"></script>
<script async src="{{ $toastrJs.RelPermalink }}"></script>
{{- else -}}
{{- $sweetAlert := resources.Get "js/sweetalert-2.1.2.min.js" -}}
<script async src="{{ $sweetAlert.RelPermalink }}"></script>
<script async src="{{ $toastrJs.RelPermalink }}"></script>
{{- end -}}

<script type="text/javascript">
function copyCode(elem){
function copyCode(elem) {
if (document.getElementById(elem)) {
if (navigator.clipboard) {
navigator.clipboard.writeText(document.getElementById(elem).textContent).then(
function () {
swal("Copied to clipboard: ",elem);
toastr.options = {
closeButton: true,
progressBar: true,
positionClass: "toast-bottom-center",
timeOut: 2000,
preventDuplicates: true,
newestOnTop: true
};
toastr.success("{{i18n "toast_success_copycode"}}" + elem, "{{i18n "success_copy_to_clipboard"}}");
},
function () {
swal("Oh, no…","Failed to copy to clipboard: ",elem);
},
toastr.options = {
closeButton: true,
progressBar: true,
positionClass: "toast-bottom-center",
timeOut: 2000,
preventDuplicates: true,
newestOnTop: true
};
toastr.error("{{i18n "toast_failure_copycode"}}" + elem, "{{i18n "error_copy_to_clipboard"}}");
}
);
} else {
swal("Oh, no…","Sorry, your browser doesn't support copying this example to your clipboard.");
toastr.options = {
closeButton: true,
progressBar: true,
positionClass: "toast-bottom-center",
timeOut: 2000,
preventDuplicates: true,
newestOnTop: true
};
toastr.warning("{{i18n "browser_clipboard_support_warning"}}", "{{i18n "warning_copy_to_clipboard"}}");
}
}
}
</script>

{{- end -}}
4 changes: 4 additions & 0 deletions layouts/partials/hooks/head-end.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@
{{- $bannerDismissJs := resources.Get "js/banner-dismiss.js" -}}
<script defer src="{{ $bannerDismissJs.RelPermalink }}"></script>
{{- end -}}

{{- if or (.HasShortcode "code_sample") (.HasShortcode "code") (.HasShortcode "codenew") -}}
<link rel="stylesheet" href="/css/toastr-2.1.4.min.css">
{{- end -}}
1 change: 1 addition & 0 deletions static/css/toastr-2.1.4.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.