UNPKG

1.86 kBTypeScriptView Raw
1/// <reference lib="dom"/>
2
3type Action = 'cut' | 'copy';
4type Response = 'success' | 'error';
5
6type Options = {
7 text?: string;
8 action?: Action;
9 target?: Element;
10 container?: Element;
11};
12
13/**
14 * Base class which takes one or more elements, adds event listeners to them,
15 * and instantiates a new `ClipboardAction` on each click.
16 */
17declare class ClipboardJS {
18 constructor(
19 selector: string | Element | NodeListOf<Element>,
20 options?: ClipboardJS.Options
21 );
22
23 /**
24 * Subscribes to events that indicate the result of a copy/cut operation.
25 * @param type Event type ('success' or 'error').
26 * @param handler Callback function.
27 */
28 on(type: Response, handler: (e: ClipboardJS.Event) => void): this;
29
30 on(type: string, handler: (...args: any[]) => void): this;
31
32 /**
33 * Clears all event bindings.
34 */
35 destroy(): void;
36
37 /**
38 * Checks if clipboard.js is supported
39 */
40 static isSupported(): boolean;
41}
42
43declare namespace ClipboardJS {
44 interface Options {
45 /**
46 * Overwrites default command ('cut' or 'copy').
47 * @param elem Current element
48 */
49 action?(elem: Element): Action;
50
51 /**
52 * Overwrites default target input element.
53 * @param elem Current element
54 * @returns <input> element to use.
55 */
56 target?(elem: Element): Element;
57
58 /**
59 * Returns the explicit text to copy.
60 * @param elem Current element
61 * @returns Text to be copied.
62 */
63 text?(elem: Element): string;
64
65 /**
66 * For use in Bootstrap Modals or with any
67 * other library that changes the focus
68 * you'll want to set the focused element
69 * as the container value.
70 */
71 container?: Element;
72 }
73
74 interface Event {
75 action: string;
76 text: string;
77 trigger: Element;
78 clearSelection(): void;
79 }
80}
81
82export = ClipboardJS;
83
84export as namespace ClipboardJS;