Documentation
useClipboard

useClipboard

The useClipboard hook enables you to copy text to the clipboard programmatically. It also provides error handling to notify you if the copy operation fails. This hook is useful in scenarios where you need to provide a copy-to-clipboard functionality, such as copying text from an input field or copying a URL.

Usage

First, you need to import the useClipboard hook from the kitchn package.

import { useClipboard } from "kitchn";

Example

Here is an example of how to use the useClipboard hook in a component:

Parameters

The useClipboard hook accepts the following parameter:

NameTypeDescription
optionsUseClipboardOptionsConfiguration options for the clipboard operation.

UseClipboardOptions

The UseClipboardOptions object can include the following properties:

NameTypeDescription
onError() => unknownA callback function that gets executed if the copy operation fails.

Return Value

The useClipboard hook returns an object with the following property:

NameTypeDescription
copy(text: string) => voidA function that copies the provided text to the clipboard.