UNPKG

dynamic-modal

Version:

The dynamic-modal is a solution of creation different modals into project using a json configuration file

26 lines (25 loc) 767 B
'use client'; import { useState } from 'react'; export function useModalHandler() { const initialState = { modalProps: { config: {}, close: () => { }, open: false, }, openModal: () => { }, }; const [modalConfig, setModalConfig] = useState(initialState); const openModal = (config) => { if (!config) { alert(`¡WARNING! this modal was not returned config, please check before use`); return; } setModalConfig({ openModal, modalProps: { config, open: true, close } }); }; const closeModal = () => setModalConfig(initialState); return { openModal, modalProps: { ...modalConfig.modalProps, close: closeModal }, }; }