Clipboard
Clipboard reads and writes the system pasteboard, bound to expo-clipboard.
import { Clipboard } from '@ng-native/expo';
inject(Clipboard)Platforms. Available on macOS, Linux, iPadOS, iPhone and Android. Absent on Web host, where available() is false.
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 { Pressable, Text } from '@ng-native/components';
import { Clipboard } from '@ng-native/expo/clipboard';
@Component({
selector: 'app-share-link',
imports: [Pressable, Text],
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.
On Linux the GTK host's clipboard is the desktop's, through GTK. GTK reads another app's text
asynchronously, so changes moves once that text has been read, and read() answers 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
On iOS and Android, a missing expo-clipboard - never installed, or installed without the app being
rebuilt since - leaves the service made, so injecting it does not throw. A call, or a read of a
value only the module knows, throws a MissingModuleError. Its message names the module and the
commands that fix it; see
Using a module.
On the web, and in a test that provides no fake, 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.