UNPKG

8.44 kBMarkdownView Raw
1[![Financial Contributors on Open Collective](https://opencollective.com/ngx-clipboard/all/badge.svg?label=financial+contributors)](https://opencollective.com/ngx-clipboard)
2[![ngx-clipboard](https://github.com/maxisam/ngx-clipboard/actions/workflows/ngx-clipboard.yml/badge.svg)](https://github.com/maxisam/ngx-clipboard/actions/workflows/ngx-clipboard.yml)
3[![npm](https://img.shields.io/npm/dt/ngx-clipboard.svg?style=flat-square)](https://www.npmjs.com/package/ngx-clipboard)
4[![GitHub release](https://img.shields.io/github/release/maxisam/ngx-clipboard.svg?style=flat-square)](https://github.com/maxisam/ngx-clipboard/releases)
5[![npm](https://img.shields.io/npm/l/ngx-clipboard.svg?style=flat-square)]()
6
7# ngx-clipboard , F.K.A [angular2-clipboard](https://www.npmjs.com/package/angular2-clipboard)
8
9From 6.0.0, there is no other JS dependency anymore. Just Angular.
10
11It works with angular version 2.0.0 and up
12
13To make more sense with the future versioning scheme of Angular, the directive selector is now rename to **ngxClipboard**
14
15## Dependencies
16
17- If you need to use it on 2.x, please use version 7.x.x.
18- If you need to use it on 4.x, please use version 8.x.x.
19- If you need to use it on 5.x, please use version 10.x.x.
20- If you need to use it on 8.x, please use version 12.x.x.
21- If you need to use it on 9.x, please use version 13.x.x.
22- If you need to use it on 10.x - 12.x, please use version 14.0.2.
23- If you need to use it on 13.x, please use version 15.x.x. (Also thanks https://github.com/arturovt for updating & tuning)
24
25The code are pretty much the same, in v8.0.0 it uses InjectionToken which requires angular4 and above.
26
27## Install
28
29You can get it on npm.
30
31```bat
32npm install ngx-clipboard --save
33```
34
35Open your module file e.g `app.module.ts` and update **imports** array
36
37```ts
38import { ClipboardModule } from 'ngx-clipboard';
39...
40imports: [
41...
42 ClipboardModule,
43...
44]
45```
46
47## Usage
48
49If you use SystemJS to load your files, you might have to update your config:
50
51```js
52System.config({
53 map: {
54 'ngx-clipboard': 'node_modules/ngx-clipboard'
55 }
56});
57```
58
59### Copy source
60
61This library support multiple kinds of copy source.
62
63- Setting `cbContent` attribute
64
65```html
66<button ngxClipboard [cbContent]="'target string'">Copy</button>
67```
68
69You can assign the parent **container** to avoid focus trapper issue, #145
70
71```html
72<div #container>
73 <button ngxClipboard [cbContent]="'target string'" [container]="container">Copy</button>
74</div>
75```
76
77- Setting an input target
78
79```html
80....
81
82<input type="text" #inputTarget />
83
84<button [ngxClipboard]="inputTarget">Copy</button>
85```
86
87- Using `copy` from `ClipboardService` to copy any text you dynamically created.
88
89```ts
90import { ClipboardService } from 'ngx-clipboard'
91
92...
93
94constructor(private _clipboardService: ClipboardService){
95...
96}
97
98copy(text: string){
99 this._clipboardService.copy(text)
100}
101```
102
103### Callbacks
104
105- `cbOnSuccess` callback attribute is triggered after copy was successful with `$event: {isSuccess: true, content: string}`
106
107```html
108<button (cbOnSuccess)="copied($event)" [cbContent]="'example string'">Copied</button>
109```
110
111Or updating parameters directly like so
112
113```html
114<button (cbOnSuccess)="isCopied = true" [cbContent]="'example string'">Copied</button>
115```
116
117- `cbOnError` callback attribute is triggered when there's failure in copying with `$event:{isSuccess: false}`
118
119### Conditionally render host
120
121You can also use the structural directive \*ngxClipboardIfSupported to conditionally render the host element
122
123```html
124<button ngxClipboard *ngxClipboardIfSupported [cbContent]="'target string'" (cbOnSuccess)="isCopied = true">
125 Copy
126</button>
127```
128
129_Special thanks to @surajpoddar16 for implementing this feature_
130
131### Handle copy response globally
132
133To handle copy response globally, you can subscribe to `copyResponse$` exposed by the `ClipboardService`
134
135```
136export class ClipboardResponseService {
137 constructor(
138 private _clipboardService: ClipboardService,
139 private _toasterService: ToasterService
140 ) {
141 this.handleClipboardResponse();
142 }
143
144 handleClipboardResponse() {
145 this._clipboardService.copyResponse$.subscribe((res: IClipboardResponse) => {
146 if (res.isSuccess) {
147 this._toasterService.pop('success', undefined, res.successMessage);
148 }
149 });
150 }
151}
152```
153
154_Special thanks to @surajpoddar16 for implementing this feature_
155
156### Clean up temporary textarea used by this module after each copy to clipboard
157
158This library creates a textarea element at the root of the body for its internal use. By default it only destroys it when the directive is destroyed. If you'd like it to be destroyed after each copy to clipboard, provide root level module configuration like this:
159
160```ts
161ClipboardService.configure({ cleanUpAfterCopy: true });
162```
163
164Special thanks to [@DmitryEfimenko](https://github.com/DmitryEfimenko) for implementing this feature
165
166## Example
167
168[stackblitz.com](https://stackblitz.com/github/maxisam/ngx-clipboard)
169
170## Build project
171
172```cmd
173npm i && npm run build
174```
175
176To run demo code locally
177
178`npm run start`
179
180## Contributing
181
182- Your commits conform to the conventions established [here](https://github.com/conventional-changelog/conventional-changelog-angular/blob/master/convention.md)
183
184## Troubleshooting
185
186Please ask your general questions at https://stackoverflow.com/questions/tagged/ngx-clipboard
187
188## Shoutouts 🙏
189
190Kudos to
191
192[Thierry Templier](http://stackoverflow.com/a/36330518/667767) This project is inspired by his answer on StackOverflow.
193
194The core function is ported from [clipboard.js](http://zenorocha.github.io/clipboard.js/) by [@zenorocha](https://twitter.com/zenorocha).
195
196This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.
197
198<img src="https://www.browserstack.com/images/layout/browserstack-logo-600x315.png" height="80" title="BrowserStack Logo" alt="BrowserStack Logo" />
199
200Big thanks to [BrowserStack](https://www.browserstack.com) for letting the maintainers use their service to debug browser issues.
201
202## Contributors
203
204### Code Contributors
205
206This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
207<a href="https://github.com/maxisam/ngx-clipboard/graphs/contributors"><img src="https://opencollective.com/ngx-clipboard/contributors.svg?width=890&button=false" /></a>
208
209### Financial Contributors
210
211Become a financial contributor and help us sustain our community. [[Contribute](https://opencollective.com/ngx-clipboard/contribute)]
212
213#### Individuals
214
215<a href="https://opencollective.com/ngx-clipboard"><img src="https://opencollective.com/ngx-clipboard/individuals.svg?width=890"></a>
216
217#### Organizations
218
219Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/ngx-clipboard/contribute)]
220
221<a href="https://opencollective.com/ngx-clipboard/organization/0/website"><img src="https://opencollective.com/ngx-clipboard/organization/0/avatar.svg"></a>
222<a href="https://opencollective.com/ngx-clipboard/organization/1/website"><img src="https://opencollective.com/ngx-clipboard/organization/1/avatar.svg"></a>
223<a href="https://opencollective.com/ngx-clipboard/organization/2/website"><img src="https://opencollective.com/ngx-clipboard/organization/2/avatar.svg"></a>
224<a href="https://opencollective.com/ngx-clipboard/organization/3/website"><img src="https://opencollective.com/ngx-clipboard/organization/3/avatar.svg"></a>
225<a href="https://opencollective.com/ngx-clipboard/organization/4/website"><img src="https://opencollective.com/ngx-clipboard/organization/4/avatar.svg"></a>
226<a href="https://opencollective.com/ngx-clipboard/organization/5/website"><img src="https://opencollective.com/ngx-clipboard/organization/5/avatar.svg"></a>
227<a href="https://opencollective.com/ngx-clipboard/organization/6/website"><img src="https://opencollective.com/ngx-clipboard/organization/6/avatar.svg"></a>
228<a href="https://opencollective.com/ngx-clipboard/organization/7/website"><img src="https://opencollective.com/ngx-clipboard/organization/7/avatar.svg"></a>
229<a href="https://opencollective.com/ngx-clipboard/organization/8/website"><img src="https://opencollective.com/ngx-clipboard/organization/8/avatar.svg"></a>
230<a href="https://opencollective.com/ngx-clipboard/organization/9/website"><img src="https://opencollective.com/ngx-clipboard/organization/9/avatar.svg"></a>