Links
react-smart-copy/link is for “Copy link” and “Share” buttons, and for fields people
paste links into. No dependencies, about 4 kB (linkPayload alone is about 1.8 kB).
Try it
Section titled “Try it”Paste drops the colour profile in Safari 18
- Slack, Docs, Notion, mail
- Press Copy link to see it.
- Editors, terminals, the address bar
Share opens the share sheet on phones and tablets, and copies the link elsewhere.
Link to a passage
A link to a passage scrolls the page to it and highlights it. Select a few words in this paragraph, then copy a link to them. Open the link in a new tab to see the passage highlighted.
Select some words in the paragraph above.
Select “release notes”, then paste a link with Ctrl + V or ⌘ + V. Ctrl + Z undoes it.
Nothing pasted yet.
The link, not just a URL
linkPayload copies a titled link and the URL, so chat and documents paste the title and
editors paste the address. Dragged, it adds text/uri-list. Only safe schemes are copied,
credentials never, and tracking parameters are removed when you ask.
useShare opens the share sheet on phones and copies the link on desktop. Closing the
share sheet is never an error, and method tells the button what to say.
selectionLink links to the words the reader selected. pasteAsLink turns
selected text into a link when a URL is pasted over it.
import { linkPayload, pasteAsLink, selectionLink } from 'react-smart-copy/link';
copy(() => linkPayload({ url, title }, { clean: true }));
const link = selectionLink();
if (link) copy(() => linkPayload(link));
<textarea onPaste={pasteAsLink} />
import { useCopy } from "react-smart-copy";import { linkPayload } from "react-smart-copy/link";
const { copy, status } = useCopy();<button onClick={() => copy(() => linkPayload({ url: issue.url, title: issue.title }, { clean: true }))}>Copy link</button>It pastes as the right thing everywhere. The clipboard gets a titled link (<a href>)
and the URL. Slack, Google Docs, Notion, Teams, Word and mail paste the title as a link;
editors, terminals and the address bar paste the URL. text: "markdown" makes the plain
text [title](url) for Markdown fields, "title-and-url" puts both on two lines. Dragged
with useDragCopy ({ target: "drag" }), it also carries text/uri-list and Firefox’s
text/x-moz-url, so it drops into tabs, bookmarks and other apps as a link.
Safe by default. Every URL is resolved to an absolute one and checked: only https:,
http:, mailto: and tel: are allowed (protocols changes that), so javascript: and
data: links are never copied or pasted. User names and passwords in a URL are always
removed. International domains are written in punycode, so a lookalike domain shows as
one. Titles are cleaned of hidden and reordering characters and capped at 300 characters
(maxTitleLength), and all HTML is escaped. Invalid input is a typed invalid-payload error.
Tracking parameters removed. clean: true (or cleanLinkUrl(url)) removes utm_*,
fbclid, gclid, msclkid and other click trackers, and keeps every other parameter and
the #section. clean: { params, keep } sets your own list.
import { cleanLinkUrl, linkPayload, resolveLink } from "react-smart-copy/link";
cleanLinkUrl("https://shop.example/p/42?utm_source=news&color=red#reviews");// "https://shop.example/p/42?color=red#reviews"
linkPayload(anchorElement); // an <a>: its resolved href and its textLinks to a passage. textFragmentUrl makes a link that scrolls to a passage and
highlights it (#:~:text=), in Chrome, Edge, Safari and Firefox. Other browsers open the
page as usual. selectionLink() links to what the reader has selected, with the page title
and the quote, ready for linkPayload. Passing a function to copy turns an invalid link into an
invalid-payload error state rather than an exception:
import { linkPayload, selectionLink, textFragmentUrl } from "react-smart-copy/link";
<button onClick={() => { const link = selectionLink(); if (link) void copy(() => linkPayload(link)); }}>Copy link to selection</button>
textFragmentUrl("https://example.com/docs#install", "npm install react-smart-copy");// "https://example.com/docs#install:~:text=npm%20install%20react%2Dsmart%2Dcopy"Long passages link by their first and last words, each taken from within one paragraph, as the syntax requires. When the passage appears more than once on the page, the words around it are added so the link finds the right one.
Pasting links. pasteAsLink turns selected text into a link when a URL is pasted over
it, as GitHub, Slack and Notion do. It writes [selection](url) by default (format
changes that) and keeps the browser’s undo. Every other paste is left alone.
readLink(result) reads the one link a paste or drop holds, with its title, from
text/uri-list, a single <a> or a single URL, or returns null.
import { pasteAsLink, readLink } from "react-smart-copy/link";
<textarea onPaste={pasteAsLink} />
usePaste({ accept: ["text", "html", "text/uri-list"], onPaste: (result) => { const link = readLink(result); if (link) addBookmark(link.url, link.title);} });| Option | Default | Notes |
|---|---|---|
text |
"url" |
plain text: "url", "title", "title-and-url" or "markdown" |
target |
clipboard | "drag" adds text/uri-list and text/x-moz-url |
clean |
false |
true, or { params, keep } |
protocols |
https:, http:, mailto:, tel: |
allowed schemes |
base |
the page’s address | resolves relative links; needed on the server |
maxTitleLength |
300 | |
maxWords / contextWords |
10 / 4 | text fragments |