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

@mr-web/react-modal

v0.1.0

Published

Fully customizable Modals for React

Downloads

3

Readme

@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