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-modal-classic

v0.0.45

Published

An easy-to-use and classic javascript React modal, that allows you to pop out “one modal at a time” in your React App.

Downloads

61

Readme

React Modal Classic

An easy-to-use and classic javascript React modal, that allows you to pop out “one modal at a time” in your React App. See it in action here.

Why this module?

This module can be used for your basic modal needs, like:

  • showing a confirmation message
  • collecting informations with a form
  • showing a login form
  • visualize a media
  • getting informations on a specific element…

It can show every content you need, following this idea: “one modal at a time”… which means, that you can't open more than one modal at the same time in the viewport.

How to install?

This module can be installed from npm using this command line:

npm i react-modal-classic

Dependencies:

Your React projet should run with these dependencies:

  • Styled Components (version ≥6.0.4)
  • React (version ≥18.0.0)
  • React Dom (version ≥18.0.0)

How to use?

1- Set the modal context provider:

This modal runs with the Context API of React, so your first task is to set the ModalProvider component in your application.

Case 1: If your application use React Router library, and you want to be able to use the NavLink component in your modal content, ModalProvider should be set inside the BrowserRouter component.

import { BrowserRouter, Routes } from "react-router-dom"
import { ModalProvider } from "react-modal-classic"

function Router(){
    return (
        <BrowserRouter>
            <ModalProvider>
                <Routes>
                    {/* ... */}
                </Routes>
            </ModalProvider>
        </BrowserRouter>
    )
}

export default Router

Case 2: Else, you don't need router components in your modal contents, you can just set the ModalProvider component in main.jx:

import React from "react"
import ReactDOM from "react-dom/client"
import { ModalProvider } from "react-modal-classic"
import App from "./App"

ReactDOM.createRoot(document.getElementById('root')).render(
    <React.StrictMode>
        <ModalProvider>
            <App />
        </ModalProvider>
    </React.StrictMode>
)

2- Open the modal with a function:

Now, you can launch the modal everywhere in your app, directly from a function named openModal(), by setting the content of the modal as a parameter. The openModal() function has to be imported from ModalContext, that you have previously set with the ModalProvider component.

import React, { useContext } from "react"
import { ModalContext } from "react-modal-classic"
import ModalContent from "./components/ModalContent"

const App = () => {
    const { openModal } = useContext(ModalContext)
    return (
        <main>
            <Button onClick={() => openModal(<ModalContent/>)}>Open modal</Button>
        </main>
    )
}

export default App

3- Add a callback function when closing modal:

You can pass a callback function as a parameter in the the openModal() function. This function triggers when the user close the modal, and is optional.

import React, { useContext } from 'react'
import styled from "styled-components"
import { ModalContext } from './lib/ModalContext'
import ModalContent from './components/ModalContent'

const App = () => {
    const { openModal } = useContext(ModalContext)
    const onCloseCallback = (name) => {console.log("Hello " + name)}
    return (
        <main>
            <Button onClick={() => openModal(<ModalContent/>, () => onCloseCallback("Robin!"))}>Open modal</Button>
        </main>
    )
}
export default App

4- Customize the look of your modals:

a- Options by default

Right from the box, the modal component is set with these default options:

const defaultOptions = {
    closeButton: "out", // has to be "out", "in" or "none"
    closeButtonColor: "#fff", // should be a color value "red", "rgba(255,0,0,0.5)", "#242424"…
    size: "md", // has to be "sm", "md", "lg", "xl"
    backgroundColor: "#fff", // should be a color value "pink", "rgba(120,120,120,0.8)", "#242424"…
    radius: "6px" // should be a size value "5px", "0.5rem"…
}

b- Set different options in the function

You can pass options as a parameter object in the openModal() function. It can be usefull if you have different kinds of contents in your app that need to be shown in a modal. This is optional. Here are two examples:

import React, { useContext } from "react"
import { ModalContext } from "react-modal-classic"
import ModalContentForm from "./components/ModalContentForm"
import ModalContentMedia from "./components/ModalContentMedia"

const App = () => {
    
    const { openModal } = useContext(ModalContext)
    const modalFormOptions = { closeButton: "in", backgroundColor: "#999", size: "md" }
    const modalMediaOptions = { closeButton: "none", size: "xl", radius: "none" }
    const onCloseCallback = (name) => {console.log("Hello " + name)}

    return (
        <main>
            <button onClick={() => openModal(<ModalContentForm/>, modalFormOptions)}>Open a form</button>
            <button onClick={() => openModal(<ModalContentMedia/>, () => onCloseCallback("Robin!"), modalMediaOptions)}>Open a media</button>
        </main>
    )
}

export default App

Note that in the second example, it is possible to use 3 parameters in the openModal() function :

  • the content as a JSX component (required)
  • a callback function that triggers when user close the modal (optional)
  • an object that defines styles of the modal (optional)

5- Close the modal

Modal can be closed by :

  • clicking the close button (close icon at the top right corner of the modal)
  • clicking somewhere in the background
  • pressing the escape key of your keyboard

Note that if your application requires a function to close the modal, you can import it from ModalContext to do so:

import React, { useContext } from 'react'
import { ModalContext } from "react-modal-classic"

function ModalContent(){
    
    const { closeModal } = useContext(ModalContext)

    return (
        <div>
            <p>This React modal is based on a component and a context provider.<br/>
                <button onClick={closeModal}>Close</button>
            </p>
        </div>
    )
}

export default ModalContent

License

This module is available under MIT licence, feel free to use it in your React web projects.