GithubHelp home page GithubHelp logo

ricardocanelas / usetoast-example Goto Github PK

View Code? Open in Web Editor NEW
2.0 0.0 0.0 368 KB

This is a simple example of how you can create your own toast using only hook/contextApi.

HTML 16.18% JavaScript 50.40% CSS 33.42%

usetoast-example's Introduction

This project was bootstrapped with Create React App.

This is a simple example of how you can create your own toast using only hook/contextApi.

See the examples here - https://ricardocanelas.github.io/useToast-example

UseToast

props type params
toasts array -
getToasts function direction
addToast function props, direction, time
removeToast function id
clearToasts function -

Directions:

  • top
  • top-left
  • top-right
  • bottom
  • bottom-left
  • bottom-right.

Installing

In index.js add the providers

import React from "react";
import ReactDOM from "react-dom";
import { ToastProvider } from "./useToast";

import "./toaster.css";

ReactDOM.render(
  <ToastProvider>
    <App />
  </ToastProvider>,
  document.getElementById("root")
);

Using:

import React from "react";
import useToast from "./useToast";

const App = () => {
  const { addToast } = useToast();

  const handleClick = () => {
    addToast(
      { title: "This is a simple message", variant: "success" },
      "bottom",
      7000
    );
  };

  return (
    <div>
      <button onClick={handleClick}>Add Toast</button>
    </div>
  );
};

Customizing your toasts

props type
DefaultToast Component
maximum number
portal DOM Element
import React from "react";
import ReactDOM from "react-dom";
import { ToastProvider } from "./useToast";

const MyAlert = ({ props, remove, id }) => {
  return (
    <div>
      {id} - {props.title}
      <button onClick={handleClick}>Add Toast</button>
    </div>
  );
};

ReactDOM.render(
  <ToastProvider DefaultToast={MyAlert} maximum={5} portal={document.body}>
    <App />
  </ToastProvider>,
  document.getElementById("root")
);

License

MIT © Ricardo Canelas

usetoast-example's People

Contributors

ricardocanelas avatar

Stargazers

P0karmanam avatar Roman 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.