redux-modal-manager
Version:
A custom libary base on redux-modal with fix bug when close modal
2 lines (1 loc) • 1.35 kB
JavaScript
;Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("react-redux");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=t(e);function o(){return o=Object.assign?Object.assign.bind():function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)({}).hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e},o.apply(null,arguments)}const u="@redux-modal-manager/SHOW",a="@redux-modal-manager/HIDE",s="@redux-modal-manager/HIDE_ALL";function c(e){return r=>{r({type:a,name:e})}}const p={[u]:(e,r)=>({...e,[r.name]:{show:!0,props:r.props}}),[a]:(e,r)=>({...e,[r.name]:{show:!1,props:{}}}),[s]:e=>{const r={};return Object.keys(e).forEach(e=>{r[e]={show:!1,props:{}}}),r}},l={};exports.connectModal=({name:t,getModalState:u=e=>e.modal})=>a=>s=>{const[p,l]=e.useState(!1),i=r.useDispatch(),d=e.useRef(null),f=r.useSelector(u),m=f&&f[t]&&f[t].show,h=f&&f[t]&&f[t].props||{};e.useEffect(()=>{if(m)return clearTimeout(d.current),l(m);d.current=setTimeout(()=>{l(!1)},500)},[m]);return p?n.default.createElement(a,o({},s,h,{handleHide:()=>{i(c(t))},show:m})):null},exports.hide=c,exports.hideAll=function(){return e=>{e({type:s})}},exports.reducer=function(e=l,r){const t=p[r.type];return t?t(e,r):e},exports.show=function(e,r={}){return t=>{t({type:u,name:e,props:r})}};