-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6cdf2bf
commit 7aad2ec
Showing
5 changed files
with
66 additions
and
10 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from "react"; | ||
|
||
export default function CopyButton({ source }) { | ||
|
||
let [shaking, setShaking] = React.useState(false); | ||
|
||
function copyTextToClipboard() { | ||
var textArea = document.createElement("textarea"); | ||
// Place in the top-left corner of screen regardless of scroll position. | ||
textArea.style.position = "fixed"; | ||
|
||
textArea.value = source; | ||
|
||
document.body.appendChild(textArea); | ||
textArea.focus(); | ||
textArea.select(); | ||
|
||
var successful; | ||
try { | ||
successful = document.execCommand("copy"); | ||
} catch (err) { | ||
console.log("Oops, unable to copy"); | ||
} | ||
document.body.removeChild(textArea); | ||
|
||
if (successful) { | ||
// show user that copying happened - update text on element (e.g. button) | ||
setShaking(true); | ||
setTimeout(() => { | ||
// reset after 1 second | ||
setShaking(false); | ||
}, 1000); | ||
} else { | ||
console.log("Copying failed"); | ||
} | ||
} | ||
|
||
let buttonStyle = { | ||
background: "transparent", | ||
border: "none", | ||
padding: "0 2px" | ||
}; | ||
if (shaking) { | ||
// NB: seesaw is defined in global App.css | ||
buttonStyle["animation"] = "0.1s linear 0s infinite alternate seesaw"; | ||
} | ||
|
||
return ( | ||
<button title="Copy" style={buttonStyle} onClick={copyTextToClipboard}> | ||
<img src="/copy_icon.png" /> | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters