Skip to content

feat(cdk-experimental/clipboard): add better support for iOS devices, preliminary documentations #16830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/cdk-experimental/clipboard/clipboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
**Warning: this service is still experimental. It may have bugs and the API may change at any
time**

The clipboard package provides helpers for working with the system clipboard.

## `Clipboard` service

The `Clipboard` service copies text to the
user's clipboard. It has two methods: `copy` and `beginCopy`. For cases where
you are copying a relatively small amount of text, you can call `copy` directly
to place it on the clipboard.

```typescript
class HeroProfile {
constructor(private clipboard: Clipboard) {}

copyHeroName() {
this.clipboard.copy('Alphonso');
}
}
```

However, for longer text the browser needs time to fill an intermediate
textarea element and copy the content. Directly calling `copy` may fail
in this case, so you can pre-load the text by calling `beginCopy`. This method
returns a `PendingCopy` object that has a `copy` method to finish copying the
text that was buffered. Please note, if you call `beginCopy`, you must
clean up the `PendingCopy` object by calling `destroy` on it after you are
finished.

```typescript
class HeroProfile {
lifetimeAchievements: string;

constructor(private clipboard: Clipboard) {}

copyAchievements() {
const pending = this.clipboard.beginCopy(this.lifetimeAchievements);
let remainingAttempts = 3;
const attempt = () => {
const result = pending.copy();
if (!result && --remainingAttempts) {
setTimeout(attempt);
} else {
// Remember to destroy when you're done!
pending.destroy();
}
}
setTimeout(attempt);
}
}
```

## `cdkCopyToClipboard` directive

The `cdkCopyToClipboard` directive can be used to easily add copy-on-click
functionality to an existing element. The directive selector doubles as an
`@Input()` for the text to be copied.

```html
<img src="avatar.jpg" alt="Hero avatar" [cdkCopyToClipboard]="getShortBio()">
```
1 change: 1 addition & 0 deletions src/cdk-experimental/clipboard/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export class PendingCopy {
const currentFocus = document.activeElement;

textarea.select();
textarea.setSelectionRange(0, textarea.value.length);
successful = this._document.execCommand('copy');

if (currentFocus instanceof HTMLElement) {
Expand Down