-
Notifications
You must be signed in to change notification settings - Fork 565
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a Clipboard Button component (#1743)
The inline button component to copy the publish url was lacking an indication that the text was copied. Additionally, the current setup was using a copy method that can have an issue in some browsers. This commit switches to use of the clipboard npm repo, moves code into a component, and adds an indication that the text was copied.
- Loading branch information
Showing
6 changed files
with
93 additions
and
30 deletions.
There are no files selected for viewing
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, { useEffect, useRef, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ReactDom from 'react-dom'; | ||
import Clipboard from 'clipboard'; | ||
|
||
function ClipboardButton({ text }) { | ||
const buttonRef = useRef(); | ||
const textCallback = useRef(); | ||
const successCallback = useRef(); | ||
|
||
const onCopy = () => { | ||
setCopied(true); | ||
}; | ||
|
||
const [isCopied, setCopied] = useState(false); | ||
|
||
// toggle the `isCopied` flag back to `false` after 4 seconds | ||
useEffect(() => { | ||
if (isCopied) { | ||
const confirmationTimeout = setTimeout(() => setCopied(false), 4000); | ||
return () => clearTimeout(confirmationTimeout); | ||
} | ||
}, [isCopied]); | ||
|
||
// update the callbacks on rerenders that change `text` or `onCopy` | ||
useEffect(() => { | ||
textCallback.current = () => text; | ||
successCallback.current = onCopy; | ||
}, [text, onCopy]); | ||
|
||
// create the `Clipboard` object on mount and destroy on unmount | ||
useEffect(() => { | ||
const clipboard = new Clipboard(buttonRef.current, { | ||
text: () => textCallback.current(), | ||
}); | ||
clipboard.on('success', () => successCallback.current()); | ||
|
||
return () => clipboard.destroy(); | ||
}, []); | ||
|
||
return ( | ||
<button ref={buttonRef} type="button" className="button button-borderless"> | ||
{isCopied ? 'Copied!' : 'Copy'} | ||
</button> | ||
); | ||
} | ||
|
||
ClipboardButton.propTypes = { | ||
disbaled: PropTypes.bool, | ||
text: PropTypes.string, | ||
}; | ||
|
||
export default ClipboardButton; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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