useCopyToClipboard

Copy text to the user's clipboard.

https://www.shaktools.com/shak-hooks
Utility

API Reference

Installation

terminal
pnpm add @shak-hooks/usehooks
import.ts
import { useCopyToClipboard } from "@shak-hooks/usehooks";
Source: packages/react/src/useCopyToClipboard.ts

Signature

signature.ts
export function useCopyToClipboard()

Parameters

No parameters.

Returns

unknown

Implementation

use-copy-to-clipboard.ts
1import { useState } from "react";
2
3export function useCopyToClipboard() {
4 const [copiedText, setCopiedText] = useState<string | null>(null);
5
6 const copy = async (text: string) => {
7 if (!navigator?.clipboard) {
8 console.warn("Clipboard not supported");
9 return false;
10 }
11
12 try {
13 await navigator.clipboard.writeText(text);
14 setCopiedText(text);
15 return true;
16 } catch (error) {
17 console.warn("Copy failed", error);
18 setCopiedText(null);
19 return false;
20 }
21 };
22
23 return [copiedText, copy] as const;
24}

Advertisement

Google Ads

Usage

example.tsx
1import { useCopyToClipboard } from "@shak-hooks/usehooks";
2
3const result = useCopyToClipboard();
4// result: unknown
5// Use values directly (React state).

Let‘s do great work together

Empowering creators with free, high-performance AI, SEO, and developer tools. Join thousands of users optimizing their workflow with Shak-Tools.

Tools10+ Free
UsersGlobal

Stay in the loop

Join our newsletter for the latest AI tools and updates.

ShakTools
© 2025 Shaktools. All Rights Reserved.Privacy Policy