Skip to content

Drag to copy

useDragCopy makes any element draggable and populates its DataTransfer with structured data when the user starts dragging it. No Clipboard API, no permission prompt — it works in every browser that supports HTML5 drag-and-drop.

Drag a card below and drop it anywhere — a text editor, a rich editor, your desktop, or the drop zone on this page.
Plain text kind: "text" INV-29018, DesignCo, ₹12,400
HTML + plain text kind: "html" <b>INV-29018</b>, DesignCo, <i>₹12,400</i>
JSON kind: "json" { id: "INV-29018", client: "DesignCo", amount: 12400 }
Cancelled source returns false preventDefault() is called; nothing drags

Drop a card here to see what landed.

Last drag

No drag yet.

Populate DataTransfer on drag

useDragCopy makes any element draggable and writes structured data into the DataTransfer when the user starts dragging — no Clipboard API, no permission prompt.

The source callback is called synchronously on dragstart and can return a string, a text / html / json / multi payload, false to cancel the drag, or null to leave the browser's default data untouched.

const { ref, status } = useDragCopy({
  source: () => ({
    kind: 'html',
    html: '<b>Hello</b>',
    text: 'Hello',
  }),
  effectAllowed: 'copy',
});
return <div ref={ref}>Drag me</div>;

// Framework-agnostic:
import { registerDragCopy } from 'react-smart-copy/core';
const { unsubscribe } = registerDragCopy(el, () => ({
  source: () => text,
}));
import { useDragCopy } from "react-smart-copy";
import { linkPayload } from "react-smart-copy/link";
function DraggableCard({ title, url }: { title: string; url: string }) {
const { ref, status } = useDragCopy({
// Escaped, scheme-checked, and with text/uri-list so it drops into the address bar too.
source: () => linkPayload({ url, title }, { target: "drag" }),
effectAllowed: "copy",
});
return (
<div ref={ref} style={{ opacity: status === "dragging" ? 0.5 : 1 }}>
{title}
</div>
);
}

The source callback is called synchronously on dragstart and must return synchronously (the DataTransfer closes when the event handler returns). Return:

Return value Effect
a string written as text/plain
a text / HTML / JSON / multi payload written according to its kind
false preventDefault() is called; the drag is cancelled
null / undefined the browser’s default drag data, untouched

The same payload shapes as useCopy are accepted (kind: "text", kind: "html", kind: "json", kind: "multi"), minus images — DataTransfer accepts strings only.

// Plain text
source: () => `${title}\n${url}`
// HTML + text fallback (e.g. for dropping into a rich-text editor)
source: () => ({ kind: "html", html: `<b>${title}</b>`, text: title })
// JSON (serialised as text/plain)
source: () => ({ kind: "json", value: { id, title }, pretty: true })
// Multiple MIME types (e.g. custom application type for your own drop targets)
source: () => ({
kind: "multi",
items: [
{ mimeType: "text/plain", data: title },
{ mimeType: "application/x-my-id", data: String(id) },
],
})

effectAllowed controls the drag cursor and the effects drop targets may request. Defaults to "copy"; pass "move" to signal that the source will be removed after a successful drop (your app must do the actual removal in dragend or on the server).

Always-fresh source. The source callback reads the latest props and state on every drag. You never need to list it in a dependency array or wrap it in a ref yourself.

enabled removes draggable and the listener without unmounting the component. Status resets to idle while disabled.

const { ref, status, outcome } = useDragCopy({
source: () => text,
enabled: !isLocked,
effectAllowed: "copyMove",
onDrag: (state) => {
if (state.status === "done") analytics.track("drag_complete");
},
onError: (err) => console.error("drag source failed", err),
});
Return value Type Description
ref RefCallback<T> Attach to the element you want to make draggable
status 'idle' | 'dragging' | 'done' Current drag state
outcome DragCopyOutcome | null Result of the last dragstart; null before any drag
onDragStart (event) => void For wiring via React’s onDragStart prop if needed

onDragStart prop path. The hook attaches its own native listener through ref, so you rarely need onDragStart. It is there for render-prop components or controlled drag libraries that require a React prop:

const { ref, onDragStart } = useDragCopy({ source: () => "data" });
return (
<SortableItem
ref={ref}
onDragStart={(e) =>
onDragStart({
type: "dragstart",
dataTransfer: e.dataTransfer,
defaultPrevented: e.defaultPrevented,
preventDefault: () => e.preventDefault(),
})
}
/>
);

SSR-safe. draggable is never set during server rendering; the ref callback runs only in the browser.

Without React. applyDragCopy and registerDragCopy are exported from react-smart-copy/core for framework-agnostic use:

import { registerDragCopy } from "react-smart-copy/core";
const { unsubscribe } = registerDragCopy(element, () => ({
source: () => `${title}\n${url}`,
effectAllowed: "copy",
}));
// later: unsubscribe();

Drag vs. copy. useCopy writes to the clipboard when the user clicks a button. useDragCopy writes to the DataTransfer when the user drags an element. Use them together if your UI supports both flows.