1.14.3 • Published 2 months ago

@react-native-clipboard/clipboard v1.14.3

Weekly downloads
6
License
MIT
Repository
github
Last release
2 months ago

@react-native-clipboard/clipboard

Build Status Version Supports macOS, iOS, Android, and Windows MIT License Lean Core Badge

React Native Clipboard API for macOS, iOS, Android, and Windows.

macOSiOSAndroidWindows

Getting started

Install the library using either Yarn:

yarn add @react-native-clipboard/clipboard

or npm:

npm install --save @react-native-clipboard/clipboard

Link

React Native v0.60+

For iOS, use cocoapods to link the package.

run the following command:

$ npx pod-install

For android, the package will be linked automatically on build.

React Native <= 0.59

run the following command to link the package:

$ react-native link @react-native-clipboard/clipboard

For iOS, make sure you install the pod file.

cd ios && pod install && cd ..

or you could follow the instructions to manually link the project

Upgrading to React Native 0.60+

New React Native comes with autolinking feature, which automatically links Native Modules in your project. In order to get it to work, make sure you unlink Clipboard first:

$ react-native unlink @react-native-clipboard/clipboard

Migrating from the core react-native module

This module was created when the Clipboard API was split out from the core of React Native. To migrate to this module you need to follow the installation instructions above and then change you imports from:

import {Clipboard} from 'react-native';

to:

import Clipboard from '@react-native-clipboard/clipboard';

Example

import React, {useState} from 'react';
import {
  SafeAreaView,
  View,
  Text,
  TouchableOpacity,
  StyleSheet,
} from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';

const App = () => {
  const [copiedText, setCopiedText] = useState('');

  const copyToClipboard = () => {
    Clipboard.setString('hello world');
  };

  const fetchCopiedText = async () => {
    const text = await Clipboard.getString();
    setCopiedText(text);
  };

  return (
    <SafeAreaView style={{flex: 1}}>
      <View style={styles.container}>
        <TouchableOpacity onPress={copyToClipboard}>
          <Text>Click here to copy to Clipboard</Text>
        </TouchableOpacity>
        <TouchableOpacity onPress={fetchCopiedText}>
          <Text>View copied text</Text>
        </TouchableOpacity>

        <Text style={styles.copiedText}>{copiedText}</Text>
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  copiedText: {
    marginTop: 10,
    color: 'red',
  },
});

export default App;

Reference

Methods

Clipboard

getString()

static getString()

Get content of string type, this method returns a Promise, so you can use following code to get clipboard content

async _getContent() {
  var content = await Clipboard.getString();
}

getStrings()

static getStrings()

(iOS only) Get contents of string array type, this method returns a Promise, so you can use following code to get clipboard content

async _getContent() {
  var content = await Clipboard.getStrings();
}

setString()

static setString(content)

Set content of string type. You can use following code to set clipboard content

_setContent() {
  Clipboard.setString('hello world');
}

Parameters

NameTypeRequiredDescription
contentstringYesThe content to be stored in the clipboard

setStrings()

static setStrings(content)

(iOS only) Set content of string array type. You can use following code to set clipboard content

_setContent() {
  Clipboard.setStrings(['hello world', 'second string']);
}

Parameters

NameTypeRequiredDescription
contentstring[]YesThe content to be stored in the clipboard

hasString()

static hasString()

Returns whether the clipboard has content or is empty. Can check if there is a content in clipboard without triggering PasteBoard notification for iOS 14+

hasURL()

static hasURL()

(iOS only) Returns whether the clipboard has a URL content. Can check if there is a URL content in clipboard without triggering PasteBoard notification for iOS 14+

hasNumber()

static hasNumber()

(iOS 14+ only) Returns whether the clipboard has a Number(UIPasteboardDetectionPatternNumber) content. Can check if there is a Number content in clipboard without triggering PasteBoard notification for iOS 14+

hasWebURL()

static hasWebURL()

(iOS 14+ only) Returns whether the clipboard has a WebURL(UIPasteboardDetectionPatternProbableWebURL) content. Can check if there is a WebURL content in clipboard without triggering PasteBoard notification for iOS 14+

useClipboard

useClipboard is a utility hooks for the Clipboard module. data contains the content stored in the clipboard.

