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

alert-toast

v0.1.5

Published

Alert Toast is a simple toast library for React. I created this library for one of my projects a while ago, and since then, I have been reusing it in other projects. To make my life easier, and hopefully yours as well, I decided to create an NPM package f

Downloads

4

Readme

Alert Toast

Alert Toast is a simple toast library for React. I created this library for one of my projects a while ago, and since then, I have been reusing it in other projects. To make my life easier, and hopefully yours as well, I decided to create an NPM package for it.

firefox_rQOsYvPO4Q

Installation

To install Alert Toast, run the following command:

npm i alert-toast

https://www.npmjs.com/package/alert-toast

Usage

To use Alert Toast, import the Alert component and place it preferably in the main component of your application.

import { Alert } from 'alert-toast';

<Alert/>

You can pass props to the Alert component to override default configurations. These props include colors, font color, and position of the toast. Please note that you can choose the position of the toast when invoking it. The provided position is the default position when no position is specified.

import Alert from 'alert-toast';

<Alert
  successColor='#388e3c'
  dangerColor='#f57c00'
  errorColor='#d32f2f'
  defaultPosition='bottom-center'
  fontColor='#ddd'
/>

To display a toast, import the useAlert hook in the component where you want to call it. Then, simply call the displayAlert function that comes with the hook.

import { useAlert } from 'alert-toast';

const MyComponent = () => {
  const { displayAlert } = useAlert();

  useEffect(() => {
    displayAlert('Toast message :)', 'success');
  }, []);

  // ...
};

displayAlert()

The displayAlert() function takes two required arguments: the message and the type. There are also optional arguments available. Here's the order of the arguments:

  • text: string (The actual message to be displayed)
  • type: "success" | "error" | "danger" (This affects the color of the toast. There are default colors, but you can override them as mentioned above)
  • duration: number (The duration the toast will be shown, default is 2000 milliseconds)
  • position: "top-right" | "top-left" | "top-center" | "bottom-right" | "bottom-left" | "bottom-center" (The position you want the toast to appear on your page. This will override the default position)

Example usage:

displayAlert('Toast message', 'success', 5000, 'bottom-left');

displayCustomAlert()

The displayCustomAlert() function is also imported with the useAlert hook. It works exactly like displayAlert(), but the key difference is that you can directly pass a hexadecimal or RGB code instead of the type to create a toast with any color you want.

import { useAlert } from 'alert-toast';

const MyComponent = () => {
  const { displayCustomAlert } = useAlert();

  useEffect(() => {
    displayCustomAlert('Custom toast color', '#0288d1', '5000', 'top-center');
  }, []);

  // ...
};

How It Works

You may have noticed that there is no provider for this library. That's because I chose to use CustomEvents to manipulate the alert state inside the component. Therefore, you should avoid using more than one Alert component simultaneously.

NOTES AND ISSUES

Please note the following considerations and limitations:

  • If you call multiple toasts with different positions simultaneously, the position of the last toast called will override the positions of the others.
  • The library does not support passing JSX elements as messages; only strings are accepted.
  • Currently, there is no built-in "close" button for the toasts.
  • The library does not provide an option for the toast to persist indefinitely or until the user manually closes it.

I plan to improve this library whenaver i have time and energy to do it, but you can improve it and create pull requests if you want.