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

react-cmac-toaster

v0.0.16

Published

React Cmac Toaster is a lightweight and customizable toast notification library designed for React applications

Downloads

1,110

Readme

React Cmac Toaster

npm version License: MIT

A simple and customizable React toast notification library for easy toast management in your applications. This package provides context-based toasts without requiring external dependencies like Redux.

Features

  • Lightweight and easy to use
  • Customizable toasts (width, color, duration, etc.)
  • Supports both text messages and JSX elements
  • No Redux required, uses React Context and Hooks
  • Automatic timeout for toasts

Installation

You can install this package using npm or yarn:

npm install react-cmac-toaster

or

yarn add react-cmac-toaster

Usage

1. Setup the ToastProvider

First, wrap your application with the ToastProvider to make the toast system available across the app:

import React from 'react';
import { ToastProvider } from 'react-cmac-toaster';

function App() {
  return (
    <ToastProvider>
      {/* Your app components go here */}
    </ToastProvider>
  );
}

export default App;

2. Triggering a Toast

You can trigger a toast anywhere in your app using the useToast hook.

import React from 'react';
import { useToast } from 'react-cmac-toaster';

const ExampleComponent = () => {
  const toast = useToast();

  const showToast = () => {
    // Displaying a simple text message
    toast.open("This is a simple toast message!", {
      backgroundColor: "lightblue",
      color: "black",
      timeoutSeconds: 5000,
    });

    // Displaying a custom JSX element
    const customToast = () => (
      <div>
        <strong>Custom Toast:</strong> With a button!
        <button>Click me</button>
      </div>
    );
    toast.open(customToast, {
      backgroundColor: "lightgreen",
    });
  };

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

export default ExampleComponent;

3. Customizing Toasts

The open function accepts a message (which can be a string or JSX element) and an options object to customize the toast.

Available Options

| Option | Type | Default | Description | |-----------------|-----------|--------------------------------|------------------------------------------------------------| | toastWidth | string | "330px" | Sets the width of the toast. | | toastMsgWidth | string | "298px" | Sets the width of the message area inside the toast. | | backgroundColor| string | "#ffffff"| Sets the background color of the toast. | | color | string | "#000000" | Sets the text color of the toast. | | timeoutSeconds| number | 3000 | Sets how long the toast should be visible (in milliseconds).|

4. Closing a Toast Programmatically

You can close a toast by using the close function, which takes the toast id as an argument:

const toast = useToast();
toast.close(1); // Close the toast with id 1

5. Toast Lifecycle

Toasts automatically disappear after the timeoutSeconds duration. However, they can also be closed manually through the close button in the UI.

Types

The following TypeScript types are available to help with integration:

Toast

export interface Toast {
  id: number;
  message: string | (() => React.ReactNode);
  toastWidth?: string;
  toastMsgWidth?: string;
  backgroundColor?: string;
  timeoutSeconds?: number;
  color?: string;
}

ToastOptions

export interface ToastOptions {
  toastWidth?: string;
  toastMsgWidth?: string;
  backgroundColor?: string;
  color?: string;
  timeoutSeconds?: number;
}

Contributing

Feel free to submit issues or pull requests for improvements and bug fixes. Any contributions are welcome.

License

This project is licensed under the MIT License - see the LICENSE file for details.