Code Monkey home page Code Monkey logo

react-yet-another-modal's Introduction

react-yet-another-modal

NPM

React modal which is dead simple, semantic, flexible.

Showcase

Features

  • transports modal content into a new React component and appends it to the document.body (creates a new independent React tree)
  • automatically resize modal to fit your screen size change
  • can be opened by the prop isOpened
  • can be opened after a click on an element that you pass through the prop link (and then it takes care of the open/close state)
  • doesn't leave any mess in DOM after closing
  • provides its child with this.props.closePortal callback
  • can be closed by closeOnEsc and closeOnOutsideClick
  • handle modal nesting style and all their z-index automatically

Demo

Try [https://react-yet-another-modal] or

git clone https://github.com/chenzhihao/react-yet-another-modal
cd react-yet-another-modal
npm install
npm run dev
open localhost:8080

Installation

npm install react-yet-another-modal --save

Usage

import React from 'react';
import ReactDOM from 'react-dom';
import Modal from 'react-yet-another-modal';

export default class App extends React.Component {
  render() {
    return
      <Modal
        title="Yet another modal"
        link={<button>Open a modal</button>}
      >
        <div>
          here we go, your modal content should be here.
        </div>
      </Modal>
    );
  }
}
ReactDOM.render(<App />, document.getElementById('react-body'));

Documentation - props

Always required

children : ReactElement

The portal expects one child (<Modal><Child ... /></Modal>) that will be ported.

link : ReactElement

The Modal button/text or any ReactElement which is used to open the modal. This element will be rendered by the Modal immediately.

Optional

isOpened : bool

If true, the modal is open. If false, the portal is closed. It's up to you to take care of the closing (aka taking care of the state). Don't use this prop if you want to make your life easier. Use link instead!

beforeCloseCallback : function

The callback before the Modal close.

closeOnEsc: bool

If true, the Modal can be closed by the key ESC.

closeOnOutsideClick: bool

If true, the Modal can be closed by the outside mouse click.

onOpen: func(DOMNode)

This callback is called when the Modal is opened and rendered (useful for animating the DOMNode).

beforeClose: func(DOMNode)

This callback is called before Modal close. You can do some DOMNode animation first and it will removes itself from DOM.

onClose: func

This callback is called when the portal closes and after beforeClose.

Tips & Tricks

  • Close modal programmatically

Sometimes you need to close your modal programmatically. A props closePortal is injected into your Modal Content Component automatically directly.

  let ProgrammaticallyCloseModal = props => {
    return (<div>
      <section>
        <button onClick={()=>this.props.closePortal()}>
          Close
        </button>
      </section>
    </div>);
  }

  ...

  <Modal title="My modal">
    <ProgrammaticallyCloseModal />
  </Modal>

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.