From 25532254701cbf36e65e91db44b27db6111d8bd8 Mon Sep 17 00:00:00 2001 From: streamich Date: Mon, 8 Apr 2019 00:42:07 +0200 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20add=20onCopy=20and=20onE?= =?UTF-8?q?rror=20events=20and=20use=20NPM=20copy=20library?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/__stories__/useCopyToClipboard.story.tsx | 5 +++- src/useCopyToClipboard.ts | 28 +++++++++----------- yarn.lock | 9 ++++++- 4 files changed, 26 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 81df383b10..993b6eb357 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ }, "homepage": "https://github.com/streamich/react-use#readme", "dependencies": { + "copy-to-clipboard": "^3.1.0", "nano-css": "^5.1.0", "react-wait": "^0.3.0", "screenfull": "^4.1.0", diff --git a/src/__stories__/useCopyToClipboard.story.tsx b/src/__stories__/useCopyToClipboard.story.tsx index fa346a017f..7fcd5a7f3e 100644 --- a/src/__stories__/useCopyToClipboard.story.tsx +++ b/src/__stories__/useCopyToClipboard.story.tsx @@ -5,7 +5,10 @@ import {useCopyToClipboard} from '..'; const Demo = () => { const [text, setText] = React.useState(''); - const [copied, copyToClipboard] = useCopyToClipboard(text); + const [copied, copyToClipboard] = useCopyToClipboard(text, { + onCopy: txt => alert('success: ' + txt), + onError: err => alert(err), + }); return (
diff --git a/src/useCopyToClipboard.ts b/src/useCopyToClipboard.ts index 62024190c6..244bcc1bfb 100644 --- a/src/useCopyToClipboard.ts +++ b/src/useCopyToClipboard.ts @@ -1,23 +1,19 @@ +import {useState, useCallback, useRef} from 'react'; import useUpdateEffect from './useUpdateEffect'; import useRefMounted from './useRefMounted'; -import {useState, useCallback, useRef} from 'react'; +const writeTextDefault = require('copy-to-clipboard'); export type WriteText = (text: string) => Promise; // https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText -export type UseCopyToClipboard = (text?: string, writeText?: WriteText) => [boolean, () => void]; - -const writeTextDefault = async (text) => { - const element = document.createElement('textarea'); - element.value = text; - document.body.appendChild(element); - try { - element.select(); - document.execCommand('copy'); - } finally { - document.body.removeChild(element); - } -}; +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; -const useCopyToClipboard: UseCopyToClipboard = (text = '', writeText = writeTextDefault) => { if (process.env.NODE_ENV !== 'production') { if (typeof text !== 'string') { console.warn('useCopyToClipboard hook expects first argument to be string.'); @@ -39,10 +35,12 @@ const useCopyToClipboard: UseCopyToClipboard = (text = '', writeText = writeText 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]); diff --git a/yarn.lock b/yarn.lock index 2025b08bdb..41d861ec33 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3932,6 +3932,13 @@ copy-to-clipboard@^3.0.8: dependencies: toggle-selection "^1.0.3" +copy-to-clipboard@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/copy-to-clipboard/-/copy-to-clipboard-3.1.0.tgz#0a28141899e6bd217b9dc13fd1689b3b38820b44" + integrity sha512-+RNyDq266tv5aGhfRsL6lxgj8Y6sCvTrVJnFUVvuxuqkcSMaLISt1wd4JkdQSphbcLTIQ9kEpTULNnoCXAFdng== + dependencies: + toggle-selection "^1.0.6" + core-js-compat@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.0.0.tgz#cd9810b8000742535a4a43773866185e310bd4f7" @@ -11010,7 +11017,7 @@ to-regex@^3.0.1, to-regex@^3.0.2: regex-not "^1.0.2" safe-regex "^1.1.0" -toggle-selection@^1.0.3: +toggle-selection@^1.0.3, toggle-selection@^1.0.6: version "1.0.6" resolved "https://registry.yarnpkg.com/toggle-selection/-/toggle-selection-1.0.6.tgz#6e45b1263f2017fa0acc7d89d78b15b8bf77da32" integrity sha1-bkWxJj8gF/oKzH2J14sVuL932jI=