API
nsh/clipboard
nsh/clipboard reads and writes the text on the system clipboard: UIPasteboard on iOS, ClipboardManager on Android, navigator.clipboard on the web.
import { Clipboard } from "nsh/clipboard";
const Clipboard: {
setString(text: string): void;
getString(): Promise<string>;
hasString(): Promise<boolean>;
};
The user can also copy from a selectable text themselves; Clipboard is for copying from code, such as a Copy button.
#Clipboard.setString
setString(text: string): void;
Puts text on the clipboard, replacing what was there.
import { signal, Text, View } from "nsh";
import { Clipboard } from "nsh/clipboard";
const copied = signal(false);
<View
onPress={() => {
Clipboard.setString(code);
copied.set(true);
}}
>
<Text>{copied() ? "Copied" : "Copy"}</Text>
</View>;
#Clipboard.getString
getString(): Promise<string>;
Resolves with the clipboard’s text, or "" when it holds none. On iOS, reading the clipboard can show the system’s paste notice (“App pasted from …”), and on Android 10 and later only the app in the foreground can read it. Read it when the user asks to paste, not on every screen.
const pasted = await Clipboard.getString();
if (pasted) code.set(pasted.trim());
#Clipboard.hasString
hasString(): Promise<boolean>;
Resolves with whether the clipboard holds text, without reading it: no paste notice on iOS. Use it to decide whether to show a Paste button. On the web, browsers only tell by reading, so it may ask the user for permission.
const canPaste = signal(false);
Clipboard.hasString().then((has) => canPaste.set(has));