UNPKG

redux-modal-manager

Version:

A custom libary base on redux-modal with fix bug when close modal

2 lines (1 loc) 1.21 kB
import r,{useState as e,useRef as n,useEffect as t}from"react";import{useDispatch as o,useSelector as a}from"react-redux";function u(){return u=Object.assign?Object.assign.bind():function(r){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var t in n)({}).hasOwnProperty.call(n,t)&&(r[t]=n[t])}return r},u.apply(null,arguments)}const s="@redux-modal-manager/SHOW",c="@redux-modal-manager/HIDE",p="@redux-modal-manager/HIDE_ALL";function m(r,e={}){return n=>{n({type:s,name:r,props:e})}}function l(r){return e=>{e({type:c,name:r})}}function i(){return r=>{r({type:p})}}const d={[s]:(r,e)=>({...r,[e.name]:{show:!0,props:e.props}}),[c]:(r,e)=>({...r,[e.name]:{show:!1,props:{}}}),[p]:r=>{const e={};return Object.keys(r).forEach(r=>{e[r]={show:!1,props:{}}}),e}},f={};function h(r=f,e){const n=d[e.type];return n?n(r,e):r}const g=({name:s,getModalState:c=r=>r.modal})=>p=>m=>{const[i,d]=e(!1),f=o(),h=n(null),g=a(c),y=g&&g[s]&&g[s].show,w=g&&g[s]&&g[s].props||{};t(()=>{if(y)return clearTimeout(h.current),d(y);h.current=setTimeout(()=>{d(!1)},500)},[y]);return i?r.createElement(p,u({},m,w,{handleHide:()=>{f(l(s))},show:y})):null};export{g as connectModal,l as hide,i as hideAll,h as reducer,m as show};