react-modal-port 0.2.6 → 1.0.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.
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.umd.js","sources":["../src/context.tsx","../src/modal-port.tsx"],"sourcesContent":["import { \n createContext, \n useContext, \n useState, \n useCallback,\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 ModalProps,\n} from './types';\n\nexport const ModalContext = createContext<ModalContextProperties | null>(null);\n\nexport const ModalContextProvider = ({ children }: Readonly<{ children: React.ReactNode }>) => {\n\n const [ stack, updateStack ] = useState<ModalStackItem<any>[]>([]);\n\n const launchModal = useCallback(<P extends Record<string, any> = Record<string, any>>(\n render: React.FunctionComponent<ModalProps<P>>, \n resolvers: LaunchModalResolvers,\n props: any = {}\n ) => {\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 containing 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\n updateStack(prevStack => [\n ...prevStack,\n {\n render,\n resolvers: wrappedResovers,\n props,\n state: {},\n },\n ]);\n }, []);\n\n const updateState = useCallback((newState: ModalState) => {\n updateStack(prevStack => {\n const updatedStack = [...prevStack];\n if (updatedStack.length > 0) {\n updatedStack[updatedStack.length - 1] = {\n ...updatedStack[updatedStack.length - 1],\n state: { ...newState }\n };\n }\n return updatedStack;\n });\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 const context = useContext(ModalContext);\n if (!context) {\n throw new Error('useModalContext must be used within a ModalContextProvider');\n }\n return context;\n}\n\n/**\n * Short-hand for `useModalContext().launchModal`\n * @returns {LaunchModal} The launchModal function\n */\nexport function useModal() {\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 | null, UpdateModalState ]} A tuple with the current modal state and a function to update it\n */\nexport function useModalState(): [ ModalState | null, UpdateModalState ] {\n const { stack, updateState } = useModalContext();\n if (stack.length === 0) {\n return [null, updateState];\n }\n const state = stack[stack.length - 1].state;\n return [ state, updateState ];\n}\n","import { \n ModalProps,\n type ModalPortProps, \n type ModalStackItem \n} from \"./types\";\nimport { useModalContext } from \"./context\";\nimport { \n type SyntheticEvent, \n useCallback, \n useEffect, \n useMemo,\n useRef,\n type ReactElement\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<ModalProps> | null = useMemo(\n () => stack.length > 0 ? stack[stack.length - 1] : null,\n [ stack ]\n );\n \n const previousModalRef = useRef<ModalStackItem<ModalProps> | null>(null);\n \n const onBackdropClick = useCallback((ev: SyntheticEvent) => {\n if (ev.currentTarget !== ev.target) return;\n if (currentModal?.resolvers.onBackdropClick) {\n currentModal.resolvers.onBackdropClick(ev);\n }\n }, [ currentModal ]);\n\n useEffect(() => {\n const previousModal = previousModalRef.current;\n \n // Modal was launched (went from no modal to having a modal)\n if (!previousModal && currentModal) {\n onModalLaunch?.();\n }\n // Modal was closed (went from having a modal to no modal)\n else if (previousModal && !currentModal) {\n onModalClose?.();\n }\n \n // Update the ref for next time\n previousModalRef.current = currentModal;\n }, [ currentModal, onModalLaunch, onModalClose ]);\n\n const ModalContent = currentModal?.render;\n\n return (\n <>\n {ModalContent && (\n <Backdrop onBackdropClick={onBackdropClick}>\n <ModalContent\n {...currentModal.resolvers}\n {...currentModal.props}\n />\n </Backdrop>\n ) || null}\n </>\n );\n};\n\nexport default ModalPort;\nexport { ModalPort };\n"],"names":["ModalContext","createContext","useModalContext","context","useContext","Error","_ref","children","_useState","useState","stack","updateStack","launchModal","useCallback","render","resolvers","props","wrappedResovers","Object","keys","reduce","acc","key","resolver","Promise","resolve","apply","slice","call","arguments","then","result","currStack","e","reject","prevStack","concat","state","updateState","newState","updatedStack","length","_extends","_jsx","Provider","value","onModalLaunch","onModalClose","Backdrop","currentModal","useMemo","previousModalRef","useRef","onBackdropClick","ev","currentTarget","target","useEffect","previousModal","current","ModalContent","_Fragment","jsx","_useModalContext2"],"mappings":"giBAiBaA,EAAeC,EAAaA,cAAgC,MA8DzD,SAAAC,IACd,IAAMC,EAAUC,EAAUA,WAACJ,GAC3B,IAAKG,EACH,MAAU,IAAAE,MAAM,8DAElB,OAAOF,CACT,wBAlEoC,SAAHG,GAA6D,IAAvDC,EAAQD,EAARC,SAErCC,EAA+BC,EAAQA,SAAwB,IAAvDC,EAAKF,EAAA,GAAEG,EAAWH,EAE1B,GAAMI,EAAcC,EAAAA,YAAY,SAC9BC,EACAC,EACAC,QAAAA,IAAAA,IAAAA,EAAa,IAGb,IAAMC,EAAkBC,OAAOC,KAAKJ,GAAWK,OAAO,SAACC,EAAKC,GAC1D,IAAMC,EAAWR,EAAUO,GAO3B,OANAD,EAAIC,GAAI,WAAA,IAA4B,OAAAE,QAAAC,QACbF,EAAQG,WAAAC,EAAAA,GAAAA,MAAAC,KADKC,aACIC,KAAhCC,SAAAA,GAGN,OADApB,EAAY,SAAAqB,GAAa,OAAAA,EAAUL,MAAM,GAAI,EAAE,GACxCI,CAAO,EAChB,CAAC,MAAAE,UAAAT,QAAAU,OAAAD,EAAA,CAAA,EACMZ,CACT,EAAG,CAAA,GAEHV,EAAY,SAAAwB,GAAS,MAAA,GAAAC,OAChBD,EACH,CAAA,CACErB,OAAAA,EACAC,UAAWE,EACXD,MAAAA,EACAq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UACbkC,EAAajC,WAGlB,MAGX,wBD2BE,OADwBd,IAAhBU,WAEV,iDAOE,IAAAmD,EAA+B7D,IAAvBQ,EAAKqD,EAALrD,MAAO4B,EAAWyB,EAAXzB,YACf,OAAqB,IAAjB5B,EAAM+B,OACD,CAAC,KAAMH,GAGT,CADO5B,EAAMA,EAAM+B,OAAS,GAAGJ,MACtBC,EAClB"}
@@ -1,20 +0,0 @@
1
- import { type ModalContextProperties, type LaunchModalResolvers, type ModalState, type UpdateModalState, ModalProps } from './types';
2
- export declare const ModalContext: import("react").Context<ModalContextProperties | null>;
3
- export declare const ModalContextProvider: ({ children }: Readonly<{
4
- children: React.ReactNode;
5
- }>) => import("react/jsx-runtime").JSX.Element;
6
- /**
7
- * Get properties from the modal context
8
- * @returns {ModalContextProperties} Object with the modal context properties
9
- */
10
- export declare function useModalContext(): ModalContextProperties;
11
- /**
12
- * Short-hand for `useModalContext().launchModal`
13
- * @returns {LaunchModal} The launchModal function
14
- */
15
- export declare function useModal(): <P extends Record<string, any> = Record<string, any>>(render: React.FunctionComponent<ModalProps<P>>, resolvers: LaunchModalResolvers, props?: import("./types").LaunchModalProps) => void;
16
- /**
17
- * Get access to the current modal state and a function to update it
18
- * @returns {[ ModalState | null, UpdateModalState ]} A tuple with the current modal state and a function to update it
19
- */
20
- export declare function useModalState(): [ModalState | null, UpdateModalState];
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.tsx"],"names":[],"mappings":"AAOA,OAAO,EAEL,KAAK,sBAAsB,EAC3B,KAAK,WAAW,EAEhB,KAAK,UAAU,EACf,KAAK,gBAAgB,EACtB,MAAM,UAAU,CAAC;AAElB,eAAO,MAAM,YAAY,wDAAqD,CAAC;AAE/E,eAAO,MAAM,oBAAoB,GAAI,cAAc,QAAQ,CAAC;IAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,CAAC,4CAkDzF,CAAC;AAEF;;;GAGG;AACH,wBAAgB,eAAe,IAAI,sBAAsB,CAMxD;AAED;;;GAGG;AACH,wBAAgB,QAAQ,IAAI,WAAW,CAGtC;AAED;;;GAGG;AACH,wBAAgB,aAAa,IAAI,CAAE,UAAU,GAAG,IAAI,EAAE,gBAAgB,CAAE,CAOvE"}
package/types/index.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export { ModalContextProvider, useModalContext, useModal, useModalState, } from './context';
2
- export { ModalPort } from './modal-port';
3
- export type { ModalResolver, LaunchModalResolvers, LaunchModalProps, ModalProps, ModalStackItem, LaunchModal, ModalState, UpdateModalState, ModalContextProperties, ModalPortRenderProps, ModalPortProps } from './types';
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,eAAe,EACf,QAAQ,EACR,aAAa,GACd,MAAM,WAAW,CAAC;AAEnB,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,cAAc,CAAC"}
@@ -1,4 +0,0 @@
1
- import { type ModalPortProps } from "./types";
2
- declare const ModalPort: React.FC<ModalPortProps>;
3
- export default ModalPort;
4
- export { ModalPort };
@@ -1 +0,0 @@
1
- {"version":3,"file":"modal-port.d.ts","sourceRoot":"","sources":["../src/modal-port.tsx"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,cAAc,EAEpB,MAAM,UAAU,CAAC;AAWlB,QAAA,MAAM,SAAS,EAAE,KAAK,CAAC,EAAE,CAAC,cAAc,CA+DvC,CAAC;AAEF,eAAe,SAAS,CAAC;AACzB,OAAO,EAAE,SAAS,EAAE,CAAC"}
@@ -1 +0,0 @@
1
- import '@testing-library/jest-dom';
package/types/types.d.ts DELETED
@@ -1,34 +0,0 @@
1
- import { type SyntheticEvent } from "react";
2
- export type ModalResolver = (...args: any[]) => Promise<void> | void;
3
- export type LaunchModalResolvers = {
4
- [key: string]: ModalResolver;
5
- };
6
- export type LaunchModalProps = {
7
- [key: string]: any;
8
- };
9
- export type ModalProps<P = Record<string, any>> = P;
10
- export type ModalStackItem<P extends Record<string, any> = Record<string, any>> = {
11
- render: React.FunctionComponent<ModalProps<P>>;
12
- resolvers: LaunchModalResolvers;
13
- props: LaunchModalProps;
14
- state: ModalState;
15
- };
16
- export type LaunchModal<P extends Record<string, any> = Record<string, any>> = (render: React.FunctionComponent<ModalProps<P>>, resolvers: LaunchModalResolvers, props?: LaunchModalProps) => void;
17
- export type ModalState = {
18
- [key: string]: any;
19
- };
20
- export type UpdateModalState = (newState: ModalState) => void;
21
- export type ModalContextProperties = {
22
- stack: ModalStackItem<any>[];
23
- launchModal: <P extends Record<string, any> = Record<string, any>>(render: React.FunctionComponent<ModalProps<P>>, resolvers: LaunchModalResolvers, props?: LaunchModalProps) => void;
24
- updateStack: (newStack: ModalStackItem<any>[]) => void;
25
- updateState: (newState: ModalState) => void;
26
- };
27
- export type ModalPortRenderProps = React.PropsWithChildren & {
28
- onBackdropClick?: (ev: SyntheticEvent) => void;
29
- };
30
- export type ModalPortProps = {
31
- onModalLaunch?: () => void | null;
32
- onModalClose?: () => void | null;
33
- render: React.FC<ModalPortRenderProps>;
34
- };