useClipboard
useClipboard
is a custom hook that performs the operation of copying a value to the clipboard.
Import
import { useClipboard } from "@yamada-ui/react"
Usage
Editable example
const { onCopy, value, setValue, hasCopied } = useClipboard() return ( <> <HStack gap="md"> <Input placeholder="text to be copied..." value={value} onChange={(e) => setValue(e.target.value)} /> <Button onClick={onCopy}>{hasCopied ? "Copied!" : "Copy"}</Button> </HStack> <Editable placeholder="Paste here" mt="md"> <EditablePreview width="full" /> <EditableInput /> </Editable> </> )
Using Initial Values
Editable example
const { onCopy, value, setValue, hasCopied } = useClipboard("initial value") return ( <> <HStack gap="md"> <Input placeholder="text to be copied..." value={value} onChange={(e) => setValue(e.target.value)} /> <Button onClick={onCopy}>{hasCopied ? "Copied!" : "Copy"}</Button> </HStack> <Editable placeholder="Paste here" mt="md"> <EditablePreview width="full" /> <EditableInput /> </Editable> </> )
Controlling Timeout
Editable example
const { onCopy, value, setValue, hasCopied } = useClipboard("", 5000) return ( <> <HStack gap="md"> <Input placeholder="text to be copied..." value={value} onChange={(e) => setValue(e.target.value)} /> <Button onClick={onCopy}>{hasCopied ? "Copied!" : "Copy"}</Button> </HStack> <Editable placeholder="Paste here" mt="md"> <EditablePreview width="full" /> <EditableInput /> </Editable> </> )
Copy the Specified Value
Editable example
const { onCopy, hasCopied } = useClipboard() const value = "Goku" return ( <HStack gap="md"> <Input value={value} isReadOnly /> <Button onClick={() => onCopy(value)}> {hasCopied ? "Copied!" : "Copy"} </Button> </HStack> )
Edit this page on GitHub