File size: 585 Bytes
a43df2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import {
  LitElement,
  html,
} from 'https://cdn.jsdelivr.net/gh/lit/dist@3/core/lit-core.min.js';

class CopyToClipboard extends LitElement {
  static properties = {
    text: {type: String},
  };

  constructor() {
    super();
    this.text = '';
  }

  render() {
    return html`
      <div @click="${this._onClick}">
        <slot></slot>
      </div>
    `;
  }

  _onClick() {
    navigator.clipboard
      .writeText(this.text)
      .catch((err) => console.error('Failed to copy text: ', err));
  }
}

customElements.define('copy-to-clipboard-component', CopyToClipboard);