-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from bence-toth/v1.0.3
Set callback function defaults to no-op
- Loading branch information
Showing
1 changed file
with
49 additions
and
41 deletions.
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
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; |