useCopyToClipboard

Custom hook that copies text to the clipboard using the [`Clipboard API`](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API).

Custom hook that copies text to the clipboard using the Clipboard API.

Usage

import { useCopyToClipboard } from "@tilt-legal/cubitt-components/utilities/hooks";

const [copiedText, copyToClipboard] = useCopyToClipboard();
const textToCopy = 'Hello, world!';

// Attempt to copy text to the clipboard
copyToClipboard(textToCopy)
  .then(success => {
    if (success) {
      console.log(`Text "${textToCopy}" copied to clipboard successfully.`);
    } else {
      console.error('Failed to copy text to clipboard.');
    }
  });

API

Signature

declare function useCopyToClipboard(): [CopiedValue, CopyFn];

Parameters

This hook takes no arguments.

Return Value

ValueTypeDescription
return[CopiedValue, CopyFn]An tuple containing the copied text and a function to copy text to the clipboard.

On this page