0.1.6 • Published 4 years ago

react-native-media-modal v0.1.6

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

Screenshot

React Native Media Modal is a component that facilitates the creation of a pop-up with media content generally used for communications or in-app marketing.

Installation

npm i react-native-media-modal --save

or

yarn add react-native-media-modal

Usage

import MediaModal from "react-native-media-modal";
<MediaModal ref={ref => { this.MyModal = ref }} />

For the methods works, you should create the reference

Methods

Method NameDescription
openOpen Medial Modal
closeClose Medial Modal

Examples

// Open Modal
function ExampleModalOpen = () => { this.MyModal.open()}

// Close Modal
function ExampleModalClose = () => { this.MyModal.close()}

Props

PropTypeDescriptionDefault
titlestringMedia Modal Content Title""
messagestringMedia Modal message""
textPositionTopbooleanUse title and message above imagefalse
showMediabooleanDisplay Media Blocktrue
mediaImagebooleanLoad Image on Media Block or Webviewtrue
mediaURLstringUrl from Image or Video Embebed if you use Webviewtrue
showCancelbooleanShow a Cancel buttontrue
showConfirmbooleanShow a Confirm buttontrue
textCancelstringText display on Cancel button""
textConfirmstringText display on Confirm button""
backdropClosebooleanClose Media Modal when press on maskfalse
useNativeDriverbooleanAllowing native code to perform the animationfalse
customStylesobjectCustom style to AlertPro{}
onCancelfunctionEvent on Cancel buttonnull
onConfirmfunctionEvent on Confirm buttonnull
onClosefunctionCallback function when modal has closednull
customComponentfunctionInject custom component inside of modalnull

Available Custom Style

customStyles: {
  BackgroundMask: {...}, // Backdrop mask 
  container: {...}, // Modal container 
  title: {...}, // Content title
  message: {...}, //Message text
  media: {...}, // Media 
  closeButton: {...}, // Close button
  closeButtonIcon: {...}, // Close button Icon
  buttonCancel: {...}, // Cancel Button
  textButtonCancel: {...}, // Cancel Button Text
  buttonConfirm: {...}, // Confirm button
  textButtonConfirm: {...} // Confirm button's Text
}

Example with modal customization

import React from 'react';
import { StyleSheet, View, Button } from 'react-native';
import MediaModal from 'react-native-media-modal';

const modalCustomStyles = {
  BackgroundMask: { backgroundColor: 'rgba(255,101,80, 0.4)' },
  container: { backgroundColor: '#e3e3e3' },
  title: { color: 'orange' },
  media: { width: 300 },
  message: { color: 'green' },
  closeButton: { backgroundColor: '#439431' },
  closeButtonIcon: { color: '#000' },
  buttonCancel: { backgroundColor: '#457d34' },
  buttonConfirm: { backgroundColor: '#542862' },
  textButtonCancel: { color: 'purple' },
  textButtonConfirm: { color: 'pink' },
  customComponent: { backgroundColor: '#dadada' }
}

const CustomComponent = <Text>Hello World!</Text>

export default class App extends React.Component {
  constructor(props) {
    super(props);
  }

  // ACTION ON CLOSE MODAL, CANCEL AND CONFIRM BUTTON TOUCH
  onClose = () => {
    console.log('Modal just closed');
  }

  onCancel = () => {
    console.log('User cancel')
    this.MyModal.close()
  }

  onConfirm = () => {
    console.log('User confirm')
  }

  render() {
    return (
      <View style={{flex: 1,    backgroundColor: '#fff',    alignItems: 'center',    justifyContent: 'center'}}>
        <Button title="Open Modal" color="#841584" accessibilityLabel="Learn more about this purple button" onPress={() => this.MyModal.open()} />
        <MediaModal
          ref={ref => { this.MyModal = ref }}
          title={'Welcome to My App'}
          message={'In this App, we will demonstrate the use of React Native Media Modal'}
          mediaURL={'https://www.url.com/myimage.png'}
          showCancel={true}
          showConfirm={true}
          onClose={this.onClose}
          onCancel={this.onCancel}
          onConfirm={this.onConfirm}
          backdropClose={false}
          customComponent={CustomComponent}
          customStyles={modalCustomStyles}
        />
      </View>
    );
  }
}

Example using functional component

import React, { useRef } from 'react'
import { Text, View, Button } from 'react-native'

import MediaModal from 'react-native-media-modal';

const modalCustomStyles = {
    BackgroundMask: { backgroundColor: 'rgba(255,101,80, 0.4)' },
    container: { backgroundColor: '#e3e3e3' },
    title: { color: 'orange' },
    media: { width: 300 },
    message: { color: 'green' },
}

const MediaModalExample = () => {

    const MediaModalRef = useRef();

    return (
        <View>
            <Text>Media Modal Example - Functional Component</Text>
            <Button title="Open Modal" color="#841584" onPress={MediaModalRef.current.open()} />
            <MediaModal
                ref={MediaModalRef}
                title={'Media Modal Functional Components'}
                message={'This is a example using functional components with Media Modal'}
                mediaURL={'https://www.url.com/myimage.png'}
                showCancel={true}
                showConfirm={true}
                backdropClose={false}
                customStyles={modalCustomStyles}
            />
        </View>
    )
}

export default MediaModalExample

Dependencies

This project use the dependency React Native Webview

Credits

Inspired on React Native Alert Pro from NY Samnang

Author

By Alan Ribeiro

If you think the project is useful, please contribute with a star!