-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 make useCopyToClipboard hook interface more idiomatic
- Loading branch information
Showing
4 changed files
with
45 additions
and
76 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 |
---|---|---|
@@ -1,54 +1,35 @@ | ||
# `useCopyToClipboard` | ||
|
||
copy text to a users clipboard. | ||
Copy text to a user's clipboard. | ||
|
||
## Usage | ||
|
||
### basic usage: | ||
|
||
```jsx | ||
import {useCopyToClipboard} from 'react-use'; | ||
|
||
const myComp = (props) => { | ||
const [success, copyToClipboard] = useCopyToClipboard(); | ||
const myText = 'text to be copied'; | ||
return ( | ||
<span onClick={ () => copyToClipboard(myText) }>{myText}</span> | ||
) | ||
} | ||
``` | ||
## Usage | ||
|
||
### with timeout: | ||
Basic usage | ||
|
||
```jsx | ||
import {useCopyToClipboard} from 'react-use'; | ||
const Demo = () => { | ||
const [text, setText] = React.useState(''); | ||
const [copied, copyToClipboard] = useCopyToClipboard(text); | ||
|
||
const myComp = (props) => { | ||
const [success, copyToClipboard] = useCopyToClipboard(2000); | ||
const myText = 'text to be copied'; | ||
return ( | ||
<div> | ||
<span onClick={ () => copyToClipboard(myText) }>{myText}</span> | ||
{ success && <span>copied to clipboard</span>} | ||
<input value={text} onChange={e => setText(e.target.value)} /> | ||
<button type="button" onClick={copyToClipboard}>copy text</button> | ||
<div>Copied: {copied ? 'Yes' : 'No'}</div> | ||
</div> | ||
) | ||
) | ||
} | ||
``` | ||
|
||
### with custom polyfill: | ||
## Reference | ||
|
||
```jsx | ||
import {useCopyToClipboard} from 'react-use'; | ||
import * as clipboard from "clipboard-polyfill" | ||
|
||
const myComp = (props) => { | ||
const [success, copyToClipboard] = useCopyToClipboard(undefined, clipboard.writeText); | ||
const myText = 'text to be copied'; | ||
return ( | ||
<div> | ||
<span onClick={ () => copyToClipboard(myText) }>{myText}</span> | ||
{ success && <span>copied to clipboard</span>} | ||
</div> | ||
) | ||
} | ||
```js | ||
const [copied, copyToClipboard] = useCopyToClipboard(text); | ||
const [copied, copyToClipboard] = useCopyToClipboard(text, copyFunction); | ||
``` | ||
|
||
, where | ||
|
||
- `copyFunction` — function that receives a single string argument, which | ||
it copies to user's clipboard. |
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 |
---|---|---|
@@ -1,41 +1,27 @@ | ||
import {useState, useEffect} from 'react'; | ||
import * as clipboard from "clipboard-polyfill" | ||
import useUpdateEffect from './useUpdateEffect'; | ||
import {useState, useCallback} from 'react'; | ||
|
||
const copyDefault = (text) => { | ||
console.log('run'); | ||
const element = document.createElement('textarea'); // create textarea HTML element | ||
element.value = text; // add the text to be copied to the element | ||
document.body.appendChild(element); // add element to DOM | ||
element.select(); // select the text | ||
document.execCommand('copy'); // execute copy command | ||
document.body.removeChild(element); // remove element from DOM | ||
const element = document.createElement('textarea'); | ||
element.value = text; | ||
document.body.appendChild(element); | ||
element.select(); | ||
document.execCommand('copy'); | ||
document.body.removeChild(element); | ||
}; | ||
|
||
const useCopyToClipboard = (text: string = '', copy = copyDefault): [boolean, () => void] => { | ||
const [copied, setCopied] = useState(false); | ||
const copyToClipboard = useCallback(() => { | ||
copy(text); | ||
setCopied(true); | ||
}, [text]); | ||
|
||
const useCopyToClipboard = (timeout = undefined, copy = copyDefault) => { | ||
useUpdateEffect(() => { | ||
setCopied(false); | ||
}, [text]); | ||
|
||
const [success, setSuccess] = useState(false); | ||
|
||
const copyToClipboard = (text) => { | ||
|
||
if (typeof text == "string" || typeof text == "number" ) { | ||
copy(text); | ||
setSuccess(true); | ||
} | ||
else { | ||
setSuccess(false); | ||
console.error(`Cannot copy typeof ${typeof text} to clipboard, must be a valid string or number.`); | ||
} | ||
} | ||
|
||
useEffect( () => { | ||
// if timeout given, set success to false | ||
if (timeout) { | ||
if (success) setTimeout( () => setSuccess(false), timeout); | ||
} | ||
}, [success]) | ||
|
||
return [success, copyToClipboard]; | ||
return [copied, copyToClipboard]; | ||
} | ||
|
||
export default useCopyToClipboard; |