Newby Coder header banner

React Native Draggable Action Button

A Draggable action button functions as a button and can be dragged

react-native-draggable package can be used to implemented this, which also allows custom bounds (i.e. along a line or within a boundary)


Creating new React Native App

Check React Native Installation for installation related info

Use react-native init command to create a new React Native project (here named NcDraggable)

react-native init NcDraggable

This creates a directory named NcDraggable and initializes it as a react native application directory

This can be skipped in case an existing react native project is being integrated into


Dependency

Go to project directory (here NcDraggable)

cd NcDraggable

Add react-native-device-info package to current project using yarn

yarn add react-native-draggable

or using npm

npm install react-native-draggable --save

iOS

After yarn add command, cd into ios folder inside project directory

cd ios

Use pod command to install any required CocoaPods dependencies:

pod install

Usage

Import
import Draggable from 'react-native-draggable';

Example usage

return (
    <View >
        <Draggable x={75} y={100} renderSize={56} renderColor='black' renderText='A' isCircle shouldReverse onShortPressRelease={()=>alert('touched!!')}/>
        <Draggable x={200} y={300} renderColor='red' renderText='B'/>
        <Draggable/>
    <Draggable x={50} y={50}>
        <CustomComponent/>
    </Draggable>
    </View>
);

Properties

PropTypeExampleDefaultDescription
renderTextstring'ANY''+'text of draggable
isCirclebool{true}---render as circle
renderSizenumber{36}{36}draggable size
imageSourcesourcerequire('./img/xxx.png')---image source
renderColorstring'black'---Colors
childrenComponent<Text>Sup</Text>---children to render as draggable
shouldReversebool{false}{false}should draggable spring back to start when released
disabledbool{false}{false}should draggable be disabled
debugbool{false}{false}should show a debug visualization
touchableOpacityPropsObject{ activeOpactiy: .1 }---props passed to TouchableOpacity component
animatedViewPropsObject{ accessibilityHint: 'drag' }---props passed to Animated.View component
xnumber{0}0initial position x
ynumber{0}0initial position y
znumber{1}1z-index / elevation
minXnumber{0}---min X value for left edge of component
minYnumber{0}---min Y value for top edge of component
maxXnumber{0}---max X value for right edge of component
maxYnumber{0}---max Y value for bottom edge of component

Events

Event callbacks whose arguments can be skipped

EventTypeArgumentsDescription
onDragfuncevent, gestureStatecalled every frame component is moved
onShortPressReleasefunceventcalled when a press is released that isn't a long press or drag
onDragReleasefuncevent, gestureState, boundscalled when a drag is released
onLongPressfunceventcalled when a long press is started
onPressInfunceventcalled when a press is started
onPressOutfunceventcalled when a press is stopped, or the component is dragged
onReleasefuncevent, wasDraggingcalled at the end of interaction, regardless if press or drag
 onReverse  func   called when a drag is released, if shouldReverse is true 

App Code

App.js
import React from 'react';
import {View, Button, Dimensions} from 'react-native';
import Draggable from 'react-native-draggable';
const {width, height} = Dimensions.get('window');

export default function App() {

  const changeFace = () => {
    console.log('drag release');
    setSource(require('./img/trump2.png'));
  };

  return (
    <View>
      <Draggable
        debug
        renderColor="red"
        renderSize={64}
        x={width * 0.75}
        y={height - 100}
        minX={width / 2}
        maxX={width}
        minY={height - 200}
        maxY={height - 20}
        renderText="💩"
      />
      <Draggable
        debug
        renderSize={128}
        renderColor="black"
        x={200}
        y={300}
        minX={0}
        maxX={width}
        minY={300}
        maxY={300}
        renderText="Constrained Slider"
        onShortPressRelease={() => alert('clicked')}
      />
      <Draggable debug y={20} z={5} renderColor={'blue'} />
    </View>
  );
}

Run instructions

Running in Android

cd into project directory (here NcDraggable)

cd NcDraggable

Run metro server to serve js

react-native start

Go to project directory in another terminal tab

Enter following run command for Android:

react-native run-android
cl-react-native-draggable-btn

Running in ios

cd into project directory (here NcDraggable)

cd NcDraggable

Enter following command :

react-native run-ios

This might take some time


If the app shows error about metro server not configured (check Running an App in iOS), then run metro server in another tab:

react-native start

Reload the app

Re-run react-native run-ios command if reloading doesn't work

cm-react-native-draggable-button-as1cm-react-native-draggable-button-as2cm-react-native-draggable-button-as3