-
Notifications
You must be signed in to change notification settings - Fork 249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Copy Image Functionality and added icons to buttons #124
Changes from 4 commits
970482f
7b53fd3
9004f88
20a0b21
941a384
6507227
bd02f5a
b60452a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
import { | ||
IconCopy, | ||
IconDownload, | ||
IconShare, | ||
IconBrandTwitter | ||
} from "@tabler/icons"; | ||
import { toast } from "react-hot-toast"; | ||
import React, { useRef, useState, useEffect } from "react"; | ||
import { | ||
download, | ||
|
@@ -56,6 +63,24 @@ const App = () => { | |
download(canvasRef.current); | ||
}; | ||
|
||
const onCopy = (e) => { | ||
e.preventDefault(); | ||
if ("ClipboardItem" in window) { | ||
canvasRef.current.toBlob((blob) => { | ||
const item = new ClipboardItem({ "image/png": blob }); | ||
navigator.clipboard.write([item]) | ||
.then(() => toast("🎉 Copied image!")) | ||
.catch(err => { | ||
toast("Sorry, copying image is not supported on this browser"); | ||
console.error("failed to copy"); | ||
}); | ||
}); | ||
} else { | ||
toast("Sorry, copying image is not supported on this browser"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can fallback to this solution: https://copee.ceriously.com There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. Thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @styfle Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @max-programming this answer is relevant: https://stackoverflow.com/a/68241516 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sallar Check it now There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven’t tried with an image blob but I suspect it won’t work now that I think about it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @max-programming @styfle I think it's not that it's not supported, the issue is that the time delay between the click and the copy call is too long and safari blocks it, the blob needs to be ready when the user clicks the button There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
console.error("failed to copy"); | ||
} | ||
}; | ||
|
||
const onDownloadJson = (e) => { | ||
e.preventDefault(); | ||
if (data != null) { | ||
|
@@ -130,11 +155,20 @@ const App = () => { | |
{data !== null && ( | ||
<> | ||
<div className="App-buttons"> | ||
<button | ||
className="App-download-button" | ||
onClick={onCopy} | ||
type="button" | ||
> | ||
<IconCopy size={18} /> | ||
Copy the Image | ||
</button> | ||
<button | ||
className="App-download-button" | ||
onClick={onDownload} | ||
type="button" | ||
> | ||
<IconDownload size={18} /> | ||
Download the Image | ||
</button> | ||
{global.navigator && "share" in navigator ? ( | ||
|
@@ -143,6 +177,7 @@ const App = () => { | |
onClick={onShare} | ||
type="button" | ||
> | ||
<IconShare size={18} /> | ||
Share | ||
</button> | ||
) : ( | ||
|
@@ -151,6 +186,7 @@ const App = () => { | |
onClick={onShareTwitter} | ||
type="button" | ||
> | ||
<IconBrandTwitter size={18} /> | ||
Share on Twitter | ||
</button> | ||
)} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there a lot of icons in this package?
This could cause the bundle to include every icon.
See https://nextjs.org/blog/next-13-1#import-resolution-for-smaller-bundles
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will take a look if I can make the change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@styfle I can't find another way to import these icons. Would you like to use a different icon library instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used the
react-icons
library which seems more performant. (Using Tabler Icons under the hood)This is the build command output