@exmg/exm-copy-to-clipboard 0.0.2-alpha.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 ADDED
@@ -0,0 +1,58 @@
1
+ # `<exm-copy-to-clipboard>` [![Published on npm](https://img.shields.io/npm/v/@exmg/exm-copy-to-clipboard.svg)](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/ExmCopyToClipboard.html)
57
+
58
+ - [Demo](https://exmg.github.io/exmachina-web-components/demo/?el=exm-copy-to-clipboard)
@@ -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 ExmCopyToClipboard 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': ExmCopyToClipboard;
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 ExmCopyToClipboard = class ExmCopyToClipboard 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
+ ExmCopyToClipboard.styles = [style];
106
+ __decorate([
107
+ property({ type: String })
108
+ ], ExmCopyToClipboard.prototype, "value", void 0);
109
+ __decorate([
110
+ state()
111
+ ], ExmCopyToClipboard.prototype, "isCopySupported", void 0);
112
+ __decorate([
113
+ state()
114
+ ], ExmCopyToClipboard.prototype, "bubbles", void 0);
115
+ __decorate([
116
+ query('#clipboard')
117
+ ], ExmCopyToClipboard.prototype, "clipboard", void 0);
118
+ ExmCopyToClipboard = __decorate([
119
+ customElement('exm-copy-to-clipboard')
120
+ ], ExmCopyToClipboard);
121
+ export { ExmCopyToClipboard };
122
+ //# sourceMappingURL=exm-copy-to-clipboard.js.map
@@ -0,0 +1,2 @@
1
+ export { ExmCopyToClipboard } from './exm-copy-to-clipboard.js';
2
+ export { style as copyToClipboardStyles } from './styles/exm-copy-to-clipboard-styles-css.js';
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { ExmCopyToClipboard } from './exm-copy-to-clipboard.js';
2
+ export { style as copyToClipboardStyles } from './styles/exm-copy-to-clipboard-styles-css.js';
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ export declare const style: import("lit").CSSResult;
@@ -0,0 +1,11 @@
1
+ import { css } from 'lit';
2
+ export const style = css `
3
+ :host {
4
+ display: inline-block;
5
+ }
6
+
7
+ #clipboard {
8
+ display: none;
9
+ }
10
+ `;
11
+ //# sourceMappingURL=exm-copy-to-clipboard-styles-css.js.map
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "@exmg/exm-copy-to-clipboard",
3
+ "version": "0.0.2-alpha.0+df24e0c",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "module": "dist/index.js",
8
+ "exports": {
9
+ ".": "./dist/index.js",
10
+ "./exm-copy-to-clipboard.js": "./dist/exm-copy-to-clipboard.js"
11
+ },
12
+ "peerDependencies": {
13
+ "@exmg/lit-base": "^3.0.3",
14
+ "lit": "^3.2.1",
15
+ "tslib": "^2.6.2"
16
+ },
17
+ "files": [
18
+ "**/*.scss",
19
+ "**/*.js",
20
+ "**/*.d.ts"
21
+ ],
22
+ "keywords": [
23
+ "web-components",
24
+ "lit",
25
+ "clipboard"
26
+ ],
27
+ "homepage": "https://bitbucket.org/exmachina/exm-web-components",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git@bitbucket.org:exmachina/exm-web-components.git",
31
+ "directory": "packages/exm-copy-to-clipboard"
32
+ },
33
+ "license": "MIT",
34
+ "scripts": {},
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "gitHead": "df24e0c74a76a7e1c258f69386036e24e7860256"
39
+ }