Skip to content

Commit 095dc6e

Browse files
authored
Fix Clipboard in safari browser (#69)
* Fix Clipboard for Safari browser * Update github action to automatically bump package.json version
1 parent 3c8e51c commit 095dc6e

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

.github/workflows/deploy.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: Deploy React Example
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66

77
permissions:
88
contents: write
@@ -19,8 +19,8 @@ jobs:
1919
uses: actions/setup-node@v3
2020
with:
2121
node-version: 16
22-
cache: 'yarn'
23-
cache-dependency-path: 'example/yarn.lock'
22+
cache: "yarn"
23+
cache-dependency-path: "example/yarn.lock"
2424

2525
- uses: pnpm/[email protected]
2626
name: Install pnpm
@@ -55,3 +55,12 @@ jobs:
5555
with:
5656
github_token: ${{ secrets.GITHUB_TOKEN }}
5757
publish_dir: ./example/build
58+
59+
- name: "Automated Version Bump"
60+
uses: "phips28/gh-action-bump-version@master"
61+
env:
62+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63+
with:
64+
minor-wording: "Add,add,new"
65+
major-wording: "MAJOR,major,Breaking Change,breaking change"
66+
patch-wording: "patch,fixes,fix,Fix"

example/src/App.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function App() {
5757
<CopyButton
5858
className="button button-outline"
5959
text={copyState.text}
60-
onClick={handleTextCopy}
60+
onCopy={handleTextCopy}
6161
>
6262
{isTextCopied ? 'Text Copied!' : 'Copy Text'}
6363
</CopyButton>
@@ -75,14 +75,14 @@ function App() {
7575
<CopyButton
7676
className="button button-outline"
7777
imageRef={imageRef}
78-
onClick={handleImageCopy}
78+
onCopy={handleImageCopy}
7979
>
8080
{isImageCopied ? 'Image Copied!' : 'Copy Image'}
8181
</CopyButton>
8282
<CopyButton
8383
className="button button-outline"
8484
imageURL={copyState.image}
85-
onClick={handleNewImageCopy}
85+
onCopy={handleNewImageCopy}
8686
>
8787
{isImageNewCopied ? 'Image Copied! (New)' : 'Copy Image (New)'}
8888
</CopyButton>

src/components/CopyButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { copyImageToClipboard, copyTextToClipboard } from '../utils/clipboard';
44
type CopyProps = {
55
children: React.ReactNode;
66
className?: string;
7-
onClick?: () => void;
7+
onCopy?: () => void;
88
text?: string;
99
imageURL?: string;
1010
imageRef?: React.RefObject<HTMLDivElement>;
@@ -28,7 +28,7 @@ const CopyButton = (props: CopyProps) => {
2828
copyImageToClipboard(content);
2929
}
3030

31-
props.onClick && props.onClick();
31+
props.onCopy && props.onCopy();
3232
};
3333

3434
return (

src/utils/clipboard.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
77
* @returns Promise
88
*/
99
export async function copyTextToClipboard(text: string): Promise<void> {
10-
if ('clipboard' in navigator) {
10+
if (navigator.clipboard) {
1111
await navigator.clipboard.writeText(text);
1212
} else {
1313
copyTextLegacy(text);
@@ -52,7 +52,7 @@ async function getTextBlobFromUrl(url: string): Promise<Blob> {
5252
*
5353
* @param {string} url
5454
* @param {HTMLDivElement} element
55-
* @returns Promise
55+
* @returns {Promise<void>}
5656
*/
5757
export async function copyImageToClipboard(
5858
content: string | HTMLDivElement
@@ -63,7 +63,7 @@ export async function copyImageToClipboard(
6363
return;
6464
}
6565

66-
if ('clipboard' in navigator) {
66+
if (navigator.clipboard) {
6767
const { type: mimeType } = await getImageBlobFromUrl(content);
6868
const blobPromise =
6969
mimeType === 'image/svg'
@@ -72,26 +72,26 @@ export async function copyImageToClipboard(
7272

7373
let clipboardObject: { [key: string]: any };
7474

75-
if (!isSafari) {
75+
if (isSafari) {
7676
clipboardObject = { [mimeType]: blobPromise };
7777
} else {
78-
const blob = await blobPromise;
79-
80-
clipboardObject = { [blob.type]: blob };
78+
clipboardObject = { [mimeType]: await blobPromise };
8179
}
8280

8381
try {
84-
return await navigator.clipboard.write([
82+
await navigator.clipboard.write([
8583
new window.ClipboardItem(clipboardObject)
8684
]);
8785
} catch (err) {
8886
console.warn(`Image not supported.`, err);
89-
90-
return;
9187
}
88+
89+
return;
9290
}
9391

94-
console.warn("'navigator.clipboard' is not supported. Pass element instead.");
92+
console.warn(
93+
"'navigator.clipboard' is not supported in this browser. Pass element instead."
94+
);
9595
}
9696

9797
/**

0 commit comments

Comments
 (0)