Skip to content

Commit

Permalink
Merge pull request #20 from bence-toth/v1.0.3
Browse files Browse the repository at this point in the history
Set callback function defaults to no-op
  • Loading branch information
bence-toth authored Jan 29, 2022
2 parents b99e7f3 + 4e19967 commit 61cb403
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 61cb403

Please sign in to comment.