<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Samitha Widanage</title>
    <description>The latest articles on DEV Community by Samitha Widanage (@samhansaka).</description>
    <link>https://dev.to/samhansaka</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3669867%2Fc55b5cc8-890a-4b27-8ce8-e6bd277a3300.jpg</url>
      <title>DEV Community: Samitha Widanage</title>
      <link>https://dev.to/samhansaka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/samhansaka"/>
    <language>en</language>
    <item>
      <title>The Right Way to Copy to Clipboard in React (2024)</title>
      <dc:creator>Samitha Widanage</dc:creator>
      <pubDate>Fri, 19 Dec 2025 03:46:59 +0000</pubDate>
      <link>https://dev.to/samhansaka/the-right-way-to-copy-to-clipboard-in-react-2024-2m7i</link>
      <guid>https://dev.to/samhansaka/the-right-way-to-copy-to-clipboard-in-react-2024-2m7i</guid>
      <description>&lt;p&gt;Copying to clipboard seems simple, right? Click a button, text goes to clipboard. Done.                                                               &lt;/p&gt;

&lt;p&gt;But if you've ever tried implementing this in React, you know it's not that straightforward. I built &lt;a href="https://www.npmjs.com/package/@samithahansaka/clipboard" rel="noopener noreferrer"&gt;@samithahansaka/clipboard&lt;/a&gt; to solve this once and for all.&lt;/p&gt;

&lt;p&gt;## The Problem with Existing Solutions                                                                                                                &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;react-copy-to-clipboard&lt;/strong&gt; - 500K+ weekly downloads, but:                                                                                            &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No hooks API (class component wrapper)
&lt;/li&gt;
&lt;li&gt;No rich content (HTML) support
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Manual implementation&lt;/strong&gt; - Copy-paste from Stack Overflow:                                                                                           &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Doesn't handle fallbacks properly
&lt;/li&gt;
&lt;li&gt;No state management for "Copied!" feedback
&lt;/li&gt;
&lt;li&gt;SSR issues with Next.js/Remix
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;**Introducing @samithahansaka/clipboard                                                                                                              &lt;/p&gt;

&lt;p&gt;Install:                                                                                                                                              &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  npm install @samithahansaka/clipboard                                                                                                             
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Features:&lt;/strong&gt;                                                                                                                                         &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hooks API ✅
&lt;/li&gt;
&lt;li&gt;Component API ✅
&lt;/li&gt;
&lt;li&gt;HTML/Rich content ✅
&lt;/li&gt;
&lt;li&gt;TypeScript first-class ✅
&lt;/li&gt;
&lt;li&gt;Bundle size ~1KB
&lt;/li&gt;
&lt;li&gt;Zero dependencies
&lt;/li&gt;
&lt;li&gt;SSR-safe ✅
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Basic Usage&lt;/strong&gt;                                                                                                                                        &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  import { useCopyToClipboard } from '@samithahansaka/clipboard';                                                                                   

  function CopyButton() {                                                                                                                           
    const { copy, copied } = useCopyToClipboard();                                                                                                  

    return (                                                                                                                                        
      &amp;lt;button onClick={() =&amp;gt; copy('Hello, World!')}&amp;gt;                                                                                                
        {copied ? '✓ Copied!' : 'Copy'}                                                                                                             
      &amp;lt;/button&amp;gt;                                                                                                                                     
    );                                                                                                                                              
  }                                                                                                                                                 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Copying Rich HTML Content&lt;/strong&gt;                                                                                                                          &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const { copy } = useCopyToClipboard();                                                                                                            

  copy({                                                                                                                                            
    text: 'Hello, World!',                                                                                                                          
    html: '&amp;lt;strong&amp;gt;Hello, World!&amp;lt;/strong&amp;gt;'                                                                                                          
  });                                                                                                                                               
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When pasted into:                                                                                                                                     &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Notepad/Terminal&lt;/strong&gt;: Gets "Hello, World!"
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Docs/Email&lt;/strong&gt;: Gets &lt;strong&gt;Hello, World!&lt;/strong&gt; (bold)
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Integration with Toast Libraries&lt;/strong&gt;                                                                                                                   &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  import { useCopyToClipboard } from '@samithahansaka/clipboard';                                                                                   
  import { toast } from 'sonner';                                                                                                                   

  function ShareButton({ url }) {                                                                                                                   
    const { copy } = useCopyToClipboard({                                                                                                           
      onSuccess: () =&amp;gt; toast.success('Link copied!'),                                                                                               
      onError: () =&amp;gt; toast.error('Failed to copy'),                                                                                                 
    });                                                                                                                                             

    return &amp;lt;button onClick={() =&amp;gt; copy(url)}&amp;gt;Share&amp;lt;/button&amp;gt;;                                                                                        
  }                                                                                                                                                 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Component API&lt;/strong&gt;                                                                                                                                      &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  import { CopyButton } from '@samithahansaka/clipboard';                                                                                           

  function App() {                                                                                                                                  
    return (                                                                                                                                        
      &amp;lt;CopyButton content="Text to copy"&amp;gt;                                                                                                           
        {({ copy, copied }) =&amp;gt; (                                                                                                                    
          &amp;lt;button onClick={copy}&amp;gt;                                                                                                                   
            {copied ? 'Done!' : 'Copy'}                                                                                                             
          &amp;lt;/button&amp;gt;                                                                                                                                 
        )}                                                                                                                                          
      &amp;lt;/CopyButton&amp;gt;                                                                                                                                 
    );                                                                                                                                              
  }                                                                                                                                                 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;How It Works&lt;/strong&gt;                                                                                                                                       &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Modern browsers&lt;/strong&gt;: Uses &lt;code&gt;navigator.clipboard.write()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Older browsers&lt;/strong&gt;: Falls back to &lt;code&gt;navigator.clipboard.writeText()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Legacy browsers&lt;/strong&gt;: Falls back to &lt;code&gt;document.execCommand('copy')&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Links&lt;/strong&gt;                                                                                                                                              &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📦 &lt;a href="https://www.npmjs.com/package/@samithahansaka/clipboard" rel="noopener noreferrer"&gt;npm&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🐙 &lt;a href="https://github.com/samithahansaka/samiya-clipboard" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📖 &lt;a href="https://samithahansaka.github.io/samiya-clipboard/" rel="noopener noreferrer"&gt;Docs&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;🎮 &lt;a href="https://codesandbox.io/p/sandbox/samithahansaka-clipboard-demo-s34ypx" rel="noopener noreferrer"&gt;Demo&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If this helped you, consider giving the repo a ⭐ on GitHub!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
