Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
let Clipboard: { setString: (string: string) => void } | undefined;
let Clipboard:
| {
setString?: (text: string) => void;
setStringAsync?: (text: string) => Promise<boolean>;
}
| undefined;

try {
Clipboard = require('expo-clipboard');
Expand All @@ -12,6 +17,23 @@ if (!Clipboard) {
);
}

export const setClipboardString = Clipboard
? (string: string) => Clipboard?.setString(string)
: null;
export const setClipboardString =
Clipboard && (Clipboard.setStringAsync || Clipboard.setString)
? (text: string) => {
try {
// `expo-clipboard` removed the synchronous `setString` in favour of
// the async API. Keep this handler synchronous (void return, like the
// legacy `setString`) and fire the async write without awaiting. Fall
// back to `setString` for older versions.
if (Clipboard?.setStringAsync) {
Clipboard.setStringAsync(text).catch((error: unknown) => {
console.log('Copying to clipboard failed...', error);
});
} else {
Clipboard?.setString?.(text);
}
} catch (error) {
console.log('Copying to clipboard failed...', error);
}
}
: null;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let Clipboard: { setString: (string: string) => void } | undefined;
let Clipboard: { setString?: (text: string) => void } | undefined;

try {
Clipboard = require('@react-native-clipboard/clipboard').default;
Expand All @@ -7,6 +7,12 @@ try {
console.log('@react-native-clipboard/clipboard is not installed');
}

export const setClipboardString = Clipboard
? (string: string) => (Clipboard ? Clipboard.setString(string) : {})
export const setClipboardString = Clipboard?.setString
? (text: string) => {
try {
Clipboard?.setString?.(text);
} catch (error) {
console.log('Copying to clipboard failed...', error);
}
}
: null;
4 changes: 3 additions & 1 deletion package/src/native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ type SaveFileOptions = {
};
type SaveFile = (options: SaveFileOptions) => Promise<string> | never;

type SetClipboardString = (text: string) => Promise<void> | never;
// Kept synchronous (void return) for backwards compatibility. The expo
// implementation fires the async clipboard write without awaiting it.
type SetClipboardString = (text: string) => void;

type ShareOptions = {
type?: string;
Expand Down
Loading