Community JavaScript Snippet

Paste-Image-From-Clipboard Handler

When a user hits Cmd-V with a screenshot in their clipboard, we want to upload it as if they had drag-dropped. This is the 30-line paste handler I keep, including the Safari-only `clipboardData.items` traversal and a graceful HEIC fallback.

Paste-Image-From-Clipboard Handler

When a user hits Cmd-V with a screenshot in their clipboard, we want to upload it as if they had drag-dropped. This is the 30-line paste handler I keep, including the Safari-only `clipboardData.items` traversal and a graceful HEIC fallback.

JavaScript
Frontend
2 snippets
js-dom
frontend
code-template
rajreeves

By @rajreeves

January 14, 2026

·

Updated May 18, 2026

956 views

28

4.2 (10)

The whole pattern hinges on clipboardData.items rather than clipboardData.files, because Safari does not populate files on paste events even when an image is on the clipboard. Walking items and filtering on kind === 'file' plus type.startsWith('image/') works in every desktop browser I have tested. The preventDefault call is what stops the textarea from inserting image.png as plain text, which is the most common bug report: "I pasted a screenshot and nothing happened, but the filename appeared." Returning the cleanup function lets React or any framework remove the listener on unmount.