UNPKG

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