Code Monkey home page Code Monkey logo

rn-quick-actions's Introduction

rn-quick-actions

Quick Actions(iOS) & App Shortcuts(Android) for React Native

Installation

yarn add rn-quick-actions

Setup

iOS

If you are using cocoapods - you may need to run pod install (from ios directory).

On iOS, Quick Actions are handled by the application:performActionForShortcutItem:completionHandler method of your app's UIApplicationDelegate (i.e. AppDelegate.m), so, you will therefore need to add the following code in your project's AppDelegate.m) file.

// add on top of the file
#import "RNShortcuts.h"
// ...

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
    [RNShortcuts performActionForShortcutItem:shortcutItem completionHandler:completionHandler];
}

Android

Configure the Kotlin version in project's build.gradle file.

// eg:
buildscript {
  ext {
    buildToolsVersion = "31.0.0"
    minSdkVersion = 21
    compileSdkVersion = 32
    targetSdkVersion = 26
    kotlinVersion = "1.6.0"
  }
}

Usage

Documentation

Imports

import Shortcuts from 'rn-quick-actions';
// if using typescript, can also use the 'ShortcutItem' type
import Shortcuts, { ShortcutItem } from 'rn-quick-actions';

Initial shortcut / action

Get the initial shortcut that the app was launched with. On iOS this will be returned just once, subsequent calls will return null.

const shortcutItem = await Shortcuts.getInitialShortcut();

Listen for shortcut / action invocations

Listen to shortcut / action invocations while app is running.

On iOS the listener is also called for the initial invocation, unless it was already received via Shortcuts.getInitialShortcut().

// 1. define the handler
function handler(item) {
  const { type, data } = item;
  // your handling logic
}

// 2. add the listener in a `useEffect` hook or `componentDidMount`
const sub = Shortcuts.onShortcutPressed(handler);

// 3. remove the listener in a `useEffect` hook or `componentWillUnmount`
sub.remove();

Set shortcuts

To set shortcuts (will replace existing dynamic actions / shortcuts)

const shortcutItem = {
  id: 'my.awesome.action',
  title: 'Do awesome things',
  shortTitle: 'Do it',
  subtitle: 'iOS only',
  iconName: 'ic_awesome',
  symbolName: 'house.fill', // SF Symbol Name (iOS only)
  data: {
    foo: 'bar',
  },
};

Shortcuts.setShortcuts([shortcutItem]);

// you can also `await` to get the current dynamic shortcuts / actions
const shortcutItems = await Shortcuts.setShortcuts([shortcutItem]);

Clear shortcuts

Clears all dynamic shortcuts.

Shortcuts.clearShortcuts();

Get shortcuts

Get the current shortcuts. Some information may be lost, such as iconName, data, etc.

const shortcutItems = await Shortcuts.getShortcuts();

Example

import { useEffect } from 'react';
import Shortcuts from 'rn-quick-actions';
import { Scan, Search } from '@/components';

export default function useShortcuts() {
  useEffect(() => {
    const shortcutsItems = [
      {
        type: 'scan',
        title: ' Scan',
        iconName: 'md_scan',
        data: {},
      },
      {
        type: 'search',
        title: 'Search',
        iconName: 'md_search',
        data: {},
      },
    ];

    Shortcuts.setShortcuts(shortcutsItems.reverse());

    const handler = (item) => {
      const { type } = item || {};
      if (type === 'scan') {
        Scan();
      }
      if (type === 'search') {
        Search();
      }
    };

    const sub = Shortcuts.onShortcutPressed(handler);
    return () => {
      sub.remove();
    };
  }, []);
}

Icons

To display icons with your shortcuts / actions you will need to add them to your project. Once added use the name of your iOS asset or Android drawable as the value for iconName above. You can also use SF Symbol Name like house.fill or globe.europe.africa for symbolName above (iOS only). If symbolName is filled, iconName is not taken into account.

iOS - Asset catalog

Add new assets to your Asset catalog by importing either png (scalar) or pdf (vector) files.

Refer Custom Icons : Home Screen Quick Action Icon Size to learn about the dimensions and design specifications.

Android - drawable

Add drawable resources to you Android project. In Android studio, choose:

  • for vector icons (SVG / PDF): File > New > Vector Asset

  • for scalar icons (PNG): File > New > Image Asset

Refer App Shortcuts: Icon design guidelines to learn about the dimensions and design specifications.

Contribution

If you want to add some features, feel free to submit PR. See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT.

rn-quick-actions's People

Contributors

yorickshan avatar

Stargazers

 avatar  avatar Zeema avatar

Watchers

 avatar

rn-quick-actions's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.