useCopyToClipboard/useCopyToClipboard.ts

usehooks-ts · packages/usehooks-ts/src/useCopyToClipboard/useCopyToClipboard.ts · typescript · MIT
ui

React hook to copy text to clipboard and track success state.

// Source: https://github.com/juliencrn/usehooks-ts | License: MIT | Copyright 2020 Julien CARON
// Provided via justclaw.it.com; keep this notice with the code.
import { useCallback, useState } from 'react'

/**
 * The copied text as `string` or `null` if nothing has been copied yet.
 */
type CopiedValue = string | null

/**
 * Function to copy text to the clipboard.
 * @param text - The text to copy to the clipboard.
 * @returns {Promise<boolean>} A promise that resolves to `true` if the text was copied successfully, or `false` otherwise.
 */
type CopyFn = (text: string) => Promise<boolean>

/**
 * Custom hook that copies text to the clipboard using the [`Clipboard API`](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API).
 * @returns {[CopiedValue, CopyFn]} An tuple containi

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key