@mr-web/react-modal

0.1.0 • Public • Published

@mr-web/react-modal

Build Status codecov

Fully customizable Modals for React

Contents

Installation

To install using npm:

npm install -P @mr-web/react-modal

or using yarn:

yarn add @mr-web/react-modal

Usage

This library is very basic. The developer has complete freedom with the appearance of the modals.

You need a ModalProvider, that wraps your app. You must supply a ModalContainer component to this provider:

// You can use any component as the container. Doing the styling is up to you.
const ModalContainer = () => <div className="modal-container"></div>

<ModalProvider ModalContainer={ModalContainer}>
  <App>
    My Stuff
  </App>
</ModalProvider>

Documentation

First create your ModalContainer and ModalContent components:

const ModalContainer = ({ children }) => (
  <div className="modal-container">{children}</div>
);

const ModalContent = ({ children, title }) => (
  <div className="modal">
    <header className="modal__header">{title}</header>
    <div className="modal__content">{children}</div>
  </div>
);

Add the modal to your app:

import React from "react";
import ReactDOM from "react-dom";
import { ModalProvider, Modal } from "@mr-web/react-modal";

const App = () => (
  <ModalProvider ModalContainer={ModalContainer}>
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
      <Modal>
        <ModalContent title="My first Modal">Welcome</ModalContent>
      </Modal>
    </div>
  </ModalProvider>
);

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Once you do so, you'll notice, that the modal is always shown. That is all the library does: show a Modal inside the ModalContainer, if there is a Modal component anywhere in the tree.

You'll have to add some logic for visibility:

// ...

class ModalShow extends React.PureComponent {
  constructor() {
    super();
    this.state = { show: false };
    this.onToggle = this.onToggle.bind(this);
  }

  onToggle() {
    this.setState({ show: !this.state.show });
  }

  render() {
    return (
      <React.Fragment>
        <button onClick={this.onToggle}>Show Modal</button>
        {this.state.show && (
          <Modal>
            <ModalContent title="My first Modal">
              <p>Welcome</p>
              <button onClick={this.onToggle}>Hide Modal</button>
            </ModalContent>
          </Modal>
        )}
      </React.Fragment>
    );
  }
}

const App = () => (
  <ModalProvider ModalContainer={ModalContainer}>
    <div className="App">
      <h1>Hello @mr-web/react-modal</h1>
      <ModalShow />
    </div>
  </ModalProvider>
);

// ...

CSS

The CSS code for the examples:

.modal-container {
  background: rgba(0, 0, 0, 0.8);
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 10000;
}

.modal {
  background: white;
  border: 2px solid black;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.modal__header {
  border-bottom: 1px solid black;
  padding: 4px 12px;
  background: #aaa;
}

.modal__content {
  padding: 12px;
}

The result can be seen here:

Edit kxo5r0nlm3

License and Copyright

MIT © Marc Reuter

Package Sidebar

Install

npm i @mr-web/react-modal

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

12.7 kB

Total Files

5

Last publish

Collaborators

  • jpec