MCPcopy
hub / github.com/intentui/intentui / useClipboard

Function useClipboard

src/hooks/use-clipboard.ts:3–29  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1import { useEffect, useRef, useState } from 'react'
2
3export function useClipboard() {
4 const [copied, setCopied] = useState(false)
5 const timeoutRef = useRef<number | null>(null)
6
7 const copy = async (value: string) => {
8 try {
9 await navigator.clipboard.writeText(value)
10 setCopied(true)
11 if (timeoutRef.current) window.clearTimeout(timeoutRef.current)
12 timeoutRef.current = window.setTimeout(() => setCopied(false), 2000)
13 return true
14 } catch {
15 if (timeoutRef.current) window.clearTimeout(timeoutRef.current)
16 timeoutRef.current = null
17 setCopied(false)
18 return false
19 }
20 }
21
22 useEffect(() => {
23 return () => {
24 if (timeoutRef.current) window.clearTimeout(timeoutRef.current)
25 }
26 }, [])
27
28 return { copy, copied }
29}

Callers 13

ColorItemFunction · 0.90
OpenInFunction · 0.90
SnippetTabPanelFunction · 0.90
PullRegistryFunction · 0.90
CopyButtonFunction · 0.90
CodeBlockFunction · 0.90
EditorTextFunction · 0.90
PlainCodeFunction · 0.90
ColorGeneratorFunction · 0.90
ColorItemFunction · 0.90
RegistryItemViewerFunction · 0.90
IconListItemFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected