1.0.1 • Published 3 years ago

async-modals v1.0.1

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

npm Types Downloads

Async Modals

A hook-based way of showing modals in React using promises designed to abstract away the complexity of modal windows.

Interested in contributing to async-modals? Please read the section below on Contributing

Table of Contents

To use async-modals install it via npm or yarn

yarn add async-modals

or

npm i async-modals

To start using it:

  1. Wrap your app in the ModalProvider Component and import the styles
import React from 'react';
import {ModalProvider} from 'async-modals';
import 'async-modals/dist/style.css';

const App: React.FC = () => {
  return (
    <ModalProvider>
      ...
    </ModalProvider>
  )
}
  1. Define a modal component
import React from 'react';

const AlertModal = ({data, submit}) => {
  return (
    <div>
      Alert! {data.message}
      <button onClick={() => submit()}>Dismiss</button>
    </div>
  )
}
  1. Use the modal anywhere
import React from 'react';
import {useModal} from 'async-modals'
import AlertModal from './AlertModal'

const Page: React.FC = () => {

  const alertModal = useModal(AlertModal);

  const handleClick = async () => {
    
    //show the modal to the user
    await alertModal.show({
      data: {
        message: 'This is an alert message'
      }
    });
  }

  return (
    <div>
      <button onClick={() => handleClick()}>Show Alert</button>
    </div>
  )
}

These are the options availabe to pass to either ModalProvider or useModal=. You may change the default values of any of these options by passing them to the defaultSettings prop of the modal provider

OptionTypeDescription
canClosebooleanIf true, the user can click on the background to close the modal. Set to true by default
showBgbooleanShow a semi-transparent background behind the modal (see also cssBgOpacity). Set to true by default
allowContentScrollingbooleanAllow the user to scroll the content of the page while the modal is open. Set to false by default
animatedbooleanSet this to true if you want to use animations or transitions (see animation section for more info)
containerClassNamestringclasses to apply to the modal container.
backgroundClassNamestring \| (closed: boolean) => stringClasses to apply to the background that appears behind the modal. Specifying a function for this property is intended for animation by applying different classes when the modal is closed
cssBgOpacitynumberSets the opacity of the background behind the modal. Defaults to 0.5
cssAnimationDurationnumberWill not have any effect unless Animated is true. Sets the duration of the animation on the background via the --duration css variable

Below is a simple implementation for a confirmation modal component.

Note that as async-modal doesnt provide any styles for the modal iteself, you will need to provide your own styles or make use of a UI library such as bootstrap or Material UI.

const ConfirmationModal = ({data, submit, cancel}) => {
  <div className="modal">
    <div className="modal-header">
      <h3>Confirm Action</h3>
      <button onClick={cancel}>x</button>
    </div>
      <div className="modal-body">
        {data.message}
      <button onClick={() => submit(false)}>Cancel</button>
      <button onClick={() => submit(true)}>Confirm</button>
    </div>
  </div>
}


export default ConfirmationModal;

A complete list of the props passed into the modal are:

PropDescription
dataObject containing data passed to the modal via useModal
cancelCalling this function indicates the user has cancelled the modal, for example by pressing the 'x' button that is present in the corner of most modals
submitSimilar to cancel, however submit allows you to pass some data back to the calling component
isClosingA boolean representing if the modal is closing. This allows you to play an exit animation or transition. To make use of this, ensure that the animation option is set to true

Functions

Basic Usage

import {ModalProvider} from 'async-modals';
import 'async-modals/dist/style.css';

const App: React.FC = () => {
  return (
    <ModalProvider>
      ...
    </ModalProvider>
  )
}

You may also specify default options using the defaultSettings prop

import {ModalProvider} from 'async-modals';
import 'async-modals/dist/style.css';

const App: React.FC = () => {
  return (
    <ModalProvider defaultSettings={{
      animated: true
    }}>
      ...
    </ModalProvider>
  )
}

Basic Usage

import {useModal} from 'async-modals';
import ModalComponent from '../components/ModalComponent';
...

const modal = useModal(ModalComponent)

You can also specify any options here as well

import {useModal} from 'async-modals';
import ModalComponent from '../components/ModalComponent'

...

const modal = useModal(ModalComponent, {
  showBg: false,
  AllowContentScrolling: true,
})

This returns a modal object with the following methods

MethodDescription
show(options)Display the modal. For options you may specify any option, and additionally you may use the data property to pass data to the modal. This returns a promise that can be awaited. Any options specified here will override those passed to useModal
close()Closes the modal if it is open.
updateModalData(data)Update the data being displayed in the modal while it is open

Below is an example of showing an alert modal with a message being passed in

const modal = useModal(AlertModal);

const showAlert = async () => {
  await modal.show({
    data: {
      message: "This is an alert"
    }
  })
};

You can also await data being returned from a modal component. The below example shows a modal that gets a user's name and then prints it to the console

When receieving data from a modal, it is important to check if it exists before using it. In this instance, if the user exits the modal, name will be undefined

const welcomeModal = useModal(WelcomeModal);

const welcome = async () => {
  const name = await welcomeModal.show()

  if(name){
    console.log(name);
  }
};

The development of this package has mostly been motivated by my own project requirements and use cases. If there is a feature that you would like to see to support your own use cases, please open an Issue or a Pull Request.

MIT Licensed. Copyright (c) Alexander Nicholson 2021.

1.0.1

3 years ago

1.0.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.5

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.3.9

3 years ago

0.3.14

3 years ago

0.3.13

3 years ago

0.3.12

3 years ago

0.3.11

3 years ago

0.3.10

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.8

3 years ago

0.3.7

3 years ago

0.3.2

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.1

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago