@marwan38/react-hook-modal
TypeScript icon, indicating that this package has built-in type declarations

0.2.9 • Public • Published

React Hook Modal

Install:

npm i @marwan38/react-hook-modal
OR
yarn add @marwan38/react-hook-modal

Usage

// Modal gets required props automatically passed through the provider
const SampleModal: React.FC<IModalProps & { aProp: boolean }> = ({stateKey, close, aProp}) = {  
    const { dynamicData } = useModalState(stateKey);
    return (
        <div onClick={close}>
            I am Modal
        </div>
    );
}

const ModalLauncher = () => {
    const {open, close, setState} = useModal(
        SampleModal,
        {
            aProp: true
        }, // Static (unchangable) props. Useful for callbacks
        {
            dynamicData: []
        }, // Accessed with the useModalState hook. Set by the setState function
    );

    const handleClick = () => {
        
        // setState returns the state passed in the 2nd argument of useModal
        setState((state) => {
            // setState uses immer under the hood!
            // so mutate away!
            // make sure to not return the value
            state.dynamicData.push('whatever');
        })
    }
    return (
        <button onClick={handleClick}>Add to state</button>
        <button onClick={open}>Open modal</button>
    );
}

API

<ModalProvider />: Wrap the root of your app with this

useModal(Component, props, state);

const { open, close, setState } = useModal(ModalRoot, {...props}, {...state});

open(); // Launches the modal
close(); // Closes the modal
setState((draftState) => {}); Mutate the draftState

useModalState(stateKey);

You will have to manually pass an interface to it for typing. IE: useModalState<State>(stateKey)

// Use within the modal component to access its state
const state = useModalState(stateKey); // State key is passed down to the modal component

CURRENTLY UNAVAILABLE

I havent figured out how to treeshake this component as I don't want to include react-spring as part of the package.

ModalRoot (Helper component)

<ModalRoot stateKey={stateKey} close={close} />

interface ModalRootProps extends IModalProps {
  modalContainerAnim?: Animation;
  overlaySpringConfig?: SpringConfig;
  children: ((args: ModalRootChildProps) => React.ReactNode) | React.ReactNode;
}

Package Sidebar

Install

npm i @marwan38/react-hook-modal

Weekly Downloads

6

Version

0.2.9

License

ISC

Unpacked Size

148 kB

Total Files

17

Last publish

Collaborators

  • marwan38