Skip to content

Commit

Permalink
Merge pull request #19 from ryangjchandler/feature/on-copy-hook
Browse files Browse the repository at this point in the history
feature: on copy hook
  • Loading branch information
ryangjchandler committed Nov 16, 2021
2 parents e64cc6c + 5c8630a commit ac8b146
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ Since you can pass any properties through to the `$clipboard` function, if you p

The clipboard will now contain `["foo","bar"]`.

### Hooks

If you are using the `npm` installation method for this package or the ESM distribution, you can use the `Clipboard.configure()` method to attach an `onCopy` hook to the clipboard.

```js
import Clipboard from '@ryangjchandler/alpine-clipboard'

Alpine.plugin(Clipboard.configure({
onCopy: () => {
console.log('Copied!')
}
}))
```

## Versioning

This projects follow the [Semantic Versioning](https://semver.org/) guidelines.
Expand Down
16 changes: 13 additions & 3 deletions dist/alpine-clipboard.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/alpine-clipboard.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export default function (Alpine) {
let onCopy = () => {}

function Clipboard(Alpine) {
Alpine.magic('clipboard', () => {
return function (target) {
if (typeof target === 'function') {
Expand All @@ -10,6 +12,17 @@ export default function (Alpine) {
}

return window.navigator.clipboard.writeText(target)
.then(onCopy)
}
})
}
}

Clipboard.configure = (config) => {
if (config.hasOwnProperty('onCopy') && typeof config.onCopy === 'function') {
onCopy = config.onCopy
}

return Clipboard
}

export default Clipboard;

0 comments on commit ac8b146

Please sign in to comment.