useClipboard
useClipboard is a custom hook that handles copying content to clipboard.
Arguments#
The useClipboard hook takes the following arguments:
| Name | Type | Required | Description | 
|---|---|---|---|
| text | string | true | The text or value that is to be copied. | 
| optionsOrTimeout | numberorobject | false | The timeout as a numberor anobjectcontaining 2 properties:timeoutandformatfor the MIME type. The timeout is measured in milliseconds and has a default of 1500ms. | 
Return value#
The useClipboard hook returns an object with the following fields:
| Name | Type | Default | Description | 
|---|---|---|---|
| value | string | The copied value. | |
| setValue | function | State action to change the copied value. | |
| onCopy | function | Callback function to copy content. | |
| hasCopied | boolean | false | If true, the content has been copied within the lasttimeoutmilliseconds. That is, it is set to true right afteronCopyis called, andfalseaftertimeouthas passed. | 
Import#
import { useClipboard } from '@chakra-ui/react'
Usage#
function Example() {const placeholder = "text to be copied...";const { onCopy, value, setValue, hasCopied } = useClipboard("");return (<><Flex mb={2}><Inputplaceholder={placeholder}value={value}onChange={(e) => {setValue(e.target.value);}}mr={2}/><Button onClick={onCopy}>{hasCopied ? "Copied!" : "Copy"}</Button></Flex><Editable placeholder="Paste here"><EditablePreview width="100%" /><EditableInput /></Editable></>)}