import React, {useEffect} from 'react';
import {Text} from 'react-native';
import {useClipboard} from '@react-native-clipboard/clipboard';

export const HooksSample = () => {
  const [data, setString] = useClipboard();

  useEffect(() => {
    setString('hello world');
  }, []);

  return <Text>{data}</Text>;
};

getImage()

static getImage()

Get content of image in base64 string type, this method returns a Promise, so you can use following code to get clipboard content (ANDROID only)

async _getContent() {
  var content = await Clipboard.getImage();
}

Mocking Clipboard

If you're using jest as a test runner, you will need to setup a mock for clipboard, as NativeModules will be undefined when testing with Jest.

Create a jest.setup.js in your project root,and set up the following to have Clipboard mocked:

import mockClipboard from '@react-native-clipboard/clipboard/jest/clipboard-mock.js';

jest.mock('@react-native-clipboard/clipboard', () => mockClipboard);

Maintainers

Contributing

Please see the contributing guide.

License

The library is released under the MIT licence. For more information see LICENSE.

@borderfreefinancial/revoshowmobileui-test@borderfreefinancial/revoshowui-testcoinforbarter-v1-react-nativeuser-native@infinitebrahmanuniverse/nolb-_react-ntelemed-ui-layer@everything-registry/sub-chunk-765@rohitninawe/chat-uikit-react-nativeejadaa-chat-sdkejaada-chat-sdktemporadelectusrytsrn-vayvayrnshellsalmonwalletv2saturnrampscorpiousscorpious_kogorn-kore-bot-sdkrn-kore-bot-sdk-devstablex-sdkvendetta-types@digitalfactory/react-native-componentsreact-native-kora-botsdkmidas_boilerplate_webviewlikeminds_chat_reactnative_integration_betahellojiomodulejitsi-fmtechipanel-rnapp-ajb-corejabo-react-native-super-simple-chathello-jio-react-nativemoor-react-native-componentsopenshare-rnordering-ui-react-nativeordering-ui-native-releaseordering-ui-native-ubanku-releasereact-native-devsetting-apilogreact-native-clipboard-moduleraven-mobile-posreact-native-kore-botsdkreact-native-kore-botsdk-devreact-native-kore-botsdk-qareact-native-kore-botsdk-testreact-native-otp-input-rebornreact-native-otp-input-enhancedreact-native-priv-sdkreact-native-template-rnbasicreact-native-template-with-nativebaseio-mobx-tsreact-native-socket-chatreact-native-netwatchreact-native-agora-chat-uikitpyoncord-typesuilib-docstrudenty-rn-sdk@bytexbyte/berify-scan@acflow/package-example@acflow/react-native-logger-manager@acflow/rnc@dialectlabs/react-native-uiupwork-task@htyf-mp/engines@hongtangyun/react-native-mini-apps-engines@hosseinmd/client@hirbod/react-native-otp-input@gooddollar/good-design@functionland/component-library@functionland/fx-components@eohjsc/react-native-smart-city@eohjsc/react-native-hts@melorra/component@melorra/platform@kafudev/react-native-core@kafudev/react-native-shell@jobber/components-native@ilz5753/rnmd@skedulo/mex-engine-proxy@roll-network/design-system@setpartnerstv/react-native-gpbonus-sdk@cometchat/chat-uikit-react-native@rtarojs/taro-rn@unbound-mod/types@voltmoney/component@voltmoney/platform@yoronsoft/rn-core@yoronsoft/rn-libscoinforbarter-react-nativeautemnostrumdhre-com-libdhre-ui-kitej-chat-sdkegenie-mobile-commonfacilisarchitecto
1.14.3

2 months ago

1.14.2

3 months ago

1.14.1

8 months ago

1.14.0

9 months ago

1.13.2

1 year ago

1.13.1

1 year ago

1.13.0

1 year ago

1.12.1

1 year ago

1.11.2

2 years ago

1.11.1

2 years ago

1.11.0

2 years ago

1.10.0

3 years ago

1.8.5

3 years ago

1.9.0

3 years ago

1.8.4

3 years ago

1.8.3

3 years ago

1.8.2

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.7.0-rc.0

4 years ago

1.6.0

4 years ago