Skip to content

Commit

Permalink
fix copy button css
Browse files Browse the repository at this point in the history
  • Loading branch information
Amy Chen authored and Amy Chen committed Apr 30, 2024
1 parent 6025cd6 commit 8a554cf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/components/CopyButton.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
addButtonErrorStateTransition,
addButtonSuccessStateTransition,
allowCopyClipboardItem,
copyDomToClipboard,
Expand Down Expand Up @@ -47,8 +48,11 @@ export default class CopyButton extends Component {
}
if (!this.props.disableFrame && this.props.elementToCopy)
this.props.elementToCopy.classList.remove("framed-border");
if (!error)
addButtonSuccessStateTransition(this.copyButtonRef.current);
if (error) {
addButtonErrorStateTransition(this.copyButtonRef.current);
return;
}
addButtonSuccessStateTransition(this.copyButtonRef.current);
}
);
},
Expand All @@ -67,6 +71,7 @@ export default class CopyButton extends Component {
);
},
imageType: "image/png",
backgroundColor: "#FFF",
...this.props.options?this.props.options:{}
}
);
Expand All @@ -80,7 +85,7 @@ export default class CopyButton extends Component {
<button
ref={this.copyButtonRef}
onClick={this.handleCopy}
className={`button-default button-secondary rounded print-hidden exclude-from-copy ${
className={`copy button-default button-secondary rounded print-hidden exclude-from-copy ${
this.state.copyInProgress ? "button--loading" : ""
}`}
title={buttonTitle}
Expand Down
13 changes: 12 additions & 1 deletion src/helpers/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export function copyDomToClipboard(domElement, options) {
}
})
.catch((e) => {
alert("Error! Unable to copy content to clipboard! " + e);
//alert("Error! Unable to copy content to clipboard! " + e);
console.log(e);
console.log("passed param ", params);
if (params.afterCopy) {
Expand Down Expand Up @@ -472,3 +472,14 @@ export function addButtonSuccessStateTransition(buttonRef, transitionDuration) {
transitionDuration || 1000
);
}

export function addButtonErrorStateTransition(buttonRef, transitionDuration) {
if (!buttonRef) return;
buttonRef.classList.add("button--loaded");
buttonRef.classList.add("error");
clearTimeout(buttonTransitionId);
buttonTransitionId = setTimeout(() => {
buttonRef.classList.remove("button--loaded");
buttonRef.classList.remove("error");
}, transitionDuration || 1000);
}
30 changes: 30 additions & 0 deletions src/styles/elements/_button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,36 @@ button.disabled {
transform: rotate(45deg);
}

.button--loaded.copy:after {
content: "copied!";
position: absolute;
top: 0;
left: -36px;
right: 0;
bottom: 0;
margin: auto;
width: 8px;
height: 20px;
border: 0;
color: green;
transform: rotate(0);
}

.button--loaded.error:after {
content: "error!";
position: absolute;
top: 2px;
left: -36px;
right: 0;
bottom: 0;
margin: auto;
width: 8px;
height: 20px;
border: 0;
color: $color-alert;
transform: rotate(0);
}

@keyframes button-loading-spinner {
from {
transform: rotate(0turn);
Expand Down

0 comments on commit 8a554cf

Please sign in to comment.