diff --git a/src/index.js b/src/index.js index 1ca3118..d0eac49 100644 --- a/src/index.js +++ b/src/index.js @@ -1,54 +1,62 @@ -import {useEffect, useState} from 'react' +import { useEffect, useState } from "react"; -const useClipboard = ({updateFrequency = 1000} = {}, onReadError) => { - const [clipboard, setClipboardContent] = useState('') +const useClipboard = ( + { updateFrequency = 1000 } = {}, + onReadError = () => {} +) => { + const [clipboard, setClipboardContent] = useState(""); - const copyToClipboard = (clipboardContent, onCopyToClipboard, onWriteError) => { + const copyToClipboard = ( + clipboardContent, + onCopyToClipboard = () => {}, + onWriteError = () => {} + ) => { if (navigator.permissions && navigator.clipboard) { - navigator.permissions.query({name: 'clipboard-write'}).then(({state}) => { - if (['granted', 'prompt'].includes(state)) { - navigator.clipboard.writeText(clipboardContent).then( - () => { - setClipboardContent(clipboardContent) - onCopyToClipboard(clipboardContent) - }, - onWriteError || (() => {}) - ) - } - else { - onWriteError({message: 'ClipboardWrite permission has been blocked as the user.'}) - } - }) + navigator.permissions + .query({ name: "clipboard-write" }) + .then(({ state }) => { + if (["granted", "prompt"].includes(state)) { + navigator.clipboard.writeText(clipboardContent).then(() => { + setClipboardContent(clipboardContent); + onCopyToClipboard(clipboardContent); + }, onWriteError); + } else { + onWriteError({ + message: + "ClipboardWrite permission has been blocked as the user.", + }); + } + }); } - } + }; useEffect(() => { - let readClipboardIntervalId + let readClipboardIntervalId; if (navigator.permissions && navigator.clipboard) { - navigator.permissions.query({name: 'clipboard-read'}).then(({state}) => { - if (['granted', 'prompt'].includes(state)) { - readClipboardIntervalId = setInterval(() => { - navigator.clipboard.readText().then( - clipboardContent => { - setClipboardContent(clipboardContent) - }, - onReadError || (() => {}) - ) - }, updateFrequency) - } - else { - onReadError({message: 'ClipboardRead permission has been blocked as the user.'}) - } - }) + navigator.permissions + .query({ name: "clipboard-read" }) + .then(({ state }) => { + if (["granted", "prompt"].includes(state)) { + readClipboardIntervalId = setInterval(() => { + navigator.clipboard.readText().then((clipboardContent) => { + setClipboardContent(clipboardContent); + }, onReadError); + }, updateFrequency); + } else { + onReadError({ + message: "ClipboardRead permission has been blocked as the user.", + }); + } + }); } return () => { if (navigator.clipboard) { - clearInterval(readClipboardIntervalId) + clearInterval(readClipboardIntervalId); } - } - }, []) + }; + }, []); - return [clipboard, copyToClipboard] -} + return [clipboard, copyToClipboard]; +}; -export default useClipboard +export default useClipboard;