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

react-hook-modal

v1.0.8

Published

A custom modal, with a custom hook for modal handling in React App

Downloads

37

Readme

react-hook-modal

A custom modal, with a custom hook for handling modals in React applications. This package allows us to centralize the logic for rendering modal components. Through a hook, we can dynamically render any coponent in this modal. This gives us flexibility in our components since we would not have to handle local states for modal rendering. For that we use the hook useModal

Install

npm install --save react-hook-modal

Usage

The "react-hook-modal" api consists of a hook: useModal, a component containing the modals: Modal, and a HOC ModalDataContextProvider. react-hook-modal, uses the react Context api to exchange states and information to be rendered in the modal through all the components of our application.

1. Declare the HOC ModalDataContextProvider to wrap the entire application

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App/App';
////////////////////////////////////
import { ModalDataContextProvider } from 'react-hook-modal';
////////////////////////////////////

ReactDOM.render(
  ////////////////////////////////////
  <ModalDataContextProvider>
    <App />
  </ModalDataContextProvider>,
  ////////////////////////////////////
  document.getElementById('root')
);

2. Put the Modal component in the container component, and add the styles e.g. App.

 ...
 import { Modal, useModal } from 'react-hook-modal';
 import 'react-hook-modal/dist/index.css';
 ...
 return (
    <div className='App'>
      <Header />
      {/* Modal section here */}
      <Modal />
    </div>
  );

3. Use the custom hook function useModal to access to the functions to interact to the modal:

  1. setComponentToRender(element: JSX.Element, options?: ModalOptions)
  2. closeModal(data?: any)
  3. setOptions(options?: ModalOptions)
  4. const setFooter(footer: JSX.Element | null)

Example:

...
import { Modal, useModal } from 'react-hook-modal';
...
  const { setComponentToRender, closeModal } = useModal(onCloseModal);

  function onCloseModal(data){
      console.log("Modal closed",data);
  }
  return (
   <button
        style={{ color: '#fff' }}
        type="button"
        onClick={() => {
          setComponentToRender(<AccountCustomComponent />, {
            title: 'My Account',
            animation: true,
            closeOnBackgroundOrEsc: false,
          });
        }}
      >
        Open Modal
      </button>
  )

The Modal component has props to customize the styles of the container, header and body sections. here an example of setting different styles:

<Modal
  styles={{
    container: {
      backgroundColor: '#1C1C1C',
      color: '#fff',
      width: '55rem',
      maxHeight: '100vh',
      maxWidth: '100vw'
    },
    header: { backgroundColor: '#2C2C2C', color: '#fff' },
    body: { maxHeight: '80vh', padding: '0rem' }
  }}
/>

The Modal component has a prop called classContainer which defines a custom container Modal class but it is recommended include the following css styles.

.app-modal {
  position: fixed;
  max-height: 100vh;
  max-width: 100vw;
  z-index: 999;
  top: 50%;
  left: 50%;
  box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.2);
  transform: translate(-50%, -50%);
  background-color: white;
  color: inherit;
  width: 50rem;
}

The setComponentToRender(element: JSX.Element, options?: ModalOptions) function takes a JSX.Element which can be a component or just an 'html' and renders it inside the body of the Modal.

options allow you to dynamically set the attributes and properties of the modal to be represented.

export interface ModalOptions {
  title?: string; // A title to display in the header
  dataProps?: any; // An object passed to the Modal state to share data: we can access **const {dataToProps} = useModal()
  width?: string | number; // Set the width for the modal
  height?: string | number; // Set the height for the modal
  closeOnBackgroundOrEsc?: boolean | undefined; // Closes the modal if we click outside the modal context or if we press the Esc key.
  resizable?: boolean; // Make a resizable Modal
  fullScreen?: boolean; // Complete viewport size
  animation?: boolean; // Make a nice animation on open the modal
  footer?: JSX.Element | null | undefined; // A component for the footer
  darkMode?: boolean; // Enable dark mode
}

image-1 image-2

Demo

License

MIT © josealejandro2928