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

toastify-test

v1.0.1

Published

A lightweight, customizable React library for toast notifications.

Downloads

117

Readme

Toastify

Toastify is a lightweight, customizable React library for showing toast notifications. It supports multiple themes, positions, notification types, and async operations with promise-based toasts.


Features

  • Customizable Notifications: Choose from various themes (dark, light), positions, and types.
  • Promise Support: Handle async operations with success and error notifications.
  • Close Button Option: Allow users to dismiss toasts manually.
  • Fully Typed: TypeScript support for better development experience.
  • Lightweight: Minimal impact on your application's size and performance, ensuring that your app remains fast and responsive.

Installation

# with npm
npm install toastify
# or with yarn
yarn add toastify
# or with bun
bun add toastify

Usage

Setting Up the Toast Provider

First, import and render the ToastContainer at the root level of your application:

import React from 'react';
import { ToastContainer } from 'toastify-react';

const App = () => (
  <>
    <ToastContainer />
    {/* Rest of your app components */}
  </>
);

export default App;

This ensures that your toasts will be displayed in the correct position and with the proper styling.


Displaying a Toast Notification

Once you've set up the ToastContainer, you can trigger toast notifications using the addToast function. Here’s an example of displaying a success toast:

Copy code
import React from 'react';
import { addToast } from 'toastify-react';

const App = () => {
  const showToast = () => {
    addToast({
      message: 'This is a success toast!',
      type: 'success', // 'success', 'error', 'info', 'warning'
      position: 'top-right', // 'top-left', 'top-right', 'bottom-left', 'bottom-right', 'top-center'
      theme: 'dark', // 'dark', 'light'
      duration: 3000, // Display duration in milliseconds
    });
  };

  return <button onClick={showToast}>Show Toast</button>;
};

export default App;

Toast with Async Operations

You can use addToast.promise to handle async operations, and show a toast while the operation is in progress. Once the operation is complete, a success or error toast will be shown.

Here’s an example:

Copy code
import React from 'react';
import { addToast } from 'toastify-react';

const App = () => {
  const saveSettings = () =>
    new Promise<void>((resolve, reject) => {
      setTimeout(() => {
        Math.random() > 0.5 ? resolve() : reject();
      }, 3000);
    });

  const handleAsyncToast = () => {
    addToast.promise({
      asyncOperation: saveSettings,
      message: 'Saving settings...',
      successMessage: 'Settings saved successfully!',
      errorMessage: 'Failed to save settings.',
      position: 'top-center',
      theme: 'light',
      duration: 3000,
    });
  };

  return <button onClick={handleAsyncToast}>Save Settings</button>;
};

export default App;

API

addToast(options)

| Property | Type | Default | Description | |-------------------|--------------|-------------------|------------------------------------------------------------| | message | string | '' | The main message of the toast. | | type | ToastType | 'info' | Type of toast: success, error, info, or warning. | | position | ToastPosition | 'bottom-right' | Position on screen: top-left, top-right, bottom-left, bottom-right, or top-center. | | theme | ToastTheme | 'dark' | Theme of the toast: dark or light. | | duration | number | 3000 | How long the toast should stay visible (in milliseconds). | | showCloseButton | boolean | false | Whether to show a close button for dismissing the toast. |

addToast.promise(options)

| Property | Type | Description | |-------------------|---------------------|------------------------------------------------------------| | asyncOperation | () => Promise<any>| The async operation to be tracked. | | message | string | The message shown during the async operation. | | successMessage | string | The message shown if the operation succeeds. | | errorMessage | string | The message shown if the operation fails. | | Other Properties | Same as addToast | Inherits all other options from addToast. |