Git Product home page Git Product logo

react-fade-modal's Introduction

React Fade Modal

npm bundle size Snyk Vulnerabilities for npm package

Responsive react modal with fade in-out effect included without any external libraries.

works with: React >= 16.8

Motivation

Most famous react modals refers to real DOM.
This modal is 100% purely made using virtual DOM instead.
Also, you can customize the UI based on your needs without worrying about css conflicts, thanks to the :where css pseudo-class and module.css.
Not less important, extreme small size.

Getting Started

You can install the module via npm or yarn:

npm install react-fade-modal
yarn add react-fade-modal

Demo

Live Demo

Usage

Basic example

import {Modal} from 'react-fade-modal'

function App() {
    const [isOpen, setIsOpen] = useState(false)
    return (
       <div>
           <p>
               <div>Basic example</div>
               <button onClick={() => setIsOpen(true)}>click to open modal</button>
               {isOpen &&
                   <Modal
                       title={"Title"}
                       setIsOpen={setIsOpen}>
                       <div>
                           <p>Modal content.</p>
                       </div>
                   </Modal>
               }
           </p>      
       </div>
    );
}

Note that Modal does not accept an isOpen prop. You must put the component into a conditional wrap, so it will be unmounted and mounted every time the condition change.

ModalProvider Example

Sometimes, Modal does not render well when you are using in a complex nested DOM component.

In this case, it's better to use ModalProvider in order to ensure that the Modal is on top of the DOM and the graphic does not change:

import {ModalProvider} from 'react-fade-modal'

function App() {
    return (
        <ModalProvider>
            <Panel/>
        </ModalProvider>
    );
}

In this example, App.js render <Panel/> only. Let's call useModalContext:

import {useModalContext} from 'react-fade-modal'

const Panel = () => {
    const {showModal} = useModalContext()
    return <p>
        {items.map(item =>
            <Button onClick={() => {
                showModal({
                    title: 'Custom title',
                    children: <div>You can pass any component. This is the Item {item}</div>,
                    closeOnClickOutside: false
                })
            }}>
                item {item}
            </Button>)}
    </p>
}

Note that I could also call the context inside any child of <Panel/>, that's how the context work basically.

showModal is a function that accept any Modal props. You can pass props as object properties as shown in the example above.

More examples available on demo/src/App.tsx

Props

Name Type Default Description
setIsOpen Function required Show the modal. Accept 1 boolean value
transitionDurationInMs number 200 Transition duration in milliseconds. Used for fade in-out the Modal
title string '' Title of the Modal
children ReactNode undefined Any JSX Component
closeOnClickOutside boolean true If true, the Modal will closed when you click outside
containerCss string '' Optional CSS Classes applied to Modal container. A grey background layer will be shown as default
modalCss string '' Optional CSS classes applied to the Modal itself
modalStyle CSSProperties {} Optional style applied to the Modal itself
titleCss string '' Optional CSS classes applied to the title
contentCss string '' Optional CSS classes applied to the children
closeIconCss string '' Optional CSS classes applied to the close icon
showCloseIcon boolean true If true, it will be rendered the default close icon
customCloseIcon ReactElement undefined If not undefined, this component will be rendered instead of the default close icon

No CSS Conflicts

Every css classes used in this component are written into module.css file and wrapped into :where pseudo-class, which gives them 0 specificity, so you can override current css properties through your custom css classes.
More about :where pseudo-class, Specificity and the :where exception
More about css modules

Project Structure

The project includes a demo folder initialized with Create React App.

When you run the command build from first-level package.json, a dist and a lib folder will be generated.

The lib folder will be placed automatically into the demo project.

You can move into demo directory and start project from its own package.json script, just like any other Create React App.

Questions

๐Ÿ› Did you find any bugs? Cool!
Report it in "issues" section under the label "bug" and I'll check it.

๐Ÿš€ Do you have any questions or feature request? Nice!
Write it into "issues" section under the label "enhancement" and I'll check it.

โญ Did you find this package useful and meet your requirements? Great!
Consider to put a โญ into the GitHub project!

License

MIT

react-fade-modal's People

Contributors

emanuelecaurio avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.