Skip to Content
RecipesCopy to Clipboard (text, image)

Copy to Clipboard

Text

import { clipboard } from "@tynd/core/client"; await clipboard.writeText("Hello, world"); const got = await clipboard.readText();

Image (base64 PNG)

clipboard.writeImage takes a base64-encoded PNG string. Convert a canvas to PNG, strip the data-URL prefix, write it.

const canvas = document.querySelector("canvas")!; const dataUrl = canvas.toDataURL("image/png"); const base64 = dataUrl.replace(/^data:image\/png;base64,/, ""); await clipboard.writeImage(base64);

Reading an image (returns { png, width, height } with png as base64):

const img = await clipboard.readImage(); // null if no image if (img) { const el = document.createElement("img"); el.src = `data:image/png;base64,${img.png}`; el.width = img.width; el.height = img.height; document.body.appendChild(el); }

HTML (write-only)

await clipboard.writeHtml(`<p><b>Hello</b> <i>world</i></p>`);

Paste into a rich-text target (email, Google Doc) → formatted content. Plain-text targets get a stripped fallback (the OS handles this).

Clear

await clipboard.clear();

Related: clipboard API.

Last updated on