@exmg/exm-copy-to-clipboard 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -0
- package/index.d.ts +2 -0
- package/index.js +3 -0
- package/package.json +43 -0
- package/src/exm-copy-to-clipboard.d.ts +49 -0
- package/src/exm-copy-to-clipboard.js +122 -0
- package/src/styles/exm-copy-to-clipboard-styles-css.d.ts +2 -0
- package/src/styles/exm-copy-to-clipboard-styles-css.js +4 -0
- package/src/styles/exm-copy-to-clipboard-styles.scss +6 -0
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# `<exm-copy-to-clipboard>` [](https://www.npmjs.com/package/@exmg/exm-copy-to-clipboard)
|
|
2
|
+
|
|
3
|
+
# @exmg/exm-copy-to-clipboard
|
|
4
|
+
|
|
5
|
+
Helper element to create icon/buttons that lets the user copy content to the clipboard. Just wrap it around
|
|
6
|
+
the button or icon and set the value that needs to be copied.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install @exmg/exm-copy-to-clipboard
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Example Usage
|
|
15
|
+
|
|
16
|
+
### Standard
|
|
17
|
+
|
|
18
|
+
```html
|
|
19
|
+
<exm-copy-to-clipboard value="mark@test.com">
|
|
20
|
+
<md-icon-button><md-icon>content-copy</md-icon></md-icon-button>
|
|
21
|
+
</exm-copy-to-clipboard>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## API
|
|
25
|
+
|
|
26
|
+
### Slots
|
|
27
|
+
|
|
28
|
+
| Name | Description |
|
|
29
|
+
| --------- | ---------------------------------- |
|
|
30
|
+
| _default_ | Button to handle copy to clipboard |
|
|
31
|
+
|
|
32
|
+
### Properties/Attributes
|
|
33
|
+
|
|
34
|
+
| Name | Type | Default | Description |
|
|
35
|
+
| ------- | -------- | ------- | ---------------------------------------- |
|
|
36
|
+
| `value` | `string` | _None_ | The value that gets copied and displayed |
|
|
37
|
+
|
|
38
|
+
### Methods
|
|
39
|
+
|
|
40
|
+
| Name | Description |
|
|
41
|
+
| -------- | ------------------------------------------------- |
|
|
42
|
+
| `toggle` | Toggles the opened state of the component |
|
|
43
|
+
| `show` | Sets the opened state of the component to _true_ |
|
|
44
|
+
| `hide` | Sets the opened state of the component to _false_ |
|
|
45
|
+
|
|
46
|
+
### Events
|
|
47
|
+
|
|
48
|
+
_None_
|
|
49
|
+
|
|
50
|
+
### CSS Custom Properties
|
|
51
|
+
|
|
52
|
+
_None_
|
|
53
|
+
|
|
54
|
+
## Additional references
|
|
55
|
+
|
|
56
|
+
- [Additional Documentation](https://exmg.github.io/exmachina-web-components/ExmgCopyToClipboard.html)
|
|
57
|
+
|
|
58
|
+
- [Demo](https://exmg.github.io/exmachina-web-components/demo/?el=exm-copy-to-clipboard)
|
package/index.d.ts
ADDED
package/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@exmg/exm-copy-to-clipboard",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./exm-copy-to-clipboard.js": "./src/exm-copy-to-clipboard.js"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"@exmg/lit-base": "^2.0.1",
|
|
13
|
+
"lit": "^3.0.0",
|
|
14
|
+
"tslib": "^2.6.2"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"**/*.scss",
|
|
18
|
+
"**/*.js",
|
|
19
|
+
"**/*.d.ts"
|
|
20
|
+
],
|
|
21
|
+
"keywords": [
|
|
22
|
+
"web-components",
|
|
23
|
+
"lit",
|
|
24
|
+
"clipboard"
|
|
25
|
+
],
|
|
26
|
+
"homepage": "https://github.com/exmg/exmachina-web-components",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git@github.com:exmg/exm-web-components.git",
|
|
30
|
+
"directory": "packages/exm-copy-to-clipboard"
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@exmg/lit-cli": "1.1.13"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build:styles": "exmg-lit-cli sass -f \"./**/*.scss\""
|
|
38
|
+
},
|
|
39
|
+
"publishConfig": {
|
|
40
|
+
"access": "public"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "0907b55c89325d59902b98a64c352bf6e1fc81ff"
|
|
43
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ExmgElement } from '@exmg/lit-base/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* `<exm-copy-to-clipboard>` Helper element to create icon/buttons that
|
|
4
|
+
* lets the user copy content to the clipboard. Just wrap it arround
|
|
5
|
+
* the button or icon and set the value that needs to be copied.
|
|
6
|
+
*
|
|
7
|
+
* ```html
|
|
8
|
+
* <exm-copy-to-clipboard value="mark@test.com">
|
|
9
|
+
* <md-icon-button icon="content-copy"><md-icon>content-copy</md-icon></md-icon-button>
|
|
10
|
+
* </exm-copy-to-clipboard>
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* @customElement exm-copy-to-clipboard
|
|
14
|
+
* @extends ExmgElement
|
|
15
|
+
*/
|
|
16
|
+
export declare class ExmgCopyToClipboard extends ExmgElement {
|
|
17
|
+
/**
|
|
18
|
+
* Value to be copied
|
|
19
|
+
* @type {String}
|
|
20
|
+
*/
|
|
21
|
+
value?: string;
|
|
22
|
+
private isCopySupported;
|
|
23
|
+
protected bubbles: boolean;
|
|
24
|
+
clipboard?: HTMLElement;
|
|
25
|
+
private htmlElement?;
|
|
26
|
+
private handleCopy;
|
|
27
|
+
private _observer?;
|
|
28
|
+
static styles: import("lit").CSSResult[];
|
|
29
|
+
constructor();
|
|
30
|
+
connectedCallback(): void;
|
|
31
|
+
firstUpdated(): void;
|
|
32
|
+
disconnectedCallback(): void;
|
|
33
|
+
addClickListener(): void;
|
|
34
|
+
/**
|
|
35
|
+
* Copy the given value to the clipboard
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
private copyToClipboard;
|
|
39
|
+
/**
|
|
40
|
+
* Handle button tap event and trigger the actual copy to clipboard
|
|
41
|
+
*/
|
|
42
|
+
handleCopyAction(e: Event): void;
|
|
43
|
+
render(): import("lit-html").TemplateResult<1>;
|
|
44
|
+
}
|
|
45
|
+
declare global {
|
|
46
|
+
interface HTMLElementTagNameMap {
|
|
47
|
+
'exm-copy-to-clipboard': ExmgCopyToClipboard;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { __decorate } from "tslib";
|
|
2
|
+
import { html } from 'lit';
|
|
3
|
+
import { customElement, state, property, query } from 'lit/decorators.js';
|
|
4
|
+
import { ExmgElement } from '@exmg/lit-base/index.js';
|
|
5
|
+
import { style } from './styles/exm-copy-to-clipboard-styles-css.js';
|
|
6
|
+
/**
|
|
7
|
+
* `<exm-copy-to-clipboard>` Helper element to create icon/buttons that
|
|
8
|
+
* lets the user copy content to the clipboard. Just wrap it arround
|
|
9
|
+
* the button or icon and set the value that needs to be copied.
|
|
10
|
+
*
|
|
11
|
+
* ```html
|
|
12
|
+
* <exm-copy-to-clipboard value="mark@test.com">
|
|
13
|
+
* <md-icon-button icon="content-copy"><md-icon>content-copy</md-icon></md-icon-button>
|
|
14
|
+
* </exm-copy-to-clipboard>
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* @customElement exm-copy-to-clipboard
|
|
18
|
+
* @extends ExmgElement
|
|
19
|
+
*/
|
|
20
|
+
let ExmgCopyToClipboard = class ExmgCopyToClipboard extends ExmgElement {
|
|
21
|
+
constructor() {
|
|
22
|
+
super();
|
|
23
|
+
this.isCopySupported = false;
|
|
24
|
+
this.bubbles = false;
|
|
25
|
+
this.isCopySupported = !!document.queryCommandSupported('copy');
|
|
26
|
+
this.handleCopy = this.handleCopyAction.bind(this);
|
|
27
|
+
}
|
|
28
|
+
connectedCallback() {
|
|
29
|
+
super.connectedCallback();
|
|
30
|
+
// Create an observer instance linked to the callback function
|
|
31
|
+
this._observer = new MutationObserver((mutationsList) => {
|
|
32
|
+
for (const mutation of mutationsList) {
|
|
33
|
+
if (mutation.type === 'childList') {
|
|
34
|
+
this.addClickListener();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
// Start observing the target node for configured mutations
|
|
39
|
+
this._observer.observe(this, { attributes: true, childList: true, subtree: false });
|
|
40
|
+
}
|
|
41
|
+
firstUpdated() {
|
|
42
|
+
this.addClickListener();
|
|
43
|
+
if (!this.isCopySupported && this.htmlElement) {
|
|
44
|
+
this.htmlElement.style.display = 'none';
|
|
45
|
+
this.dispatchEvent(new CustomEvent('copy-not-supported', { bubbles: this.bubbles, composed: true }));
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
disconnectedCallback() {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
(_a = this.htmlElement) === null || _a === void 0 ? void 0 : _a.removeEventListener('click', this.handleCopy);
|
|
51
|
+
(_b = this._observer) === null || _b === void 0 ? void 0 : _b.disconnect();
|
|
52
|
+
super.disconnectedCallback();
|
|
53
|
+
}
|
|
54
|
+
addClickListener() {
|
|
55
|
+
var _a, _b;
|
|
56
|
+
(_a = this.htmlElement) === null || _a === void 0 ? void 0 : _a.removeEventListener('click', this.handleCopy);
|
|
57
|
+
const elements = this.querySelectorAll('*');
|
|
58
|
+
this.htmlElement = elements[0];
|
|
59
|
+
(_b = this.htmlElement) === null || _b === void 0 ? void 0 : _b.addEventListener('click', this.handleCopy);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Copy the given value to the clipboard
|
|
63
|
+
* @private
|
|
64
|
+
*/
|
|
65
|
+
copyToClipboard() {
|
|
66
|
+
const clipboardNode = this.shadowRoot ? this.clipboard : null;
|
|
67
|
+
if (!clipboardNode) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
clipboardNode.style.display = 'block';
|
|
71
|
+
const range = document.createRange();
|
|
72
|
+
const selection = window.getSelection();
|
|
73
|
+
range.selectNodeContents(clipboardNode);
|
|
74
|
+
selection.removeAllRanges();
|
|
75
|
+
selection.addRange(range);
|
|
76
|
+
try {
|
|
77
|
+
document.execCommand('copy');
|
|
78
|
+
this.dispatchEvent(new CustomEvent('exm-copy-to-clipboard-copied', {
|
|
79
|
+
detail: this.value,
|
|
80
|
+
bubbles: this.bubbles,
|
|
81
|
+
composed: true,
|
|
82
|
+
}));
|
|
83
|
+
}
|
|
84
|
+
catch (err) {
|
|
85
|
+
// eslint-disable-next-line no-console
|
|
86
|
+
console.error('copy to clipboard failed', err);
|
|
87
|
+
}
|
|
88
|
+
selection.removeAllRanges();
|
|
89
|
+
clipboardNode.style.display = 'none';
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Handle button tap event and trigger the actual copy to clipboard
|
|
93
|
+
*/
|
|
94
|
+
handleCopyAction(e) {
|
|
95
|
+
this.copyToClipboard();
|
|
96
|
+
e.stopPropagation();
|
|
97
|
+
}
|
|
98
|
+
render() {
|
|
99
|
+
return html `
|
|
100
|
+
<slot></slot>
|
|
101
|
+
<span id="clipboard">${this.value}</span>
|
|
102
|
+
`;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
ExmgCopyToClipboard.styles = [style];
|
|
106
|
+
__decorate([
|
|
107
|
+
property({ type: String })
|
|
108
|
+
], ExmgCopyToClipboard.prototype, "value", void 0);
|
|
109
|
+
__decorate([
|
|
110
|
+
state()
|
|
111
|
+
], ExmgCopyToClipboard.prototype, "isCopySupported", void 0);
|
|
112
|
+
__decorate([
|
|
113
|
+
state()
|
|
114
|
+
], ExmgCopyToClipboard.prototype, "bubbles", void 0);
|
|
115
|
+
__decorate([
|
|
116
|
+
query('#clipboard')
|
|
117
|
+
], ExmgCopyToClipboard.prototype, "clipboard", void 0);
|
|
118
|
+
ExmgCopyToClipboard = __decorate([
|
|
119
|
+
customElement('exm-copy-to-clipboard')
|
|
120
|
+
], ExmgCopyToClipboard);
|
|
121
|
+
export { ExmgCopyToClipboard };
|
|
122
|
+
//# sourceMappingURL=exm-copy-to-clipboard.js.map
|