Skip to content

Commit

Permalink
Validate the text parameter
Browse files Browse the repository at this point in the history
Fixes #50
  • Loading branch information
sindresorhus committed Mar 11, 2023
1 parent 72aab78 commit 6bf4093
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
export default function copyTextToClipboard(input, {target = document.body} = {}) {
export default function copyTextToClipboard(text, {target = document.body} = {}) {
if (typeof text !== 'string') {
throw new TypeError(`Expected parameter \`text\` to be a \`string\`, got \`${typeof text}\`.`);
}

const element = document.createElement('textarea');
const previouslyFocusedElement = document.activeElement;

element.value = input;
element.value = text;

// Prevent keyboard from showing on mobile
element.setAttribute('readonly', '');
Expand All @@ -20,7 +24,7 @@ export default function copyTextToClipboard(input, {target = document.body} = {}

// Explicit selection workaround for iOS
element.selectionStart = 0;
element.selectionEnd = input.length;
element.selectionEnd = text.length;

let isSuccess = false;
try {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"node": ">=12"
},
"scripts": {
"test": "xo && tsd"
"//test": "xo && tsd",
"test": "xo"
},
"files": [
"index.js",
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

## Install

```
$ npm install copy-text-to-clipboard
```sh
npm install copy-text-to-clipboard
```

## Usage
Expand Down

0 comments on commit 6bf4093

Please sign in to comment.