MCPcopy Create free account
hub / github.com/github/docs / useCopyClipboard

Function useCopyClipboard

components/hooks/useClipboard.ts:11–41  ·  view source on GitHub ↗
(
  text: string,
  options?: IOptions
)

Source from the content-addressed store, hash-verified

9}
10
11export default function useCopyClipboard(
12 text: string,
13 options?: IOptions
14): [boolean, () => Promise<void>] {
15 const [isCopied, setIsCopied] = useState(false)
16 const successDuration = options && options.successDuration
17
18 useEffect(() => {
19 if (isCopied && successDuration) {
20 const id = setTimeout(() => {
21 setIsCopied(false)
22 }, successDuration)
23
24 return () => {
25 clearTimeout(id)
26 }
27 }
28 }, [isCopied, successDuration])
29
30 return [
31 isCopied,
32 async () => {
33 try {
34 await navigator.clipboard.writeText(text)
35 setIsCopied(true)
36 } catch {
37 setIsCopied(false)
38 }
39 },
40 ]
41}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected