Code Monkey home page Code Monkey logo

react-toastbox's Introduction

react-toastbox

A minimal react toast box inspired by reddit's toast message design and react-toastify.

Installation

NPM version

Using npm:

npm install react-toastbox

Codesandbox demo

Live Demo

Screenshots

Toaster

API

props

name type default description
timerExpires Number 6000(in 'ms') Expiration time till toast message should be visible
pauseOnHover Boolean true On hovering toast, it's timer should be paused
position String 'top-right' Position for toast to be displayed.
intent String 'primary' Theme for toast message

Possible values for API

positionClasses = [
  "top-right",
  "top-left",
  "top-center",
  "bottom-right",
  "bottom-left",
  "bottom-center"
];
paints = ["success", "warning", "danger"];

Quick API tour

In order to use toast box, you need to define component <ToastBox/> in root file which will listen for events(success,danger,etc), for example i.e. App.js.

Then when you want to show error or success message, just use {toast} method available from package. toast.success('Yayy')

ToastBox

import ToastBox from "react-toastbox";
.
.
.
<ToastBox timerExpires={5000} position="top-right" pauseOnHover={true} intent="success"/>

{ toast }

import { toast } from "react-toastbox";
.
.
.
handleClick = () => {
  /* Then when you want to show toast, call method and pass argument as text to display*/
  toast.success('Toast it up');
}

Usage

  • Use <ToastBox /> once in your app which listens to events such as success,error,etc... So in your root component(mainly App.js or main.js), register this component with suitable props acc. to your needs

Parent (App.js)

import React from "react";
import ToastBox from "react-toastbox";

/* Your root app logic here */
.
.
.
 <ToastBox
    timerExpires={5000}
    position="top-right"
    pauseOnHover={true}
  />
  • Then you can call any methods such as success,error,etc... from your children. The <ToastBox /> will listen to event and propagate changes.

Child(childToRender.js)

import React from "react";
import { toast } from "react-toastbox";

function Child() {
  return (
    <div className="App">
      <button
        onClick={() => {
          /* Then when you want to show toast, call method and pass argument as text to display*/
          toast.success("Toast it up");
        }}
      >
        Show me toast
      </button>
    </div>
  );
}
export default Child;

In Nutshell,

import React from "react";
import ReactDOM from "react-dom";
import ToastBox, { toast } from "react-toastbox";
import "./styles.css";

function App() {
  return (
    <div className="App">
      {/* First define toastbox component which will listen to events*/}
      <ToastBox
        timerExpires={5000}
        position="top-right"
        pauseOnHover={true}
        intent="success"
      />
      <button
        onClick={() => {
          /* Then when you want to show toast, call method and pass argument as text to display*/
          toast.success("Toast it up");
        }}
      >
        Show me toast
      </button>
    </div>
  );
}

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

react-toastbox's People

Contributors

meetzaveri avatar

Stargazers

Amrita Halder avatar

Watchers

James Cloos 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.