copy-component
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,8 +0,2 @@ | ||
const copyText = async (elements) => { | ||
// Copy to the clipboard | ||
const doc = document.createDocumentFragment(); | ||
const element = document.createElement("div"); | ||
doc.appendChild(element); | ||
elements.forEach((el) => element.appendChild(el.cloneNode(true))); | ||
const copyText = async (element) => { | ||
if ("clipboard" in navigator) { | ||
@@ -92,3 +86,4 @@ try { | ||
try { | ||
await copyText(slottedElements); | ||
// cant support multiple elements due to formatting problems with the other slots | ||
await copyText(slottedElements[0]); | ||
this.dispatchEvent( | ||
@@ -95,0 +90,0 @@ new CustomEvent("copy", { |
{ | ||
"name": "copy-component", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A vanilla custom element that adds the capability to wrap html so it the inner text can be added to the users clipboard", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,2 +13,3 @@ # copy-component | ||
- Browser support: Modern, requires support for Web Component APIs (v1). | ||
- Limitation: In order to ensure that formatting can be copied this component requires you to [wrap your copyable content in a single element)[#limitations]. | ||
@@ -48,2 +49,30 @@ [Live Demos](https://griffa.dev/demos/copy-component/) | ||
## Limitations | ||
In order to preserve the formatting of copied content you must wrap your content you want to copy in single parent dom node i.e. | ||
```html | ||
<copy-component> | ||
<div> | ||
<p>Stuff to copy</p> | ||
<br /> | ||
<p>Copy me too!</p> | ||
</div> | ||
<button slot="button">Copy me!</button> | ||
</copy-component> | ||
``` | ||
NOT | ||
```html | ||
<copy-component> | ||
<p>Stuff to copy</p> | ||
<br /> | ||
<p>Copy me too!</p> | ||
<button slot="button">Copy me!</button> | ||
</copy-component> | ||
``` | ||
This is because due to how shadow dom works, it is not possible to get the formatted inner text of a single slot without also getting the text of the other slots (e.g. the button slot). | ||
## Programmatic API | ||
@@ -50,0 +79,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6073
107
103