0.2.5 โ€ข Published 4 months ago

react-cloudbox-alerts v0.2.5

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

React Cloudbox Alerts

A flexible and powerful alert system for React applications.

NPM npm npm bundle size GitHub Workflow Status

React Cloudbox Alerts provides a complete solution for displaying notifications, toasts, confirmations, and progress indicators in your React applications. With a focus on flexibility, performance, and developer experience, this library makes it easy to add beautiful alerts with minimal setup.

Features

  • ๐Ÿš€ Global Alert System - Show alerts from anywhere in your application
  • โœจ Multiple Alert Types - Success, error, warning, info, and custom alerts
  • ๐ŸŽญ Animations - Smooth entrance and exit animations
  • ๐ŸŒ— Dark Mode Support - Seamless integration with light/dark themes
  • ๐Ÿ“ฑ Responsive Design - Works on all screen sizes
  • ๐Ÿ”ง Highly Customizable - Tailor alerts to match your application's design
  • ๐Ÿงฉ Modular Architecture - Use only what you need
  • ๐Ÿ“ฆ Small Footprint - Minimal impact on your bundle size
  • ๐Ÿงช Well Tested - Comprehensive test coverage

Documentation

For full documentation and examples, visit our Storybook.

Installation

# npm
npm install react-cloudbox-alerts

# yarn
yarn add react-cloudbox-alerts

# pnpm
pnpm add react-cloudbox-alerts

Quick Start

1. Set up the AlertContainer

Add the AlertContainer component at the root level of your application:

import React from 'react';
import { AlertContainer } from 'react-cloudbox-alerts';

function App() {
  return (
    <div className="app">
      {/* Add AlertContainer at the root level */}
      <AlertContainer position="top-right" />
      
      {/* Your application content */}
      <main>
        <h1>My Application</h1>
        {/* ... */}
      </main>
    </div>
  );
}

export default App;

2. Show alerts using AlertService

Now you can trigger alerts from anywhere in your application:

import React from 'react';
import { AlertService } from 'react-cloudbox-alerts';

function Dashboard() {
  const handleSave = () => {
    // Save data
    AlertService.success('Data saved successfully!');
  };

  const handleError = () => {
    AlertService.error('An error occurred during the operation.');
  };

  return (
    <div>
      <h2>Dashboard</h2>
      <button onClick={handleSave}>Save Data</button>
      <button onClick={handleError}>Trigger Error</button>
    </div>
  );
}

export default Dashboard;

Advanced Usage

Theme Integration

The library includes a built-in theme provider that integrates with your application's theme:

import React from 'react';
import { AlertContainer, ThemeProvider } from 'react-cloudbox-alerts';

function App() {
  return (
    <ThemeProvider>
      <div className="app">
        <AlertContainer />
        {/* Your application content */}
      </div>
    </ThemeProvider>
  );
}

export default App;

Custom Alert Styling

Customize the appearance of alerts to match your application's design:

import React from 'react';
import { AlertContainer } from 'react-cloudbox-alerts';

function App() {
  return (
    <div className="app">
      <AlertContainer 
        position="bottom-center"
        containerWidth={400}
        spacing={15}
        iconSet="fa" // Use Font Awesome icons
      />
      {/* Your application content */}
    </div>
  );
}

export default App;

Confirmation Alerts

Show alerts that require user confirmation:

import { AlertService } from 'react-cloudbox-alerts';

async function deleteItem() {
  try {
    await AlertService.confirm('Are you sure you want to delete this item?', {
      confirmText: 'Delete',
      cancelText: 'Cancel',
      variant: 'danger'
    });
    
    // User confirmed, proceed with deletion
    await deleteItemFromDatabase();
    AlertService.success('Item deleted successfully!');
  } catch (error) {
    // User cancelled or an error occurred
    console.log('Operation cancelled');
  }
}

Progress Alerts

Show alerts with progress indicators:

import { AlertService } from 'react-cloudbox-alerts';

