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

simple-react-notifications

v1.2.16

Published

Tiny react.js notification library (1kb gzip).

Downloads

384

Readme

Simple-React-Notifications

Tiny library (only 1kb gzip) that allows you to add notifications to your app. We don't want to blow up our bundle size because of notifications, right?

Demo

https://alexpermyakov.github.io/simple-react-notifications/

Despite the small size, it supports:

Installation

$ npm install simple-react-notifications
$ yarn add simple-react-notifications

Usage

Rendering success and error default notifications

Notifier has a few built-in components for displaying an error or a successfull operation:

import React from "react";
import notifier from "simple-react-notifications";
import "simple-react-notifications/dist/index.css";

const App = () => (
  <div>
    <button
      onClick={() => {
        notifier.success("Your items have been updated");
        // notifier.error("Something went wrong, try again.");
      }}
    >
      Lets render a default component
    </button>
  </div>
);

Rendering user defined component

The real power comes with rendering our own component. In this case it's not even a notification, just a view with real data:

const RouteInfo = ({ header, onClosePanel }) => (
  <div
    className="route-info"
    onClick={onClosePanel}
    style={{
      height: "200px",
      background: "#54cea7",
      color: "white",
      padding: "8px 16px",
      position: "relative",
      boxShadow: "rgba(0, 0, 0, 1) 0px 0px 14px 2px"
    }}
  >
    <h3 className="subtitle">{header}</h3>
    <p>Bicycle 2.4 km, 8 min.</p>
    <p>Use caution - may involve errors or sections not suited for bicycling</p>
    <button
      className="button"
      style={{ position: "absolute", bottom: "16px", right: "16px" }}
    >
      Use this route
    </button>
  </div>
);

It completely up to us the way we add styles. We can use styled-components or whatever we like. The notify() method will just render it:

const App = () => (
  <div>
    <button
      onClick={() =>
        notifier({
          render: ({ id, onClose }) => (
            <RouteInfo
              key={id}
              onClosePanel={onClose}
              header={"The shortest way to ride home."}
            />
          )
        })
      }
    >
      Notify with just a text message!
    </button>
  </div>
);

As you can see here, render() receives onClose callback, which we have to pass inside our component in order to close the notification when user click on the button.

Positioning

By default, all items will be positioned in the top right corner. The following values are allowed: top-right, top-center, top-left, bottom-right, bottom-center, bottom-left.

const App = () => (
  <div>
    <button
      onClick={() => {
        // notifier({
        //   position: "top-left"
        // });

        notifier({
          single: true, // display only the latest item
          position: "top-center",
          render: ({ id, onClose }) => (
            <RouteInfo
              key={id}
              onClosePanel={onClose}
              header={"The shortest way to ride home."}
            />
          )
        });
      }}
    >
      Show two of them in different places
    </button>
  </div>
);

Configuring all in one place

Instead of specifing all params again and again for each item, we can put it in one place:

notifier.configure({
  autoClose: 2000,
  position: "top-center",
  delay: 500,
  single: false,
  width: "480px"
});

const App = () => (
  <div>
    <button
      onClick={() => {
        notifier.success("Your items have been updated");
      }}
    >
      Display an item with 500 ms delay. Now it is done in configure()
    </button>
  </div>
);

Params in notifier function will override their default values in configure().

Animation

First, define the css-animation somewhere in your .css file:

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes fadeOut {
  from {
    opacity: 1;
  }
  to {
    opacity: 0;
  }
}

Second, specify it during the notifier() call or in configure():

notifier.configure({
  position: "top-center",
  animation: {
    in: "fadeIn", // try to comment it out
    out: "fadeOut",
    duration: 600 // overriding the default(300ms) value
  }
});

const App = () => (
  <div>
    <button
      onClick={() => {
        notifier.success("Your items have been updated");
      }}
    >
      Show two of them in different places
    </button>
  </div>
);

You can specify only in or out params as well.

Remove notification items programmatically

import React from "react";
import notifier from "simple-react-notifications";

notifier.configure({
  render: ({ id, onClose }) => (
    <RouteInfo
      key={id}
      onClosePanel={onClose}
      header={"The shortest way to ride home."}
    />
  )
});

class App extends React.Component {
  id = null;

  render() {
    return (
      <div>
        <button onClick={() => (this.id = notifier())}>Notify</button>
        <button onClick={() => notifier.dismiss(this.id)}>Dismiss</button>
        <button onClick={() => notifier.dismiss()}>Dismiss all</button>
      </div>
    );
  }
}