0.1.30 • Published 5 months ago

react-cmac-toaster v0.1.30

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

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.

Description Description

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: "black",
      color: "white",
      timeoutSeconds: 5000,
      toastStatus: "success",
    });

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

  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

OptionTypeDefaultDescription
toastWidthstring"100%"Sets the width of the toast.
toastMsgWidthstring"100%"Sets the width of the message area inside the toast.
backgroundColorstring"#ffffff"Sets the background color of the toast.
colorstring"#000000"Sets the text color of the toast.
showCloseBtnbooleantrueDetermines whether the close button should be displayed.
timeoutSecondsnumber3000Sets how long the toast should be visible (in milliseconds).
toastStatusstring"default"Accepts one of the following values to define the toast type: "success", "error", "warning", "notify", "default".

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.

0.1.10

5 months ago

0.1.11

5 months ago

0.1.12

5 months ago

0.1.13

5 months ago

0.1.14

5 months ago

0.1.15

5 months ago

0.1.0

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.8

5 months ago

0.1.7

5 months ago

0.1.9

5 months ago

0.1.4

5 months ago

0.1.3

5 months ago

0.1.6

5 months ago

0.1.5

5 months ago

0.1.30

5 months ago

0.1.27

5 months ago

0.1.28

5 months ago

0.1.29

5 months ago

0.1.20

5 months ago

0.1.21

5 months ago

0.1.22

5 months ago

0.1.23

5 months ago

0.1.24

5 months ago

0.1.25

5 months ago

0.1.26

5 months ago

0.1.16

5 months ago

0.1.17

5 months ago

0.1.18

5 months ago

0.1.19

5 months ago

0.0.16

9 months ago

0.0.10

9 months ago

0.0.11

9 months ago

0.0.12

9 months ago

0.0.13

9 months ago

0.0.14

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.15

9 months ago

0.0.9

9 months ago

0.0.8

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.1

9 months ago