svelte-copy
Advanced tools
Comparing version 1.2.4 to 1.3.0
export declare const copyText: (text: string) => Promise<void>; | ||
export declare const copy: (element: HTMLElement, text: string) => { | ||
update: (t: string) => string; | ||
interface Parameters { | ||
text: string; | ||
events?: string | string[]; | ||
} | ||
export declare const copy: (element: HTMLElement, params: Parameters | string) => { | ||
update: (newParams: Parameters | string) => void; | ||
destroy: () => void; | ||
}; | ||
export {}; |
31
copy.js
@@ -24,4 +24,4 @@ export const copyText = async (text) => { | ||
}; | ||
export const copy = (element, text) => { | ||
async function click() { | ||
export const copy = (element, params) => { | ||
async function handle() { | ||
if (text) | ||
@@ -36,7 +36,28 @@ try { | ||
} | ||
element.addEventListener('click', click, true); | ||
let events = typeof params == 'string' ? ['click'] : [params.events].flat(1); | ||
let text = typeof params == 'string' ? params : params.text; | ||
events.forEach((event) => { | ||
element.addEventListener(event, handle, true); | ||
}); | ||
return { | ||
update: (t) => (text = t), | ||
destroy: () => element.removeEventListener('click', click, true), | ||
update: (newParams) => { | ||
const newEvents = typeof newParams == 'string' ? ['click'] : [newParams.events].flat(1); | ||
const newText = typeof newParams == 'string' ? newParams : newParams.text; | ||
const addedEvents = newEvents.filter((x) => !events.includes(x)); | ||
const removedEvents = events.filter((x) => !newEvents.includes(x)); | ||
addedEvents.forEach((event) => { | ||
element.addEventListener(event, handle, true); | ||
}); | ||
removedEvents.forEach((event) => { | ||
element.removeEventListener(event, handle, true); | ||
}); | ||
events = newEvents; | ||
text = newText; | ||
}, | ||
destroy: () => { | ||
events.forEach((event) => { | ||
element.removeEventListener(event, handle, true); | ||
}); | ||
}, | ||
}; | ||
}; |
{ | ||
"name": "svelte-copy", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"repository": { | ||
@@ -14,10 +14,12 @@ "type": "git", | ||
"devDependencies": { | ||
"@sveltejs/adapter-vercel": "next", | ||
"@sveltejs/kit": "next", | ||
"ghostsui": "^1.3.3", | ||
"svelte": "^3.48.0", | ||
"svelte-preprocess": "^4.10.6", | ||
"svelte2tsx": "^0.5.9", | ||
"@sveltejs/adapter-vercel": "1.0.0-next.77", | ||
"@sveltejs/kit": "1.0.0-next.510", | ||
"@sveltejs/package": "^1.0.0-next.5", | ||
"ghostsui": "^1.5.0", | ||
"svelte": "^3.50.1", | ||
"svelte-preprocess": "^4.10.7", | ||
"svelte2tsx": "^0.5.19", | ||
"tslib": "^2.4.0", | ||
"typescript": "^4.6.4" | ||
"typescript": "^4.8.4", | ||
"vite": "^3.1.5" | ||
}, | ||
@@ -24,0 +26,0 @@ "type": "module", |
@@ -22,3 +22,3 @@ # Svelte Copy | ||
<button use:copy={"Hello World"}> | ||
<button use:copy={'Hello World'}> | ||
Click me! | ||
@@ -55,2 +55,14 @@ </button> | ||
# Custom Triggers | ||
By default, copy action is fired on click event. You can change this behavior by passing an object with the name or names of the events that you want to trigger the copy action. | ||
```html | ||
<button | ||
use:copy={{ text: 'Hello' , events: ['touchstart', 'mouseenter']}} | ||
on:svelte-copy={(event) => alert(event.detail)}> | ||
Move cursor over button or touch button to cause alert on copy | ||
</button> | ||
``` | ||
# Support | ||
@@ -57,0 +69,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
5546
80
70
10