openModal

How to use

openModal method takes an object as an argument. This object should contain at least the component key which should point to your Modal component. You could also add a props key pointing to an objecting containing extra properties that will be passed down the modal component.

import { Plugin } from '@root'
import MyModal from '@components/MyModal'

const App = () => {
    const handleOpenMyModal = useCallback(() => {
        Plugin.openModal({
            component: MyModal,
            props: {
                // pass down custom props to MyModal component
            }
        })
    }, [])
    
    return (
        <div>
            <button onClick={handleOpenMyModal}>
                {'Open my modal'}
            </button>
        </div>
    )
}

Properties

Sizes

Dashboard comes with a range of predefined sizes you'll be able to choose from in order to render your modal.

Choose your size by first retrieve the available ones form the PluginAPI

import { Plugin } from '@root'

const modalSizes = Plugin.MODALSIZE

Multiple modals

Before Dashboard 4.1, modals used to be rendered by calling Plugin.modal.open(...) And that allowed the plugins to render one modal at a time, but with the new ModalAPI, Dashboard supports opening multiple modals at the same time without un-mounting the previous ones.

For example, if you call the deprecated method Plugin.modal.open(...) while you have a modal already rendered, Dashboard will un-mount the rendered modal, and mount the new one. When them a user closes the modal, Dashboard will close the current opened one without going back to the previous one

With the new API Dashboard will stack the modals on top of each other every time Plugin.openModal(...) has been called

So to open multiple modals, simply call Plugin.openModal(...) with your new modal properties

Confirm before close

You can tell Dashboard to display a confirm prompt to the user, when the user tries to close your modal This could be appropriate to do if your modal has changes that will get lost, or still processing some data.

Dashboard will look into your modal component ref and check if the ref has aonBeforeClose() method. If so Dashboard will call the method and wait for it to resolve or reject, therefore it should return a promise.

If onBeforeClose() resolves then Dashboard will close the modal right away. If it's rejected, Dashboard will display a dialog to the user to confirm closing the modal, or cancel.

You can specify the dialog message along with the dialog button labels. You also can register callbacks to see what action the user took, onConfirm/onCancel

The object you reject with, will be the dialog params

Dialog params

Example

import {
    Plugin
} from '@root'

import {
    useRef,
    forwardRef,
    useImperativeHandle
} from 'react'

const MyModal = (props, ref) => {
    const hasChanges = useRef()
    
    useImperativeHandle(ref, () => ({
        onBeforeClose: () => {
            return new Promise((resolve, reject) => {
                if (hasChanges.current) {
                    reject({
                        message: 'Are you sure you want to cancel?'
                        localizations: {
                            cancel: 'Continue editing',
                            confirm: 'Discard my changes'
                        },
                        onCancel: () => {},
                        onConfirm: () => {}
                    })
                } else {
                    resolve()
                }
            })
        }
    }))
    
    return (
        <div>
            <p>{'My awesome modal'}</p>
        </div>
    )
}

export default forwardRef(MyModal)

Last updated