React CopyToClipboard component

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Search for a command to run...

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
No comments yet. Be the first to comment.
Writing this up so I have somewhere to quickly grab basic Jest setup. This information is copied from the Jest Documentation Site compressed into just the parts I'm interested in. Create folders mkdir -p <project>/src cd <project> Install and init n...

In this blog post, we'll explore the process of setting up and configuring a Postgres database using Docker and seeding it with test data. This approach can be used for a variety of apps and can be easily adapted for your own needs. We'll cover all t...

Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started! Server & Client Code In SvelteKit, the server-sid...

Introduction In this post, I will explore how to create a REST API using Spring Boot and Kotlin. We will dive into the fundamentals of Spring Boot and Kotlin, explain how to set up the project, and guide you through the process of building the API st...

Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...

A quick post documenting a simple CopyToClipboard component I created this week. It allows users to create a git branch slug when working on a story.

Click the Clipboard Icon button and the user gets a git branch command copied to their clipboard, simply but very handy. For example it generates:
git checkout -b st-270-write-blog-post
Paste the command into the command line and we're off to the races 😀
It also shows a temporary message to the user to notify them that the copy was successful.
Supply the component with a getText function that returns the text you want to copy to the clipboard. In this case we're generating a git slug but it can be any string you like.
...
<CopyToClipboard getText={() => slugify(`${screenId}-${heading}`)} />
...
This is a Functional React component using Typescript. We use the Clipboard API navigator.clipboard to access the system clipboard.
import React, { useState } from 'react';
import { IconClipboard } from '../Icons';
export type Props = {
getText: () => string;
};
export const CopyToClipboard: React.FC<Props> = ({ getText }) => {
const [copied, setCopied] = useState(false);
const handleClick = () => {
navigator.clipboard
.writeText(getText())
.then(() => {
setCopied(true);
setTimeout(() => {
setCopied(false);
}, 1000);
})
.catch(error => console.log('Handle the error better than this :-)'));
};
return (
<>
<button onClick={handleClick}>
<IconClipboard />
</button>
{copied && <p>Copied!</p>}
</>
);
};
Check my previous post on how to get the clipboard icons for the button 🙂