Hone logo
Hone
Problems

React useClipboard Hook

This challenge asks you to create a custom React hook, useClipboard, that provides functionality for copying text to the user's clipboard. This hook will simplify the process of copying data in your React applications, providing a clean and reusable solution for common clipboard interactions. It's a useful tool for features like sharing links, copying code snippets, or providing quick access to information.

Problem Description

You need to implement a useClipboard hook in TypeScript that manages the copying of text to the user's clipboard. The hook should provide the following functionality:

  • text: A string representing the text to be copied. This is an input to the hook.
  • copy: A function that, when called, attempts to copy the current text value to the clipboard.
  • isCopied: A boolean state variable indicating whether the copy operation was successful.
  • error: A string containing an error message if the copy operation fails. This should be null if no error occurred.

The hook should handle potential errors during the copy operation (e.g., browser API limitations, permissions issues) and update the error state accordingly. It should also update the isCopied state to true upon successful copy and reset it to false initially and after a successful copy.

Key Requirements:

  • The hook must be written in TypeScript.
  • The hook must use the useState hook for managing state.
  • The hook must use the navigator.clipboard.writeText() method to copy the text to the clipboard.
  • The hook must handle potential errors during the copy operation.
  • The hook must return an object containing the text, copy, isCopied, and error values.

Expected Behavior:

  1. Initially, isCopied should be false and error should be null.
  2. When the copy function is called:
    • If the copy operation is successful, isCopied should be set to true and error should be set to null.
    • If the copy operation fails, isCopied should remain false and error should be set to an appropriate error message (e.g., "Failed to copy text to clipboard.").
  3. The text value should be used as the content to be copied.
  4. After a successful copy, isCopied should automatically reset to false after a short delay (e.g., 1 second). This provides visual feedback to the user.

Edge Cases to Consider:

  • The user's browser might not support the navigator.clipboard API.
  • The user might deny permission to access the clipboard.
  • The text to be copied might be very long, potentially causing issues. (While not a primary concern, consider it for robustness).

Examples

Example 1:

Input: Initial text: "Hello, world!", subsequent copy call.
Output: isCopied: true, error: null (after successful copy and 1 second delay, isCopied becomes false)
Explanation: The hook successfully copies "Hello, world!" to the clipboard and sets isCopied to true. After 1 second, isCopied is reset to false.

Example 2:

Input: Text: "This is a long string...", copy call, browser does not support navigator.clipboard.
Output: isCopied: false, error: "Clipboard API not supported."
Explanation: The hook detects that the browser does not support the clipboard API and sets the error accordingly.

Example 3:

Input: Text: "Some text", copy call, user denies clipboard permission.
Output: isCopied: false, error: "Failed to copy text to clipboard."
Explanation: The hook detects that the copy operation failed due to user permission and sets the error accordingly.

Constraints

  • The hook should be implemented using functional components and hooks.
  • The text input should be a string.
  • The delay for resetting isCopied after a successful copy should be configurable (default to 1 second).
  • The hook should not introduce any unnecessary dependencies.
  • The error message should be user-friendly and informative.

Notes

  • Consider using try...catch blocks to handle potential errors during the copy operation.
  • You can use setTimeout to implement the delay for resetting isCopied.
  • Think about how to provide a fallback mechanism if the navigator.clipboard API is not available. A simple check at the beginning of the hook can suffice.
  • Focus on creating a clean, reusable, and well-documented hook. Good error handling is crucial.
Loading editor...
typescript