npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@marwan38/react-hook-modal

v0.2.9

Published

React modal hook making modals with state a breeze

Downloads

9

Readme

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