2.0.0 • Published 1 year ago

react-modal-better-hooks v2.0.0

Weekly downloads
-
License
GNU
Repository
github
Last release
1 year ago

react-modal-better-hooks

an eaist way to open/close modals and pass props to modals, or register an dynmaic modal and you can use these modals globally.

If you are looking for documentation for obsolete react-modal-better-hooks@1, please, check this branch.

Usage

  • Install react-modal-better-hooks via yarn or npm
yarn add react-modal-better-hooks

#or npm install react-modal-better-hooks
  • Wrap app entry with ModalProvider
import { ModalProvider } from 'react-modal-better-hooks'

export default () => {
  return (
    <div>      
      <ModalProvider defaultProps={{
        width: '500px',
        centered: true
      }}>
        <AppComponnet />
      </ModalProvider>
    </div>
  )
}

Breaking Changes

  • Remove useModal hooks and use useOpenModal, useCloseModal and useUpdateModal instead
  • Remove the withModal HOC and use useRegisterModal to register the Modal, and this can be use in anywhere
  • Remove loading return value in 1.x and use useIsModalLoading instead
  • Remove the need to manually mount WrappedModalElement to the component tree in 1.x
  • Remove ignoreEvent parameter and in 1.x

API

useRegisterModal

This hooks is used to register the modals, register modals by calling the hooks

  • Parameters

    useRegisterModal(modals)
    namedescriptiondefault
    modalsIt's an object where the object key is the modalId and the value is the configuration of the modal you want to register{}
    modalId.componentthe modal ComponentNA
    modalId.isLazyspecify current modal is a lazy modalfalse
    modalId.loaderlazy modal loaderNA
  • Useage

import { useRegisterModal } from 'react-modal-better-hooks'

import Modal1 from 'path/to/modal1'

const Page = () => {
  useRegisterModal({
    modal1: {
      component: modal1
    },
    modal2: {
      isLazy: true,
      loader: () => import('path/to/modal2')
    }
  })

  return (
  	<>
    	{/* page logic */}
    </>
  )
}

useOpenModal

This hooks is used to open modal, it returns a function to open modal, and open modal by calling the function

  • Parameters

    openModal(modalId, props)
    namedescriptiondefault
    modalIdthe id corresponding to the modal that needs to be openedNA
    propsthe props that need to be passed into the modal component{}
  • Useage

import { useOpenModal } from 'react-modal-better-hooks'

interface ModalProps {
	title: string
  content: string
}

const Page = () => {
  const openModal = useOpenModal<ModalPropsInterface>()

  return (
  	<>
    	<div onClick={() => openModal({
        modalId: 'idOfModalToOpen',
        props: {
          title: 'modalTitle',
          content: 'modalContent'
        }
      })}></div>
    </>
  )
}

useCloseModal

This hooks is used to close modal or close all modals, it returns a function named closeModal to close modal, and the function to close all modals is called closeAllModals

  • Parameters

    const { closeModal, closeAllModals } = useCloseModal()
    
    closeModal('modalId')
    namedescriptiondefault
    modalIdthe id corresponding to the modal that needs to be closedNA
  • Useage

import { useCloseModal } from 'react-modal-better-hooks'

const Page = () => {
  const { closeModal, closeAllModals } = useCloseModal()
  
  return (
  	<>
    	<div onClick={() => closeModal('idOfModalToClose')}>close one modal</div>
    	<div onClick={closeAllModals}>close all modals</div>
    </>
  )
}

useUpdateModal

The props of the modal are not necessarily completely unchanged, so if you need to update a certain modal props, you can use this hooks, which return a function, the parameters are similar to openModal, and there is an property merge to specify whether to merge the previous props

  • Parameters

    updateModal(modalId, config)
    namedescriptiondefault
    modalIdthe id corresponding to the modal that needs to be openedNA
    config.propsthe props that need to be passed into the modal componentNA
    config.mergeif it's true, old props will be merged, otherwise, will override old propsfalse
  • Usage

import { useUpdateModal, useOpenModal } from 'react-modal-better-hooks'

interface ModalProps {
	title: string
  content: string
}

const Page = () => {
  const openModal = useOpenModal<ModalPropsInterface>()
  const updateModal = useUpdateModal<ModalPropsInterface>()
  
  return (
  	<>
    	<div onClick={() => {
        openModal('idOfModalToOpen', {
          title: 'modalTitle',
          content: 'modalContent'
        })
        
				/**
					* the modal's props will be 
					* { title: 'newModalTitle', content: 'modalContent' }
					* during second render
					*/
        setTimeOut(() => {
          updateModal('idOfModalToUpdate', {
            merge: true,
            props: {
              title: 'newModalTitle'
            }
          })
        }, 5000)
        
        /**
					* the modal's props will be 
					* { title: 'newModalTitle', content: 'newModalContent' }
					* during second render
					*/
        setTimeOut(() => {
          updateModal('idOfModalToUpdate', {
            props: {
              title: 'newModalTitle',
              content: 'newModalContent'
            }
          })
        }, 10000)
      }}>open and update modal</div>
    </>
  )
}

useModalIsLoading

This hooks is used to determine whether modal or modals is loading and returns a boolean, if the modal is not registered or the modal is not a LazyModal, it will always return false

  • Usage
import { useModalIsLoading } from 'react-modal-better-hooks'

const Page = () => {
  //	returns true when either modal1 or modal2 is loading
  const isLoading = useModalIsLoading(['modal1', 'modal2'])
  
  //	only reutrn modal1 loading state
  const isLoading2 = useModalIsLoading('modal1')
  
  return (
  	<>
    </>
  )
}

Motivation

  • reduce unnecessary business code
  • easier to controll modal display/hidden or update its' props
  • common modal props
1.8.2

1 year ago

1.9.0

1 year ago

1.8.1

1 year ago

1.8.0

1 year ago

2.0.0-alpha.1

1 year ago

2.0.0-alpha

1 year ago

2.0.0

1 year ago

1.8.6

1 year ago

1.8.5

1 year ago

1.8.3

1 year ago

1.7.3

1 year ago

1.7.2

1 year ago

1.7.1

2 years ago

1.6.2

2 years ago

1.7.0

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago