Skip to content

Commit 17b80d8

Browse files
Merge pull request #25 from ryangjchandler/feature/x-clipboard
2 parents 9d14fdc + 13362fb commit 17b80d8

File tree

5 files changed

+85
-24
lines changed

5 files changed

+85
-24
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ To copy some data to the clipboard, invoke `$clipboard` from an event handler in
5555
</div>
5656
```
5757

58+
### Directive
59+
60+
This package also includes an `x-clipboard` directive that can be added to any element (usually a `button`) and it will copy the result of the expression on `click`.
61+
62+
```html
63+
<div x-data="{ input: 'Foo!' }">
64+
<button x-clipboard="input">
65+
Copy to Clipboard
66+
</button>
67+
</div>
68+
```
69+
70+
If you are rendering on the server, you might prefer to copy raw content instead of evaluating the expression as JavaScript. This can be done by adding the `.raw` modifier to the directive.
71+
72+
Here's a Blade snippet as an example:
73+
74+
```blade
75+
<button x-clipboard.raw="{{ $post->url() }}">
76+
Copy to Clipboard
77+
</button>
78+
```
79+
5880
### `Object` and `Array`
5981

6082
Since you can pass any properties through to the `$clipboard` function, if you pass through an `Object` or `Array`, it will be run through `JSON.stringify` before being copied to the clipboard.

dist/alpine-clipboard.js

Lines changed: 29 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/alpine-clipboard.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/index.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,13 @@
2121
<button type="button" @click="$clipboard(items)">Copy the JSON representation of `items` to your clipboard</button>
2222
</div>
2323

24+
<div x-data="{ text: 'boo' }">
25+
<button x-clipboard="text">This uses clipboard directive to copy the text "boo" to your clipboard.</button>
26+
27+
<button x-clipboard.raw="Here is some raw text to copy to the clipboard.">
28+
This will let you copy any raw text in the directive.
29+
</button>
30+
</div>
31+
2432
<script src="../dist/alpine-clipboard.js"></script>
2533
</body>

src/index.js

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,32 @@
11
let onCopy = () => {}
22

3+
const copy = (target) => {
4+
if (typeof target === 'function') {
5+
target = target()
6+
}
7+
8+
if (typeof target === 'object') {
9+
target = JSON.stringify(target)
10+
}
11+
12+
return window.navigator.clipboard.writeText(target)
13+
.then(onCopy)
14+
}
15+
316
function Clipboard(Alpine) {
417
Alpine.magic('clipboard', () => {
5-
return function (target) {
6-
if (typeof target === 'function') {
7-
target = target()
8-
}
9-
10-
if (typeof target === 'object') {
11-
target = JSON.stringify(target)
12-
}
13-
14-
return window.navigator.clipboard.writeText(target)
15-
.then(onCopy)
16-
}
18+
return copy
19+
})
20+
21+
Alpine.directive('clipboard', (el, { modifiers, expression }, { evaluateLater, cleanup }) => {
22+
const getCopyContent = modifiers.includes('raw') ? c => c(expression) : evaluateLater(expression)
23+
const clickHandler = () => getCopyContent(copy)
24+
25+
el.addEventListener('click', clickHandler)
26+
27+
cleanup(() => {
28+
el.removeEventListener('click', clickHandler)
29+
})
1730
})
1831
}
1932

0 commit comments

Comments
 (0)