function uploadFile(file) {
  // Create a progress alert
  const progressAlert = AlertService.progress('Uploading file...', {
    variant: 'info',
    withIcon: true,
  });
  
  // Update progress as the operation proceeds
  const uploadTask = createUploadTask(file);
  
  uploadTask.on('progress', (snapshot) => {
    const progress = snapshot.bytesTransferred / snapshot.totalBytes;
    progressAlert.update(`Uploading: ${Math.round(progress * 100)}%`, progress);
  });
  
  uploadTask.on('success', () => {
    progressAlert.complete('File uploaded successfully!');
  });
  
  uploadTask.on('error', (error) => {
    progressAlert.error(`Upload failed: ${error.message}`);
  });
}

API Reference

Components

<AlertContainer>

The container component that manages and displays alerts.

PropTypeDefaultDescription
positionstring'top-right'Position of alerts on the screen. Options: 'top-right', 'top-left', 'top-center', 'bottom-right', 'bottom-left', 'bottom-center'
newestOnTopbooleantrueWhether to show newest alerts on top
limitnumber5Maximum number of alerts to show at once (0 = unlimited)
spacingnumber10Space between alerts in pixels
containerWidthnumber300Container width in pixels
iconSetstring'ri'Icon set to use (e.g., 'ri' for Remix Icons, 'fa' for Font Awesome)
darkModeboolean-Override dark mode from theme context
offsetnumber20Offset from edge of screen in pixels
zIndexnumber9999Z-index of the container

<Alert>

The individual alert component (usually used internally by AlertContainer).

PropTypeDefaultDescription
variantstring'primary'Alert style variant: 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'light'
withIconbooleanfalseWhether to display an icon
withBackgroundbooleanfalseUse colored background instead of colored text
dismissiblebooleanfalseWhether the alert can be dismissed
iconstring-Custom icon class (defaults based on variant)
autoHideDurationnumber0Auto-hide duration in milliseconds (0 = no auto-hide)
animationstring'fade'Animation type: 'fade', 'slide', 'bounce', 'zoom', 'slide-up', 'slide-down', 'slide-left', 'slide-right'
positionstring'top'Alert position for animations: 'top', 'bottom', 'left', 'right', 'center'
classNamestring-Additional CSS classes
onDismissfunction-Callback function when alert is dismissed
childrennode-Alert content

<ThemeProvider>

Provider component for theme context.

PropTypeDefaultDescription
defaultDarkModebooleanfalseInitial dark mode state
childrennode-Child components

Services

AlertService

Service for showing alerts programmatically.

MethodParametersReturnsDescription
show(message, options)alertIdShows a generic alert
success(message, options)alertIdShows a success alert
error(message, options)alertIdShows an error alert
warning(message, options)alertIdShows a warning alert
info(message, options)alertIdShows an info alert
remove(alertId)-Removes an alert by ID
clear--Removes all alerts
confirm(message, options)PromiseShows a confirmation alert
progress(message, options){update, complete, error}Shows a progress alert

Hooks

useTheme

Hook to access theme context.

import { useTheme } from 'react-cloudbox-alerts';

function MyComponent() {
  const { darkMode, toggleTheme } = useTheme();
  
  return (
    <button onClick={toggleTheme}>
      Switch to {darkMode ? 'Light' : 'Dark'} Mode
    </button>
  );
}

Browser Support

React Cloudbox Alerts supports all modern browsers:

  • Chrome (and Chromium-based browsers like Edge)
  • Firefox
  • Safari
  • Opera

Contributing

We welcome contributions from the community! Please check out our Contributing Guidelines for details on how to get started and our Code of Conduct for our community standards.

Versioning

This project follows Semantic Versioning. For the versions available, see the tags on this repository.

License

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

0.2.5

4 months ago

0.2.4

4 months ago

0.2.3

4 months ago

0.2.2

4 months ago

0.2.1

4 months ago

0.2.0

4 months ago