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

toast-comp

v1.0.2

Published

A simple and customizable React notification component with support for multiple types, icons, animations, and accessibility features.

Downloads

3

Readme

React Notification Library

A simple and customizable React notification component with support for multiple types, icons, animations, and accessibility features.

Features

  • Notification Types: Success, Info, Warning, Error.
  • Icons: Includes icons for each notification type.
  • Animations: Fade, Pop, and Slide animations.
  • Accessibility: Supports aria-live and role attributes for better accessibility.

Installation

You can install the package via npm:

npm install toast-camp
yarn add toast-camp

Usage

import React from "react";
import useNotification from "toast-camp";

const App = () => {
  const { showNotification } = useNotification();

  const handleShowNotification = () => {
    showNotification({
      type: "success",
      message: "This is a success notification!",
      animation: "slide",
      onClose: () => console.log("Notification closed"),
    });
  };

  return (
    <div>
      <button onClick={handleShowNotification}>
        Show Notification
      </button>
    </div>
  );
};

export default App;

Notification Component

import React, { useEffect, useRef } from "react";
import {
  AiOutlineCheckCircle,
  AiOutlineInfoCircle,
  AiOutlineWarning,
  AiOutlineCloseCircle,
  AiOutlineClose,
} from "react-icons/ai";
import "./Notification.css";
import { NotificationProps } from "./types";

const iconStyles: React.CSSProperties = { marginRight: "10px" };
const icons: Record<string, JSX.Element> = {
  success: <AiOutlineCheckCircle style={iconStyles} />,
  info: <AiOutlineInfoCircle style={iconStyles} />,
  warning: <AiOutlineWarning style={iconStyles} />,
  error: <AiOutlineCloseCircle style={iconStyles} />,
};

const animations: Record<string, string> = {
  fade: "fadeIn",
  pop: "popup",
  slide: "slideIn",
};

const Notification: React.FC<NotificationProps> = ({
  type = "info",
  message,
  onClose,
  animation = "slide",
}) => {
  // A11y
  const notificationRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    if (notificationRef.current) {
      notificationRef.current.focus();
    }
  }, []);

  const ariaRole = type === "error" || type === "warning" ? "alert" : "status";
  const ariaLive =
    type === "error" || type === "warning" ? "assertive" : "polite";
  // A11y

  return (
    <div
      className={`notification ${type} ${animations[animation]}`}
      // A11y
      role={ariaRole}
      aria-live={ariaLive}
      tabIndex={-1}
      ref={notificationRef}
      // A11y
    >
      {icons[type]} {message}
      <button className="closeBtn" onClick={() => onClose()}>
        <AiOutlineClose color="white" />
      </button>
    </div>
  );
};

export default Notification;

Types

export interface NotificationProps {
  type?: "success" | "info" | "warning" | "error";
  message: string;
  onClose: () => void;
  animation?: "fade" | "pop" | "slide";
}

Customization

  • To customize the notification styles, you can override the default CSS classes in Notification.css.
.notification {
  padding: 16px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: white;
  font-size: 16px;
}

.notification.success {
  background-color: #4caf50;
}

.notification.info {
  background-color: #2196f3;
}

.notification.warning {
  background-color: #ff9800;
}

.notification.error {
  background-color: #f44336;
}

.closeBtn {
  background: none;
  border: none;
  cursor: pointer;
}

.fadeIn {
  animation: fadeIn 0.3s ease-in-out;
}

.popup {
  animation: popup 0.3s ease-in-out;
}

.slideIn {
  animation: slideIn 0.3s ease-in-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes popup {
  from {
    transform: scale(0.8);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes slideIn {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(0);
  }
}