-
-
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.
- Loading branch information
Showing
7 changed files
with
129 additions
and
1 deletion.
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,35 @@ | ||
# `useCopyToClipboard` | ||
|
||
Copy text to a user's clipboard. | ||
|
||
|
||
## Usage | ||
|
||
Basic usage | ||
|
||
```jsx | ||
const Demo = () => { | ||
const [text, setText] = React.useState(''); | ||
const [copied, copyToClipboard] = useCopyToClipboard(text); | ||
|
||
return ( | ||
<div> | ||
<input value={text} onChange={e => setText(e.target.value)} /> | ||
<button type="button" onClick={copyToClipboard}>copy text</button> | ||
<div>Copied: {copied ? 'Yes' : 'No'}</div> | ||
</div> | ||
) | ||
} | ||
``` | ||
|
||
## Reference | ||
|
||
```js | ||
const [copied, copyToClipboard] = useCopyToClipboard(text); | ||
const [copied, copyToClipboard] = useCopyToClipboard(text, writeText); | ||
``` | ||
|
||
, where | ||
|
||
- `writeText` — 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import ShowDocs from './util/ShowDocs'; | ||
import {useCopyToClipboard} from '..'; | ||
|
||
const Demo = () => { | ||
const [text, setText] = React.useState(''); | ||
const [copied, copyToClipboard] = useCopyToClipboard(text, { | ||
onCopy: txt => alert('success: ' + txt), | ||
onError: err => alert(err), | ||
}); | ||
|
||
return ( | ||
<div> | ||
<input value={text} onChange={e => setText(e.target.value)} /> | ||
<button type="button" onClick={copyToClipboard}>copy text</button> | ||
<div>Copied: {copied ? 'Yes' : 'No'}</div> | ||
<div style={{margin: 10}}> | ||
<input type="text" placeholder="now paste it in here"/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
storiesOf('Side-effects|useCopyToClipboard', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useCopyToClipboard.md')} />) | ||
.add('Demo', () => <Demo/>) |
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,55 @@ | ||
import {useState, useCallback, useRef} from 'react'; | ||
import useUpdateEffect from './useUpdateEffect'; | ||
import useRefMounted from './useRefMounted'; | ||
const writeTextDefault = require('copy-to-clipboard'); | ||
|
||
export type WriteText = (text: string) => Promise<void>; // https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText | ||
export interface UseCopyToClipboardOptions { | ||
writeText?: WriteText; | ||
onCopy?: (text: string) => void; | ||
onError?: (error: any, text: string) => void; | ||
} | ||
export type UseCopyToClipboard = (text?: string, options?: UseCopyToClipboardOptions) => [boolean, () => void]; | ||
|
||
const useCopyToClipboard: UseCopyToClipboard = (text = '', options) => { | ||
const {writeText = writeTextDefault, onCopy, onError} = (options || {}) as UseCopyToClipboardOptions; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
if (typeof text !== 'string') { | ||
console.warn('useCopyToClipboard hook expects first argument to be string.'); | ||
} | ||
} | ||
|
||
const mounted = useRefMounted(); | ||
const latestText = useRef(text); | ||
const [copied, setCopied] = useState(false); | ||
const copyToClipboard = useCallback(async () => { | ||
if (latestText.current !== text) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn('Trying to copy stale text.'); | ||
} | ||
return; | ||
} | ||
|
||
try { | ||
await writeText(text); | ||
if (!mounted.current) return; | ||
setCopied(true); | ||
onCopy && onCopy(text); | ||
} catch (error) { | ||
if (!mounted.current) return; | ||
console.error(error); | ||
setCopied(false); | ||
onError && onError(error, text); | ||
} | ||
}, [text]); | ||
|
||
useUpdateEffect(() => { | ||
setCopied(false); | ||
latestText.current = text; | ||
}, [text]); | ||
|
||
return [copied, copyToClipboard]; | ||
} | ||
|
||
export default useCopyToClipboard; |
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