0.2.9 • Published 4 years ago

@marwan38/react-hook-modal v0.2.9

Weekly downloads
15
License
ISC
Repository
github
Last release
4 years ago

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;
}
0.2.7

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.2

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago