react-modal-port 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/index.modern.mjs +1 -1
- package/dist/index.modern.mjs.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +8 -8
- package/readme.md +19 -9
- package/types/index.d.ts +8 -2
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=require("react/jsx-runtime"),t=require("react");function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},r.apply(this,arguments)}var n=t.createContext({stack:[],launchModal:function(){},updateStack:function(){},updateState:function(){}});function o(){return t.useContext(n)}exports.ModalContextProvider=function(o){var a=o.children,c=t.useState([]),u=c[0],s=c[1]
|
|
1
|
+
var e=require("react/jsx-runtime"),t=require("react");function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},r.apply(this,arguments)}var n=t.createContext({stack:[],launchModal:function(){},updateStack:function(){},updateState:function(){}});function o(){return t.useContext(n)}exports.ModalContextProvider=function(o){var a=o.children,c=t.useState([]),u=c[0],s=c[1],l={stack:u,launchModal:function(e,t,r){void 0===r&&(r={});var n=Object.keys(t).reduce(function(e,r){var n=t[r];return e[r]=function(){try{return Promise.resolve(n.apply(void 0,[].slice.call(arguments))).then(function(e){return s(function(e){return e.slice(0,-1)}),e})}catch(e){return Promise.reject(e)}},e},{});s([].concat(u,[{render:e,resolvers:n,props:r,state:{}}]))},updateStack:s,updateState:function(e){var t=[].concat(u);t[u.length-1].state=r({},e),s(t)}};return e.jsx(n.Provider,{value:l,children:a})},exports.ModalPort=function(n){var a=n.onModalLaunch,c=n.onModalClose,u=n.render,s=o().stack,l=t.useMemo(function(){return s.length>0?s[s.length-1]:null},[s]),i=t.useCallback(function(e){e.currentTarget===e.target&&(null==l?void 0:l.resolvers.onBackdropClickUse)&&l.resolvers.onBackdropClickUse(e)},[l]);return t.useEffect(function(){l?a&&a():c&&c()},[l,a,c]),l&&e.jsx(u,{onBackdropClick:i,children:l.render(r({},l.resolvers,l.props))})||null},exports.useModal=function(){return o().launchModal},exports.useModalContext=o,exports.useModalState=function(){var e=o(),t=e.stack,r=e.updateState;if(0===t.length)throw new Error("Trying to access modal state while no modal is open");return[t[t.length-1].state,r]};
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n type SyntheticEvent \n} from 'react';\nimport { \n type ModalStackItem,\n type ModalContextProperties,\n type LaunchModal,\n type LaunchModalResolvers,\n type ModalState,\n type UpdateModalState,\n} from '../types';\n\nexport const ModalContext = createContext<ModalContextProperties>({\n stack: [],\n launchModal: () => {},\n updateStack: () => {},\n updateState: () => {},\n});\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem[]>([]);\n\n const launchModal: LaunchModal = (render, resolvers,
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n type SyntheticEvent \n} from 'react';\nimport { \n type ModalStackItem,\n type ModalContextProperties,\n type LaunchModal,\n type LaunchModalResolvers,\n type ModalState,\n type UpdateModalState,\n} from '../types';\n\nexport const ModalContext = createContext<ModalContextProperties>({\n stack: [],\n launchModal: () => {},\n updateStack: () => {},\n updateState: () => {},\n});\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem[]>([]);\n\n const launchModal: LaunchModal = (render, resolvers, props = {}) => {\n // Wrap resolvers to update the stack\n const wrappedResovers = Object.keys(resolvers).reduce((acc, key) => {\n const resolver = resolvers[key];\n acc[key] = async (...args: any[]) => {\n const result = await resolver(...args);\n // update stack with an array conating all the items in the stack except the last one\n updateStack(currStack => currStack.slice(0, -1));\n return result;\n };\n return acc;\n }, {} as LaunchModalResolvers);\n updateStack([\n ...stack,\n {\n render,\n resolvers: wrappedResovers,\n props,\n state: {},\n },\n ]);\n };\n\n const updateState = (newState: ModalState) => {\n const updatedStack = [\n ...stack,\n ];\n updatedStack[stack.length - 1].state = { ...newState };\n updateStack(updatedStack);\n };\n\n const contextProperties: ModalContextProperties = {\n stack,\n launchModal,\n updateStack,\n updateState,\n };\n\n return <ModalContext.Provider value={contextProperties}>{children}</ModalContext.Provider>;\n\n};\n\n/**\n * Get properties from the modal context\n * @returns {ModalContextProperties} Object with the modal context properties\n */\nexport function useModalContext(): ModalContextProperties {\n return useContext(ModalContext);\n}\n\n/**\n * Short-hand for `useModalContext().launchModal`\n * @returns {LaunchModal} The launchModal function\n */\nexport function useModal(): LaunchModal {\n const { launchModal } = useModalContext();\n return launchModal;\n}\n\n/**\n * Get access to the current modal state and a function to update it\n * @returns {[ ModalState, UpdateModalState ]} A tuple with the current modal state and a function to update it\n */\nexport function useModalState(): [ ModalState, UpdateModalState ] {\n const { stack, updateState } = useModalContext();\n if (stack.length === 0) \n throw new Error('Trying to access modal state while no modal is open');\n const state = stack[stack.length - 1].state;\n return [ state, updateState ];\n}\n","import { \n type ModalPortProps, \n type ModalStackItem \n} from \"../types\";\nimport { useModalContext } from \"./context\";\nimport { \n type SyntheticEvent, \n useCallback, \n useEffect, \n useMemo\n} from \"react\";\n\nconst ModalPort: React.FC<ModalPortProps> = ({\n onModalLaunch,\n onModalClose,\n render: Backdrop\n}) => {\n\n const { stack } = useModalContext();\n const currentModal: ModalStackItem | null = useMemo(\n () => stack.length > 0 ? stack[stack.length - 1] : null,\n [ stack ]\n );\n const onBackdropClick = useCallback((ev: SyntheticEvent) => {\n if (ev.currentTarget !== ev.target) return;\n currentModal?.resolvers.onBackdropClickUse &&\n currentModal.resolvers.onBackdropClickUse(ev);\n }, [ currentModal ]);\n\n useEffect(() => {\n if (currentModal) {\n onModalLaunch && onModalLaunch();\n } else {\n onModalClose && onModalClose();\n }\n }, [ currentModal, onModalLaunch, onModalClose ]);\n\n return currentModal && (\n <Backdrop onBackdropClick={onBackdropClick}>\n {currentModal.render({\n ...currentModal.resolvers,\n ...currentModal.props,\n })}\n </Backdrop>\n ) || null;\n};\n\nexport default ModalPort;\n"],"names":["ModalContext","createContext","stack","launchModal","updateStack","updateState","useModalContext","useContext","_ref","children","_useState","useState","contextProperties","render","resolvers","props","wrappedResovers","Object","keys","reduce","acc","key","resolver","Promise","resolve","apply","slice","call","arguments","then","result","currStack","e","reject","concat","state","newState","updatedStack","length","_extends","_jsx","Provider","value","onModalLaunch","onModalClose","Backdrop","currentModal","useMemo","onBackdropClick","useCallback","ev","currentTarget","target","onBackdropClickUse","useEffect","_useModalContext2","Error"],"mappings":"8RAeaA,EAAeC,EAAaA,cAAyB,CAChEC,MAAO,GACPC,YAAa,WAAQ,EACrBC,YAAa,WAAQ,EACrBC,YAAa,WAAK,aAqDJC,IACd,OAAOC,EAAAA,WAAWP,EACpB,8BApDoC,SAAHQ,GAAM,IAAAC,EAAQD,EAARC,SAErCC,EAA+BC,EAAAA,SAA2B,IAAlDT,EAAKQ,EAAEN,GAAAA,EAAWM,EAE1B,GA+BME,EAA4C,CAChDV,MAAAA,EACAC,YAjC+B,SAACU,EAAQC,EAAWC,QAAAA,IAAAA,IAAAA,EAAQ,CAAA,GAE3D,IAAMC,EAAkBC,OAAOC,KAAKJ,GAAWK,OAAO,SAACC,EAAKC,GAC1D,IAAMC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H,WAEV,kDAMgB,WACd,IAAAoD,EAA+BjD,IAAvBJ,EAAKqD,EAALrD,MAAOG,EAAWkD,EAAXlD,YACf,GAAqB,IAAjBH,EAAMoC,OACR,MAAU,IAAAkB,MAAM,uDAElB,MAAO,CADOtD,EAAMA,EAAMoC,OAAS,GAAGH,MACtB9B,EAClB"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jsx as n}from"react/jsx-runtime";import{createContext as
|
|
1
|
+
import{jsx as n}from"react/jsx-runtime";import{createContext as r,useState as t,useContext as e,useMemo as o,useCallback as a,useEffect as c}from"react";function u(){return u=Object.assign?Object.assign.bind():function(n){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var e in t)Object.prototype.hasOwnProperty.call(t,e)&&(n[e]=t[e])}return n},u.apply(this,arguments)}var i=r({stack:[],launchModal:function(){},updateStack:function(){},updateState:function(){}}),l=function(r){var e=r.children,o=t([]),a=o[0],c=o[1],l={stack:a,launchModal:function(n,r,t){void 0===t&&(t={});var e=Object.keys(r).reduce(function(n,t){var e=r[t];return n[t]=function(){try{return Promise.resolve(e.apply(void 0,[].slice.call(arguments))).then(function(n){return c(function(n){return n.slice(0,-1)}),n})}catch(n){return Promise.reject(n)}},n},{});c([].concat(a,[{render:n,resolvers:e,props:t,state:{}}]))},updateStack:c,updateState:function(n){var r=[].concat(a);r[a.length-1].state=u({},n),c(r)}};return n(i.Provider,{value:l,children:e})};function s(){return e(i)}function d(){return s().launchModal}function f(){var n=s(),r=n.stack,t=n.updateState;if(0===r.length)throw new Error("Trying to access modal state while no modal is open");return[r[r.length-1].state,t]}var p=function(r){var t=r.onModalLaunch,e=r.onModalClose,i=r.render,l=s().stack,d=o(function(){return l.length>0?l[l.length-1]:null},[l]),f=a(function(n){n.currentTarget===n.target&&(null==d?void 0:d.resolvers.onBackdropClickUse)&&d.resolvers.onBackdropClickUse(n)},[d]);return c(function(){d?t&&t():e&&e()},[d,t,e]),d&&n(i,{onBackdropClick:f,children:d.render(u({},d.resolvers,d.props))})||null};export{l as ModalContextProvider,p as ModalPort,d as useModal,s as useModalContext,f as useModalState};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n type SyntheticEvent \n} from 'react';\nimport { \n type ModalStackItem,\n type ModalContextProperties,\n type LaunchModal,\n type LaunchModalResolvers,\n type ModalState,\n type UpdateModalState,\n} from '../types';\n\nexport const ModalContext = createContext<ModalContextProperties>({\n stack: [],\n launchModal: () => {},\n updateStack: () => {},\n updateState: () => {},\n});\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem[]>([]);\n\n const launchModal: LaunchModal = (render, resolvers,
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n type SyntheticEvent \n} from 'react';\nimport { \n type ModalStackItem,\n type ModalContextProperties,\n type LaunchModal,\n type LaunchModalResolvers,\n type ModalState,\n type UpdateModalState,\n} from '../types';\n\nexport const ModalContext = createContext<ModalContextProperties>({\n stack: [],\n launchModal: () => {},\n updateStack: () => {},\n updateState: () => {},\n});\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem[]>([]);\n\n const launchModal: LaunchModal = (render, resolvers, props = {}) => {\n // Wrap resolvers to update the stack\n const wrappedResovers = Object.keys(resolvers).reduce((acc, key) => {\n const resolver = resolvers[key];\n acc[key] = async (...args: any[]) => {\n const result = await resolver(...args);\n // update stack with an array conating all the items in the stack except the last one\n updateStack(currStack => currStack.slice(0, -1));\n return result;\n };\n return acc;\n }, {} as LaunchModalResolvers);\n updateStack([\n ...stack,\n {\n render,\n resolvers: wrappedResovers,\n props,\n state: {},\n },\n ]);\n };\n\n const updateState = (newState: ModalState) => {\n const updatedStack = [\n ...stack,\n ];\n updatedStack[stack.length - 1].state = { ...newState };\n updateStack(updatedStack);\n };\n\n const contextProperties: ModalContextProperties = {\n stack,\n launchModal,\n updateStack,\n updateState,\n };\n\n return <ModalContext.Provider value={contextProperties}>{children}</ModalContext.Provider>;\n\n};\n\n/**\n * Get properties from the modal context\n * @returns {ModalContextProperties} Object with the modal context properties\n */\nexport function useModalContext(): ModalContextProperties {\n return useContext(ModalContext);\n}\n\n/**\n * Short-hand for `useModalContext().launchModal`\n * @returns {LaunchModal} The launchModal function\n */\nexport function useModal(): LaunchModal {\n const { launchModal } = useModalContext();\n return launchModal;\n}\n\n/**\n * Get access to the current modal state and a function to update it\n * @returns {[ ModalState, UpdateModalState ]} A tuple with the current modal state and a function to update it\n */\nexport function useModalState(): [ ModalState, UpdateModalState ] {\n const { stack, updateState } = useModalContext();\n if (stack.length === 0) \n throw new Error('Trying to access modal state while no modal is open');\n const state = stack[stack.length - 1].state;\n return [ state, updateState ];\n}\n","import { \n type ModalPortProps, \n type ModalStackItem \n} from \"../types\";\nimport { useModalContext } from \"./context\";\nimport { \n type SyntheticEvent, \n useCallback, \n useEffect, \n useMemo\n} from \"react\";\n\nconst ModalPort: React.FC<ModalPortProps> = ({\n onModalLaunch,\n onModalClose,\n render: Backdrop\n}) => {\n\n const { stack } = useModalContext();\n const currentModal: ModalStackItem | null = useMemo(\n () => stack.length > 0 ? stack[stack.length - 1] : null,\n [ stack ]\n );\n const onBackdropClick = useCallback((ev: SyntheticEvent) => {\n if (ev.currentTarget !== ev.target) return;\n currentModal?.resolvers.onBackdropClickUse &&\n currentModal.resolvers.onBackdropClickUse(ev);\n }, [ currentModal ]);\n\n useEffect(() => {\n if (currentModal) {\n onModalLaunch && onModalLaunch();\n } else {\n onModalClose && onModalClose();\n }\n }, [ currentModal, onModalLaunch, onModalClose ]);\n\n return currentModal && (\n <Backdrop onBackdropClick={onBackdropClick}>\n {currentModal.render({\n ...currentModal.resolvers,\n ...currentModal.props,\n })}\n </Backdrop>\n ) || null;\n};\n\nexport default ModalPort;\n"],"names":["ModalContext","createContext","stack","launchModal","updateStack","updateState","ModalContextProvider","_ref","children","_useState","useState","contextProperties","render","resolvers","props","wrappedResovers","Object","keys","reduce","acc","key","resolver","Promise","resolve","apply","slice","call","arguments","then","result","currStack","e","reject","concat","state","newState","updatedStack","length","_extends","_jsx","Provider","value","useModalContext","useContext","useModal","useModalState","_useModalContext2","Error","ModalPort","onModalLaunch","onModalClose","Backdrop","currentModal","useMemo","onBackdropClick","useCallback","ev","currentTarget","target","onBackdropClickUse","useEffect"],"mappings":"iYAeaA,EAAeC,EAAsC,CAChEC,MAAO,GACPC,YAAa,WAAQ,EACrBC,YAAa,WAAQ,EACrBC,YAAa,WAAK,IAGPC,EAAuB,SAAHC,GAAM,IAAAC,EAAQD,EAARC,SAErCC,EAA+BC,EAA2B,IAAlDR,EAAKO,EAAEL,GAAAA,EAAWK,EAE1B,GA+BME,EAA4C,CAChDT,MAAAA,EACAC,YAjC+B,SAACS,EAAQC,EAAWC,QAAAA,IAAAA,IAAAA,EAAQ,CAAA,GAE3D,IAAMC,EAAkBC,OAAOC,KAAKJ,GAAWK,OAAO,SAACC,EAAKC,GAC1D,IAAMC,EAAWR,EAAUO,GAO3B,OANAD,EAAIC,kBAAgCE,OAAAA,QAAAC,QACbF,EAAQG,WAAA,EAAA,GAAAC,MAAAC,KADKC,aACIC,KAAhCC,SAAAA,GAGN,OADAzB,EAAY,SAAA0B,UAAaA,EAAUL,MAAM,GAAI,EAAE,GACxCI,CAAO,EAChB,CAAC,MAAAE,GAAA,OAAAT,QAAAU,OAAAD,EACD,CAAA,EAAOZ,CACT,EAAG,IACHf,KAAW6B,OACN/B,EACH,CAAA,CACEU,OAAAA,EACAC,UAAWE,EACXD,MAAAA,EACAoB,MAAO,CAAA,KAGb,EAaE9B,YAAAA,EACAC,YAZkB,SAAC8B,GACnB,IAAMC,EAAY,GAAAH,OACb/B,GAELkC,EAAalC,EAAMmC,OAAS,GAAGH,MAAKI,KAAQH,GAC5C/B,EAAYgC,EACd,GASA,OAAOG,EAACvC,EAAawC,SAAQ,CAACC,MAAO9B,EAAiBH,SAAGA,GAE3D,WAMgBkC,IACd,OAAOC,EAAW3C,EACpB,CAMgB,SAAA4C,IAEd,OADwBF,IAAhBvC,WAEV,CAMgB,SAAA0C,IACd,IAAAC,EAA+BJ,IAAvBxC,EAAK4C,EAAL5C,MAAOG,EAAWyC,EAAXzC,YACf,GAAqB,IAAjBH,EAAMmC,OACR,MAAU,IAAAU,MAAM,uDAElB,MAAO,CADO7C,EAAMA,EAAMmC,OAAS,GAAGH,MACtB7B,EAClB,CCnFM,IAAA2C,EAAsC,SAA7BzC,GACb,IAAA0C,EAAa1C,EAAb0C,cACAC,EAAY3C,EAAZ2C,aACQC,EAAQ5C,EAAhBK,OAGQV,EAAUwC,IAAVxC,MACFkD,EAAsCC,EAC1C,kBAAMnD,EAAMmC,OAAS,EAAInC,EAAMA,EAAMmC,OAAS,GAAK,IAAI,EACvD,CAAEnC,IAEEoD,EAAkBC,EAAY,SAACC,GAC/BA,EAAGC,gBAAkBD,EAAGE,SAChB,MAAZN,OAAY,EAAZA,EAAcvC,UAAU8C,qBACxBP,EAAavC,UAAU8C,mBAAmBH,EAC5C,EAAG,CAAEJ,IAUL,OARAQ,EAAU,WACJR,EACFH,GAAiBA,IAEjBC,GAAgBA,GAEpB,EAAG,CAAEE,EAAcH,EAAeC,IAE3BE,GACLb,EAACY,EAAQ,CAACG,gBAAiBA,EAAe9C,SACvC4C,EAAaxC,OAAM0B,EACfc,CAAAA,EAAAA,EAAavC,UACbuC,EAAatC,WAGjB,IACP"}
|
package/dist/index.modern.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{jsx as t}from"react/jsx-runtime";import{createContext as e,useState as
|
|
1
|
+
import{jsx as t}from"react/jsx-runtime";import{createContext as e,useState as r,useContext as n,useMemo as o,useCallback as a,useEffect as c}from"react";function s(){return s=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},s.apply(this,arguments)}const l=e({stack:[],launchModal:()=>{},updateStack:()=>{},updateState:()=>{}}),u=({children:e})=>{const[n,o]=r([]);return t(l.Provider,{value:{stack:n,launchModal:(t,e,r={})=>{const a=Object.keys(e).reduce((t,r)=>{const n=e[r];return t[r]=async(...t)=>{const e=await n(...t);return o(t=>t.slice(0,-1)),e},t},{});o([...n,{render:t,resolvers:a,props:r,state:{}}])},updateStack:o,updateState:t=>{const e=[...n];e[n.length-1].state=s({},t),o(e)}},children:e})};function i(){return n(l)}function d(){const{launchModal:t}=i();return t}function p(){const{stack:t,updateState:e}=i();if(0===t.length)throw new Error("Trying to access modal state while no modal is open");return[t[t.length-1].state,e]}const h=({onModalLaunch:e,onModalClose:r,render:n})=>{const{stack:l}=i(),u=o(()=>l.length>0?l[l.length-1]:null,[l]),d=a(t=>{t.currentTarget===t.target&&(null==u?void 0:u.resolvers.onBackdropClickUse)&&u.resolvers.onBackdropClickUse(t)},[u]);return c(()=>{u?e&&e():r&&r()},[u,e,r]),u&&t(n,{onBackdropClick:d,children:u.render(s({},u.resolvers,u.props))})||null};export{u as ModalContextProvider,h as ModalPort,d as useModal,i as useModalContext,p as useModalState};
|
|
2
2
|
//# sourceMappingURL=index.modern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.modern.mjs","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n type SyntheticEvent \n} from 'react';\nimport { \n type ModalStackItem,\n type ModalContextProperties,\n type LaunchModal,\n type LaunchModalResolvers,\n type ModalState,\n type UpdateModalState,\n} from '../types';\n\nexport const ModalContext = createContext<ModalContextProperties>({\n stack: [],\n launchModal: () => {},\n updateStack: () => {},\n updateState: () => {},\n});\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem[]>([]);\n\n const launchModal: LaunchModal = (render, resolvers,
|
|
1
|
+
{"version":3,"file":"index.modern.mjs","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n type SyntheticEvent \n} from 'react';\nimport { \n type ModalStackItem,\n type ModalContextProperties,\n type LaunchModal,\n type LaunchModalResolvers,\n type ModalState,\n type UpdateModalState,\n} from '../types';\n\nexport const ModalContext = createContext<ModalContextProperties>({\n stack: [],\n launchModal: () => {},\n updateStack: () => {},\n updateState: () => {},\n});\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem[]>([]);\n\n const launchModal: LaunchModal = (render, resolvers, props = {}) => {\n // Wrap resolvers to update the stack\n const wrappedResovers = Object.keys(resolvers).reduce((acc, key) => {\n const resolver = resolvers[key];\n acc[key] = async (...args: any[]) => {\n const result = await resolver(...args);\n // update stack with an array conating all the items in the stack except the last one\n updateStack(currStack => currStack.slice(0, -1));\n return result;\n };\n return acc;\n }, {} as LaunchModalResolvers);\n updateStack([\n ...stack,\n {\n render,\n resolvers: wrappedResovers,\n props,\n state: {},\n },\n ]);\n };\n\n const updateState = (newState: ModalState) => {\n const updatedStack = [\n ...stack,\n ];\n updatedStack[stack.length - 1].state = { ...newState };\n updateStack(updatedStack);\n };\n\n const contextProperties: ModalContextProperties = {\n stack,\n launchModal,\n updateStack,\n updateState,\n };\n\n return <ModalContext.Provider value={contextProperties}>{children}</ModalContext.Provider>;\n\n};\n\n/**\n * Get properties from the modal context\n * @returns {ModalContextProperties} Object with the modal context properties\n */\nexport function useModalContext(): ModalContextProperties {\n return useContext(ModalContext);\n}\n\n/**\n * Short-hand for `useModalContext().launchModal`\n * @returns {LaunchModal} The launchModal function\n */\nexport function useModal(): LaunchModal {\n const { launchModal } = useModalContext();\n return launchModal;\n}\n\n/**\n * Get access to the current modal state and a function to update it\n * @returns {[ ModalState, UpdateModalState ]} A tuple with the current modal state and a function to update it\n */\nexport function useModalState(): [ ModalState, UpdateModalState ] {\n const { stack, updateState } = useModalContext();\n if (stack.length === 0) \n throw new Error('Trying to access modal state while no modal is open');\n const state = stack[stack.length - 1].state;\n return [ state, updateState ];\n}\n","import { \n type ModalPortProps, \n type ModalStackItem \n} from \"../types\";\nimport { useModalContext } from \"./context\";\nimport { \n type SyntheticEvent, \n useCallback, \n useEffect, \n useMemo\n} from \"react\";\n\nconst ModalPort: React.FC<ModalPortProps> = ({\n onModalLaunch,\n onModalClose,\n render: Backdrop\n}) => {\n\n const { stack } = useModalContext();\n const currentModal: ModalStackItem | null = useMemo(\n () => stack.length > 0 ? stack[stack.length - 1] : null,\n [ stack ]\n );\n const onBackdropClick = useCallback((ev: SyntheticEvent) => {\n if (ev.currentTarget !== ev.target) return;\n currentModal?.resolvers.onBackdropClickUse &&\n currentModal.resolvers.onBackdropClickUse(ev);\n }, [ currentModal ]);\n\n useEffect(() => {\n if (currentModal) {\n onModalLaunch && onModalLaunch();\n } else {\n onModalClose && onModalClose();\n }\n }, [ currentModal, onModalLaunch, onModalClose ]);\n\n return currentModal && (\n <Backdrop onBackdropClick={onBackdropClick}>\n {currentModal.render({\n ...currentModal.resolvers,\n ...currentModal.props,\n })}\n </Backdrop>\n ) || null;\n};\n\nexport default ModalPort;\n"],"names":["ModalContext","createContext","stack","launchModal","updateStack","updateState","ModalContextProvider","children","useState","_jsx","Provider","value","render","resolvers","props","wrappedResovers","Object","keys","reduce","acc","key","resolver","async","args","result","currStack","slice","state","newState","updatedStack","length","_extends","useModalContext","useContext","useModal","useModalState","Error","ModalPort","onModalLaunch","onModalClose","Backdrop","currentModal","useMemo","onBackdropClick","useCallback","ev","currentTarget","target","onBackdropClickUse","useEffect"],"mappings":"mYAeaA,EAAe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AAUmC,qBACxBP,EAAa5B,UAAUmC,mBAAmBH,EAC5C,EAAG,CAAEJ,IAUL,OARAQ,EAAU,KACJR,EACFH,GAAiBA,IAEjBC,GAAgBA,GAClB,EACC,CAAEE,EAAcH,EAAeC,IAE3BE,GACLhC,EAAC+B,EAAQ,CAACG,gBAAiBA,EAAepC,SACvCkC,EAAa7B,OAAMmB,EAAA,CAAA,EACfU,EAAa5B,UACb4B,EAAa3B,WAGjB"}
|
package/dist/index.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react/jsx-runtime"),require("react")):"function"==typeof define&&define.amd?define(["exports","react/jsx-runtime","react"],t):t((e||self).reactModalPort={},e.
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react/jsx-runtime"),require("react")):"function"==typeof define&&define.amd?define(["exports","react/jsx-runtime","react"],t):t((e||self).reactModalPort={},e.jsx,e.react)}(this,function(e,t,n){function r(){return r=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},r.apply(this,arguments)}var o=n.createContext({stack:[],launchModal:function(){},updateStack:function(){},updateState:function(){}});function a(){return n.useContext(o)}e.ModalContextProvider=function(e){var a=e.children,c=n.useState([]),u=c[0],i=c[1],l={stack:u,launchModal:function(e,t,n){void 0===n&&(n={});var r=Object.keys(t).reduce(function(e,n){var r=t[n];return e[n]=function(){try{return Promise.resolve(r.apply(void 0,[].slice.call(arguments))).then(function(e){return i(function(e){return e.slice(0,-1)}),e})}catch(e){return Promise.reject(e)}},e},{});i([].concat(u,[{render:e,resolvers:r,props:n,state:{}}]))},updateStack:i,updateState:function(e){var t=[].concat(u);t[u.length-1].state=r({},e),i(t)}};return t.jsx(o.Provider,{value:l,children:a})},e.ModalPort=function(e){var o=e.onModalLaunch,c=e.onModalClose,u=e.render,i=a().stack,l=n.useMemo(function(){return i.length>0?i[i.length-1]:null},[i]),s=n.useCallback(function(e){e.currentTarget===e.target&&(null==l?void 0:l.resolvers.onBackdropClickUse)&&l.resolvers.onBackdropClickUse(e)},[l]);return n.useEffect(function(){l?o&&o():c&&c()},[l,o,c]),l&&t.jsx(u,{onBackdropClick:s,children:l.render(r({},l.resolvers,l.props))})||null},e.useModal=function(){return a().launchModal},e.useModalContext=a,e.useModalState=function(){var e=a(),t=e.stack,n=e.updateState;if(0===t.length)throw new Error("Trying to access modal state while no modal is open");return[t[t.length-1].state,n]}});
|
|
2
2
|
//# sourceMappingURL=index.umd.js.map
|
package/dist/index.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.umd.js","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n type SyntheticEvent \n} from 'react';\nimport { \n type ModalStackItem,\n type ModalContextProperties,\n type LaunchModal,\n type LaunchModalResolvers,\n type ModalState,\n type UpdateModalState,\n} from '../types';\n\nexport const ModalContext = createContext<ModalContextProperties>({\n stack: [],\n launchModal: () => {},\n updateStack: () => {},\n updateState: () => {},\n});\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem[]>([]);\n\n const launchModal: LaunchModal = (render, resolvers,
|
|
1
|
+
{"version":3,"file":"index.umd.js","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n type SyntheticEvent \n} from 'react';\nimport { \n type ModalStackItem,\n type ModalContextProperties,\n type LaunchModal,\n type LaunchModalResolvers,\n type ModalState,\n type UpdateModalState,\n} from '../types';\n\nexport const ModalContext = createContext<ModalContextProperties>({\n stack: [],\n launchModal: () => {},\n updateStack: () => {},\n updateState: () => {},\n});\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem[]>([]);\n\n const launchModal: LaunchModal = (render, resolvers, props = {}) => {\n // Wrap resolvers to update the stack\n const wrappedResovers = Object.keys(resolvers).reduce((acc, key) => {\n const resolver = resolvers[key];\n acc[key] = async (...args: any[]) => {\n const result = await resolver(...args);\n // update stack with an array conating all the items in the stack except the last one\n updateStack(currStack => currStack.slice(0, -1));\n return result;\n };\n return acc;\n }, {} as LaunchModalResolvers);\n updateStack([\n ...stack,\n {\n render,\n resolvers: wrappedResovers,\n props,\n state: {},\n },\n ]);\n };\n\n const updateState = (newState: ModalState) => {\n const updatedStack = [\n ...stack,\n ];\n updatedStack[stack.length - 1].state = { ...newState };\n updateStack(updatedStack);\n };\n\n const contextProperties: ModalContextProperties = {\n stack,\n launchModal,\n updateStack,\n updateState,\n };\n\n return <ModalContext.Provider value={contextProperties}>{children}</ModalContext.Provider>;\n\n};\n\n/**\n * Get properties from the modal context\n * @returns {ModalContextProperties} Object with the modal context properties\n */\nexport function useModalContext(): ModalContextProperties {\n return useContext(ModalContext);\n}\n\n/**\n * Short-hand for `useModalContext().launchModal`\n * @returns {LaunchModal} The launchModal function\n */\nexport function useModal(): LaunchModal {\n const { launchModal } = useModalContext();\n return launchModal;\n}\n\n/**\n * Get access to the current modal state and a function to update it\n * @returns {[ ModalState, UpdateModalState ]} A tuple with the current modal state and a function to update it\n */\nexport function useModalState(): [ ModalState, UpdateModalState ] {\n const { stack, updateState } = useModalContext();\n if (stack.length === 0) \n throw new Error('Trying to access modal state while no modal is open');\n const state = stack[stack.length - 1].state;\n return [ state, updateState ];\n}\n","import { \n type ModalPortProps, \n type ModalStackItem \n} from \"../types\";\nimport { useModalContext } from \"./context\";\nimport { \n type SyntheticEvent, \n useCallback, \n useEffect, \n useMemo\n} from \"react\";\n\nconst ModalPort: React.FC<ModalPortProps> = ({\n onModalLaunch,\n onModalClose,\n render: Backdrop\n}) => {\n\n const { stack } = useModalContext();\n const currentModal: ModalStackItem | null = useMemo(\n () => stack.length > 0 ? stack[stack.length - 1] : null,\n [ stack ]\n );\n const onBackdropClick = useCallback((ev: SyntheticEvent) => {\n if (ev.currentTarget !== ev.target) return;\n currentModal?.resolvers.onBackdropClickUse &&\n currentModal.resolvers.onBackdropClickUse(ev);\n }, [ currentModal ]);\n\n useEffect(() => {\n if (currentModal) {\n onModalLaunch && onModalLaunch();\n } else {\n onModalClose && onModalClose();\n }\n }, [ currentModal, onModalLaunch, onModalClose ]);\n\n return currentModal && (\n <Backdrop onBackdropClick={onBackdropClick}>\n {currentModal.render({\n ...currentModal.resolvers,\n ...currentModal.props,\n })}\n </Backdrop>\n ) || null;\n};\n\nexport default ModalPort;\n"],"names":["ModalContext","createContext","stack","launchModal","updateStack","updateState","useModalContext","useContext","_ref","children","_useState","useState","contextProperties","render","resolvers","props","wrappedResovers","Object","keys","reduce","acc","key","resolver","Promise","resolve","apply","slice","call","arguments","then","result","currStack","e","reject","concat","state","newState","updatedStack","length","_extends","_jsx","Provider","value","onModalLaunch","onModalClose","Backdrop","currentModal","useMemo","onBackdropClick","useCallback","ev","currentTarget","target","onBackdropClickUse","useEffect","_useModalContext2","Error"],"mappings":"4iBAeaA,EAAeC,EAAaA,cAAyB,CAChEC,MAAO,GACPC,YAAa,WAAQ,EACrBC,YAAa,WAAQ,EACrBC,YAAa,WAAK,aAqDJC,IACd,OAAOC,EAAAA,WAAWP,EACpB,wBApDoC,SAAHQ,GAAM,IAAAC,EAAQD,EAARC,SAErCC,EAA+BC,EAAAA,SAA2B,IAAlDT,EAAKQ,EAAEN,GAAAA,EAAWM,EAE1B,GA+BME,EAA4C,CAChDV,MAAAA,EACAC,YAjC+B,SAACU,EAAQC,EAAWC,QAAAA,IAAAA,IAAAA,EAAQ,CAAA,GAE3D,IAAMC,EAAkBC,OAAOC,KAAKJ,GAAWK,OAAO,SAACC,EAAKC,GAC1D,IAAMC,EAAWR,EAAUO,GAO3B,OANAD,EAAIC,kBAAgCE,OAAAA,QAAAC,QACbF,EAAQG,WAAA,E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AXlD,YACf,GAAqB,IAAjBH,EAAMoC,OACR,MAAU,IAAAkB,MAAM,uDAElB,MAAO,CADOtD,EAAMA,EAAMoC,OAAS,GAAGH,MACtB9B,EAClB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-modal-port",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Launch modals from any React component.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "src/index.ts",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
],
|
|
21
21
|
"scripts": {
|
|
22
22
|
"build": "npm run build:esm && npm run build:esm-modern && npm run build:cjs && npm run build:umd",
|
|
23
|
-
"build:esm": "microbundle -o dist/index.mjs --format esm",
|
|
24
|
-
"build:esm-modern": "microbundle -o dist/index.modern.mjs --format modern",
|
|
25
|
-
"build:cjs": "microbundle -o dist/index.cjs --format cjs",
|
|
26
|
-
"build:umd": "microbundle -o dist/index.umd.js --format umd",
|
|
23
|
+
"build:esm": "microbundle -o dist/index.mjs --format esm --globals react/jsx-runtime=jsx",
|
|
24
|
+
"build:esm-modern": "microbundle -o dist/index.modern.mjs --format modern --globals react/jsx-runtime=jsx",
|
|
25
|
+
"build:cjs": "microbundle -o dist/index.cjs --format cjs --globals react/jsx-runtime=jsx",
|
|
26
|
+
"build:umd": "microbundle -o dist/index.umd.js --format umd --globals react/jsx-runtime=jsx",
|
|
27
27
|
"prepare": "npm run build",
|
|
28
28
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
29
29
|
},
|
|
@@ -48,13 +48,13 @@
|
|
|
48
48
|
"url": "https://github.com/oliverwehn/react-modal-port/issues"
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://github.com/oliverwehn/react-modal-port#readme",
|
|
51
|
+
"dependencies": {},
|
|
51
52
|
"devDependencies": {
|
|
53
|
+
"react": "^18",
|
|
54
|
+
"react-dom": "^18",
|
|
52
55
|
"@types/react": "^18",
|
|
53
56
|
"@types/react-dom": "^18.2.19",
|
|
54
57
|
"microbundle": "^0.15.1",
|
|
55
|
-
"react": "^18.2.0",
|
|
56
|
-
"react-dom": "^18",
|
|
57
|
-
"tslib": "^2.6.2",
|
|
58
58
|
"typescript": "^5.3.3"
|
|
59
59
|
},
|
|
60
60
|
"peerDependencies": {
|
package/readme.md
CHANGED
|
@@ -84,10 +84,14 @@ const Page: React.FC = () => {
|
|
|
84
84
|
{
|
|
85
85
|
decideYay: () => setDecision(true),
|
|
86
86
|
decideNay: () => setDecision(false),
|
|
87
|
-
|
|
87
|
+
// Provide an `onBackdropClick` resolver to have it passed to
|
|
88
|
+
// the Backdrop component
|
|
89
|
+
onBackdropClick: () => {},
|
|
88
90
|
},
|
|
89
|
-
//
|
|
90
|
-
|
|
91
|
+
// Optionally, add further props to be passed to the modal component
|
|
92
|
+
{
|
|
93
|
+
timeLeft: Math.random() * 86400
|
|
94
|
+
}
|
|
91
95
|
);
|
|
92
96
|
}
|
|
93
97
|
|
|
@@ -113,7 +117,7 @@ The `launchModal` function expects the following arguments:
|
|
|
113
117
|
|----------|------|-------------|
|
|
114
118
|
| render | `React.FC` | A function that renders the modal and returns a `ReactNode`. |
|
|
115
119
|
| resolvers | `{ [key: string]: (...args: any[]) => Promise<void>\|void }` | An object of resolver functions that will be passed as props to `render`. |
|
|
116
|
-
|
|
|
120
|
+
| props | `{ [key: string]: any }` | Optional: Further props you’d like to pass to the modal component in `render`. |
|
|
117
121
|
|
|
118
122
|
Modals can be created and designed to fully fit your needs. The only requirement is to define the functions to resolve them, for example, when clicking a button. The modal component for the example above could look like this:
|
|
119
123
|
|
|
@@ -170,8 +174,6 @@ So you could adapt the `onClick` handler in our earlier example as follows:
|
|
|
170
174
|
letMeThinkAboutIt: (p: Promise) => p,
|
|
171
175
|
leaveMeAlone: () => {},
|
|
172
176
|
},
|
|
173
|
-
// Optional name of the `onBackdropClick` resolver
|
|
174
|
-
"leaveMeAlone"
|
|
175
177
|
);
|
|
176
178
|
}
|
|
177
179
|
|
|
@@ -262,9 +264,17 @@ export const AskForNameModal: React.FC<AskForNameModalProps> = ({
|
|
|
262
264
|
};
|
|
263
265
|
|
|
264
266
|
const onButtonClick = () => {
|
|
265
|
-
launchModal(
|
|
266
|
-
|
|
267
|
-
|
|
267
|
+
launchModal(
|
|
268
|
+
ConfirmModal,
|
|
269
|
+
{
|
|
270
|
+
// Chain first modal’s resolution to the second one’s
|
|
271
|
+
confirm: () => provideName(modalState.name),
|
|
272
|
+
},
|
|
273
|
+
// Pass the name on to the ConfirmModal for rendering
|
|
274
|
+
{
|
|
275
|
+
name: modalState.name,
|
|
276
|
+
}
|
|
277
|
+
);
|
|
268
278
|
};
|
|
269
279
|
|
|
270
280
|
return (
|
package/types/index.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
2
|
|
|
3
|
+
import { PropsWithChildren } from "react";
|
|
4
|
+
|
|
3
5
|
export type ModalResolver = (...args: any[]) => Promise<void>|void;
|
|
4
6
|
|
|
5
7
|
|
|
@@ -7,17 +9,21 @@ export type LaunchModalResolvers = {
|
|
|
7
9
|
[key: string]: ModalResolver;
|
|
8
10
|
};
|
|
9
11
|
|
|
12
|
+
export type LaunchModalProps = {
|
|
13
|
+
[key: string]: any;
|
|
14
|
+
};
|
|
15
|
+
|
|
10
16
|
export type ModalStackItem = {
|
|
11
17
|
render: React.FunctionComponent<LaunchModalResolvers>;
|
|
12
18
|
resolvers: LaunchModalResolvers;
|
|
13
|
-
|
|
19
|
+
props: LaunchModalProps;
|
|
14
20
|
state: ModalState;
|
|
15
21
|
};
|
|
16
22
|
|
|
17
23
|
export type LaunchModal = (
|
|
18
24
|
render: React.FunctionComponent<LaunchModalResolvers>,
|
|
19
25
|
resolvers: LaunchModalResolvers,
|
|
20
|
-
|
|
26
|
+
props?: LaunchModalProps,
|
|
21
27
|
) => void;
|
|
22
28
|
|
|
23
29
|
export type ModalState = { [key: string]: any };
|