-
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.
Set callback function defaults to no-op
- Loading branch information
1 parent
b99e7f3
commit 4e19967
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; |