Skip to content

Commit

Permalink
Add fallback to ensure older versions of iOS Safari are supported
Browse files Browse the repository at this point in the history
Older versions of Safari on iOS had problems selecting text programatically. This small commit adds a workaround for that by using an older, yet still supported, method of selecting text when on iOS devices.
  • Loading branch information
GeoffSelby committed Mar 1, 2020
1 parent c230181 commit 0d718f6
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/copyToClipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,23 @@ const copyToClipboard = text => {

document.body.appendChild(fakeElem)

fakeElem.select()
if (isIOS()) {
const range = document.createRange()
const selection = window.getSelection()
range.selectNodeContents(fakeElem)
selection.removeAllRanges()
selection.addRange(range)
fakeElem.setSelectionRange(0, 999999)
} else {
fakeElem.select()
}

document.execCommand("copy")

document.body.removeChild(fakeElem)
fakeElem = null
}

const isIOS = () => navigator.userAgent.match(/ipad|iphone/i)

export default copyToClipboard

0 comments on commit 0d718f6

Please sign in to comment.