0.1.2 โ€ข Published 5 months ago

react-elegant-toasts v0.1.2

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

React Elegant Toasts

npm npm bundle size npm GitHub

A highly customizable, accessible, and elegant toast notification system for React applications. Built with TypeScript and modern React practices.

Features

  • ๐ŸŽจ Highly customizable appearance
  • ๐ŸŽญ Multiple animation types (slide, fade, zoom, bounce)
  • ๐Ÿ“ 6 different positions
  • โŒ› Progress bar with pause on hover
  • ๐ŸŒ“ Dark mode support
  • โ™ฟ Accessibility features (ARIA roles, keyboard navigation)
  • ๐Ÿ”„ RTL support
  • โšก Lightweight (~3.5KB gzipped)
  • ๐Ÿ“ฑ Responsive design
  • ๐ŸŽฏ TypeScript support
  • ๐Ÿงช Fully tested

Installation

npm install react-elegant-toasts
# or
yarn add react-elegant-toasts
# or
pnpm add react-elegant-toasts

Quick Start

import { ToastProvider, useToast } from 'react-elegant-toasts';
import 'react-elegant-toasts/dist/styles.css';

// Wrap your app with ToastProvider
function App() {
  return (
    <ToastProvider>
      <YourApp />
    </ToastProvider>
  );
}

// Use in your components
function YourComponent() {
  const toast = useToast();

  const showToast = () => {
    toast.addToast({
      type: 'success',
      title: 'Success!',
      message: 'Operation completed successfully',
    });
  };

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

Advanced Usage

Provider Configuration

<ToastProvider
  defaultPosition="top-right"
  defaultAnimation="slide"
  defaultDuration={5000}
  maxToasts={5}
  pauseOnPageIdle={true}
  pauseOnFocusLoss={true}
  containerClassName="my-toasts"
  containerStyle={{ zIndex: 9999 }}
>
  <YourApp />
</ToastProvider>

Toast Options

// Basic toast
toast.addToast({
  message: 'Simple notification'
});

// Full configuration
toast.addToast({
  type: 'success',
  title: 'Custom Toast',
  message: 'This is a fully customized toast',
  position: 'bottom-right',
  animation: 'bounce',
  duration: 8000,
  pauseOnHover: true,
  closeOnClick: true,
  progressBar: true,
  rtl: false,
  className: 'my-toast',
  style: { backgroundColor: '#4caf50' },
  icon: <CustomIcon />,
  role: 'alert'
});

// Update existing toast
const id = toast.addToast({ message: 'Loading...' });
// ... after some operation
toast.updateToast(id, { 
  type: 'success',
  message: 'Operation completed!' 
});

// Remove specific toast
toast.removeToast(id);

// Remove all toasts
toast.removeAll();

Toast Types

// Success toast
toast.addToast({
  type: 'success',
  title: 'Success',
  message: 'Operation completed successfully'
});

// Error toast
toast.addToast({
  type: 'error',
  title: 'Error',
  message: 'Something went wrong'
});

// Warning toast
toast.addToast({
  type: 'warning',
  title: 'Warning',
  message: 'Please check your input'
});

// Info toast
toast.addToast({
  type: 'info',
  title: 'Info',
  message: 'New updates available'
});

Animations

// Slide animation
toast.addToast({
  animation: 'slide',
  message: 'Sliding notification'
});

// Fade animation
toast.addToast({
  animation: 'fade',
  message: 'Fading notification'
});

// Zoom animation
toast.addToast({
  animation: 'zoom',
  message: 'Zooming notification'
});

// Bounce animation
toast.addToast({
  animation: 'bounce',
  message: 'Bouncing notification'
});

Custom Styling

/* Override default styles */
.toast {
  /* Custom toast container styles */
}

.toast.success {
  /* Custom success toast styles */
}

.toast-title {
  /* Custom title styles */
}

.toast-message {
  /* Custom message styles */
}

.toast-progress {
  /* Custom progress bar styles */
}

/* Dark mode overrides */
@media (prefers-color-scheme: dark) {
  .toast {
    /* Dark mode styles */
  }
}

TypeScript Support

import type { 
  ToastProps, 
  ToastPosition, 
  ToastType, 
  ToastAnimation 
} from 'react-elegant-toasts';

// Use in your code
const position: ToastPosition = 'top-right';
const type: ToastType = 'success';
const animation: ToastAnimation = 'slide';

Usage with Next.js

// app/providers.tsx
'use client';

import { ToastProvider } from 'react-elegant-toasts';
import 'react-elegant-toasts/dist/styles.css';

export function Providers({ children }: { children: React.ReactNode }) {
  return <ToastProvider>{children}</ToastProvider>;
}

// app/layout.tsx
import { Providers } from './providers';

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html>
      <body>
        <Providers>{children}</Providers>
      </body>
    </html>
  );
}

Usage with Remix

// app/root.tsx
import { ToastProvider } from 'react-elegant-toasts';
import toastStyles from 'react-elegant-toasts/dist/styles.css';

export const links: LinksFunction = () => [
  { rel: 'stylesheet', href: toastStyles }
];

export default function App() {
  return (
    <html>
      <body>
        <ToastProvider>
          <Outlet />
        </ToastProvider>
      </body>
    </html>
  );
}

API Reference

ToastProvider Props

PropTypeDefaultDescription
defaultPositionToastPosition'top-right'Default position for toasts
defaultAnimationToastAnimation'slide'Default animation type
defaultDurationnumber5000Default duration in milliseconds
maxToastsnumber5Maximum number of toasts shown at once
containerClassNamestringundefinedCustom class for the container
containerStyleCSSPropertiesundefinedCustom styles for the container
pauseOnPageIdlebooleantruePause toasts when page is idle
pauseOnFocusLossbooleantruePause toasts when window loses focus

Toast Props

PropTypeDefaultDescription
typeToastType'info'Type of toast
titlestringundefinedToast title
messagestringrequiredToast message
positionToastPosition'top-right'Toast position
animationToastAnimation'slide'Animation type
durationnumber5000Duration in milliseconds
pauseOnHoverbooleantruePause timer on hover
closeOnClickbooleantrueClose toast on click
progressBarbooleantrueShow progress bar
rtlbooleanfalseRight-to-left support
classNamestringundefinedCustom class name
styleCSSPropertiesundefinedCustom styles
iconReactNodeundefinedCustom icon
rolestring'alert'ARIA role

Browser Support

  • Chrome (latest)
  • Firefox (latest)
  • Safari (latest)
  • Edge (latest)
  • Opera (latest)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

MIT ยฉ Your Name

Acknowledgments

  • Inspired by the best practices of toast notifications
  • Built with accessibility in mind
  • Designed for modern React applications