Clipboard
Clipboard reads and writes the system pasteboard, bound to expo-clipboard.
import { Clipboard } from '@ng-native/expo';
inject(Clipboard)This uses expo-clipboard's own JavaScript rather than its native module directly, because it does
real work: it carries the event name the change listener subscribes to, and the option defaults
each call expects.
Install
npx expo install expo-clipboardimport { Clipboard } from '@ng-native/expo/clipboard';The smallest useful example
import { Component, inject } from '@angular/core';
import { Clipboard } from '@ng-native/expo/clipboard';
@Component({
selector: 'app-share-link',
template: `<pressable (press)="copy()"><text>Copy link</text></pressable>`,
})
export class ShareLink {
private readonly clipboard = inject(Clipboard);
protected async copy(): Promise<void> {
await this.clipboard.write('https://example.com');
}
}What it does
changes- a signal: how many times the pasteboard has changed since the app started reading it. It counts changes rather than holding the text, and that is deliberate: on iOS 16 and later, reading the clipboard is what prompts the user for permission. A signal that held the contents would prompt on every change - including changes made by other apps - which is both a poor experience and a good way to have the read denied. The notification is free; the read stays explicit.read()- resolves to what is on the clipboard. On iOS 16+, this is the call that prompts for permission.write(text)- puts text on the clipboard. Resolves once native has it.
The change listener lives as long as the app does and is removed when the app is destroyed.
Clipboard is a root service, which makes it one per app rather than one per process: an app that
is mounted and unmounted - by a test's unmount(), a reload, or a host that embeds it - gets a
fresh listener each time and leaves none behind.
Without the module installed
changes stays at 0. read() resolves to an empty string. write() resolves without doing
anything.
Reference
Clipboardservice@ng-native/expoimport { Clipboard } from '@ng-native/expo';changes counts pasteboard changes rather than holding the contents, and that is deliberate: on iOS 16 and later, *reading* the clipboard prompts the user for permission. A signal that held the text would prompt on every change, including changes made by other apps, which is both a terrible experience and a good way to have the read denied. So the notification is free and the read is explicit.
Methods
read(): Promise<string>What is on the clipboard. On iOS 16+ this is what asks the user for permission.
write(text: string): Promise<void>Put text on the clipboard. Resolves once native has it.