Skip to content

Commit

Permalink
Set callback function defaults to no-op
Browse files Browse the repository at this point in the history
  • Loading branch information
bence-toth committed Jan 29, 2022
1 parent b99e7f3 commit 4e19967
Showing 1 changed file with 49 additions and 41 deletions.
90 changes: 49 additions & 41 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 4e19967

Please sign in to comment.