Skip to content

๐Ÿฆ„ A modal library that can be called imperatively from anywhere!

License

Notifications You must be signed in to change notification settings

laogui/react-native-magic-modal

ย 
ย 

Repository files navigation

React Native Magic Modal Cover

A modal library that can be called imperatively from anywhere!

React Native Magic Modal ๐Ÿฆ„

Do you find using modals in React Native to be a bit of a pain? Trying to keep control of its open state and repeating the code everywhere you want to use it can be pretty tedious.

And the problem only gets worse when you try to create complex flows, where one modal opens another with conditionals in place. Once you get past two modals, your main component is a mess, and the state is all over the place.

This library thoughtfully encapsulates complex concepts to provide a smooth experience when using React modals, inside or outside components (In Sagas, for example!)

Take a look to a in-depth explanation of its concepts on its Medium article.

๐Ÿ“ธ Examples

IOS Android

๐Ÿ›  Installation

yarn add react-native-magic-modal

โš™๏ธ Usage

First, insert a MagicModalPortal in the top of the application.

import { MagicModalPortal } from 'react-native-magic-modal';

export default function App() {
  return (
    <SomeRandomProvider>
      <MagicModalPortal />  // <-- On the top of the app component hierarchy
      <Router /> // Your app router or something could follow below
    </SomeRandomProvider>
  );
}

Then, you are free to use the magicModal as shown from anywhere you want.

import React from 'react';
import { View, Text, TouchableOpacity } from 'react-native';
import { MagicModalPortal, magicModal } from 'react-native-magic-modal';

const ConfirmationModal = () => (
  <View>
    <TouchableOpacity onPress={() => magicModal.hide({ success: true })}>
      <Text>Click here to confirm</Text>
    </TouchableOpacity>
  </View>
);

const ResponseModal = ({ text }) => (
  <View>
    <Text>{text}</Text>
    <TouchableOpacity onPress={() => magicModal.hide()}>
      <Text>Close</Text>
    </TouchableOpacity>
  </View>
);

const handleConfirmationFlow = async () => {
  // We can call it with or without props, depending on the requirements of the modal.
  const result = await magicModal.show(ConfirmationModal);

  if (result.success) {
    return magicModal.show(() => <ResponseModal text="Success!" />);
  }

  return magicModal.show(() => <ResponseModal text="Failure :(" />);
};

export const MainScreen = () => {
  return (
    <View>
      <TouchableOpacity onPress={handleConfirmationFlow}>
        <Text>Start the modal flow!</Text>
      </TouchableOpacity>
      <MagicModalPortal />
    </View>
  );
};

See example to see it in practice.

๐Ÿ“– Documentation

The docs are located here in the project's Github Pages

๐Ÿ‘จโ€๐Ÿซ Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

โš–๏ธ License

MIT

About

๐Ÿฆ„ A modal library that can be called imperatively from anywhere!

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • TypeScript 78.0%
  • JavaScript 20.7%
  • Shell 1.3%