🔍 Copy Text Button
Make copying text effortless with the Copy Button, a simple yet powerful React component designed for quick text duplication. Ideal for sharing links, copying tags, or grabbing reusable content, this button enhances user experience with instant feedback—whether it's a tooltip, icon change, or subtle animation.
Preview
Click the button below to copy the text
Follow below steps 👇🏻
Install dependencies
1npm i lucide-react
Component
Create a file copy-text-button.tsx in your components folder and paste this code
1'use client';23import React, { useState } from 'react';4import { Check, Copy } from 'lucide-react';56const CopyTextButton = ({7handle, // * Text you want the user to copy8icon = <Copy className="h-5 w-5"/>, // * Icon to show on the button9variant = 'default',10onCopy11}: {12handle: string;13icon?: React.ReactNode;14variant?: 'default' | 'small';15onCopy?: () => void;16}) => {17const [copied, setCopied] = useState(false);18const handleCopy = () => {19if (onCopy) onCopy();20else navigator.clipboard.writeText(handle);21setCopied(true);22setTimeout(() => setCopied(false), 1200);23};2425return (26<button27className={`bg-gray-800 w-min hover:bg-gray-700 text-white px-4 py-2 rounded-lg flex items-center gap-2 transition ${28variant === 'small' && 'text-xs'29}`}30onClick={handleCopy}31>32{copied ? (33<Check className={`${variant === 'small' ? 'w-4 h-4' : 'w-5 h-5'} text-green-400`} />34) : (35icon36)}37<span className="whitespace-nowrap">{handle}</span>38</button>39);40};4142export default CopyTextButton;
Usage
1<CopyTextButton handle={'Stackbits is awesome'} icon={<Rocket className="h-5 w-5" />} />
⭐️ Got a question or feedback?
Feel free to reach out!