copy-to-clipboard.tsx


Overview

copy-to-clipboard.tsx is a React functional component that provides a user interface element for copying a given text string to the system clipboard. It visually indicates the copy status by toggling between a copy icon and a confirmation checkmark, along with a tooltip that shows either "Copy" or "Copied" based on the action state.

This component is designed to enhance UX by giving instant feedback when a user copies text, commonly used in applications for sharing codes, links, or snippets quickly.


Component: CopyToClipboard

Description

CopyToClipboard is a React component that wraps the react-copy-to-clipboard library's CopyToClipboard component (aliased here as Clipboard) with additional UI feedback and internationalized tooltips.

Props

Internal State

External Hooks and Dependencies

Methods

handleCopy() : void

Render Output

The component renders a Tooltip wrapping the Clipboard component:

Usage Example

import CopyToClipboard from './copy-to-clipboard';

function Demo() {
  return (
    <div>
      <p>Copy this text:</p>
      <CopyToClipboard text="Hello, world!" />
    </div>
  );
}

Implementation Details


Interaction with Other Parts of the System


Mermaid Diagram: Component Interaction

componentDiagram
    component CopyToClipboard {
        +Props: { text: string }
        +State: copied: boolean
        +handleCopy(): void
    }

    component Clipboard {
        +Props: { text: string, onCopy: () => void }
    }

    component Tooltip {
        +Props: { title: string }
    }

    CopyToClipboard --> Clipboard : renders with onCopy={handleCopy}
    CopyToClipboard --> Tooltip : wraps Clipboard with dynamic title
    CopyToClipboard --> useTranslate : calls for localization
    Clipboard --> System Clipboard : copies text on click
    Tooltip --> User : shows tooltip on hover

Summary

copy-to-clipboard.tsx is a small but highly reusable React component that enhances user experience by providing intuitive clipboard copy functionality with visual and textual feedback. It integrates with internationalization and UI systems, making it a robust choice for any React/Ant Design-based application requiring copy-to-clipboard features.