forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(www): use new clipboard.writeText API if it exists (gatsbyjs…
- Loading branch information
Showing
5 changed files
with
78 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from "react" | ||
import { render } from "react-testing-library" | ||
|
||
import Copy from "../copy" | ||
|
||
test(`it renders Copy by default`, () => { | ||
const { queryByText } = render(<Copy content="1234" />) | ||
|
||
expect(queryByText(`Copy`)).toBeInTheDocument() | ||
}) | ||
|
||
test(`it renders screen-reader text`, () => { | ||
const { container } = render(<Copy content="1234" />) | ||
|
||
expect(container.querySelector(`[aria-roledescription]`)).toBeInTheDocument() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import copyToClipboard from "../copy-to-clipboard" | ||
|
||
beforeEach(() => { | ||
window.navigator.clipboard = { | ||
writeText: jest.fn(), | ||
} | ||
|
||
document.execCommand = jest.fn() | ||
document.createRange = jest.fn() | ||
window.getSelection = jest.fn(() => { | ||
return { | ||
removeAllRanges: jest.fn(), | ||
addRange: jest.fn(), | ||
} | ||
}) | ||
}) | ||
|
||
test(`it uses writeText API, by default`, async () => { | ||
const str = `waddup` | ||
await copyToClipboard(str) | ||
|
||
expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith(str) | ||
}) | ||
|
||
test(`it falls back to execCommand, if writeText is not defined`, async () => { | ||
window.navigator.clipboard = {} | ||
|
||
await copyToClipboard(`sup`) | ||
|
||
expect(document.execCommand).toHaveBeenCalledWith(`copy`) | ||
expect(document.execCommand).toHaveBeenCalledTimes(1) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,30 @@ | ||
// https://hackernoon.com/copying-text-to-clipboard-with-javascript-df4d4988697f | ||
const copyToClipboard = str => { | ||
const el = document.createElement(`textarea`) // Create a <textarea> element | ||
el.value = str // Set its value to the string that you want copied | ||
el.setAttribute(`readonly`, ``) // Make it readonly to be tamper-proof | ||
el.style.position = `absolute` | ||
el.style.left = `-9999px` // Move outside the screen to make it invisible | ||
document.body.appendChild(el) // Append the <textarea> element to the HTML document | ||
const selected = | ||
document.getSelection().rangeCount > 0 // Check if there is any content selected previously | ||
? document.getSelection().getRangeAt(0) // Store selection if found | ||
: false // Mark as false to know no selection existed before | ||
el.select() // Select the <textarea> content | ||
document.execCommand(`copy`) // Copy - only works as a result of a user action (e.g. click events) | ||
document.body.removeChild(el) // Remove the <textarea> element | ||
if (selected) { | ||
// If a selection existed before copying | ||
document.getSelection().removeAllRanges() // Unselect everything on the HTML document | ||
document.getSelection().addRange(selected) // Restore the original selection | ||
const clipboard = window.navigator.clipboard | ||
/* | ||
* fallback to older browsers (including Safari) | ||
* if clipboard API not supported | ||
*/ | ||
if (!clipboard || typeof clipboard.writeText !== `function`) { | ||
const textarea = document.createElement(`textarea`) | ||
textarea.value = str | ||
textarea.setAttribute(`readonly`, true) | ||
textarea.setAttribute(`contenteditable`, true) | ||
textarea.style.position = `absolute` | ||
textarea.style.left = `-9999px` | ||
document.body.appendChild(textarea) | ||
textarea.select() | ||
const range = document.createRange() | ||
const sel = window.getSelection() | ||
sel.removeAllRanges() | ||
sel.addRange(range) | ||
textarea.setSelectionRange(0, textarea.value.length) | ||
document.execCommand(`copy`) | ||
document.body.removeChild(textarea) | ||
|
||
return Promise.resolve(true) | ||
} | ||
|
||
return clipboard.writeText(str) | ||
} | ||
|
||
export default copyToClipboard |