React Notification is a customizable notification component for React applications. It provides an easy way to add dynamic notifications with various options for customization and a rich features set, allowing you to enhance user experience in your projects.
You can access the live demo of this React Notification on Netlify:
Netlify: React Notification Demo
- Add notifications with various statuses such as success, failure, and warning.
- Customize notification appearance including text, status, background color, duration, and more.
- Support for custom icons to accompany notifications.
- Option to have fixed notifications that do not automatically clear.
- Close button rotation for enhanced interactivity.
- Easily integrate into existing React applications.
- Customize notification position with options such as:
- top center
- bottom center
- top left
- top right
- bottom left
- bottom right
To use the React Notification component in your React application, follow these steps:
-
Download the
Notification
folder from the repository. The folder structure should be as follows:├── node_modules ├── public ├── src │ ├── components │ │ ├── Navbar │ │ ├── Notification │ │ │ ├── Notification.css │ │ │ ├── Notification.js │ │ │ ├── NotificationContext.js │ │ │ └── notificationExample.js │ │ └── NotificationPage │ ├── App.js │ ├── index.js │ └── app.css ├── .gitignore ├── LICENSE ├── README.md ├── package.json └── package-lock.json
-
In your project, import
NotificationsProvider
fromNotificationContext.js
to manage notifications context:import { NotificationsProvider } from './components/Notification/NotificationContext';
-
Import the Notification component from Notification.js to use it in your application:
import Notification from './components/Notification/Notification';
-
Wrap your application in
NotificationsProvider
to provide notification context. Also, include theNotification
component anywhere inside theApp
component to display notifications:import React from 'react'; import { NotificationsProvider } from './components/Notification/NotificationContext'; import Notification from './components/Notification/Notification'; const App = () => { return ( <NotificationsProvider> <div className="App"> {/* Include the Notification component */} <Notification position="topCenter" /> {/* Your other components */} </div> </NotificationsProvider> ); }; export default App;
-
Place the
Notification
component wherever you want notifications to appear in your application. You can specify the position using theposition
prop. By default, notifications appear at the top center:<Notification position="topCenter" />
Available position options:
bottomCenter topLeft topRight bottomLeft bottomRight
-
Please replace
'./components/Notification/NotificationContext'
and'./components/Notification/Notification'
with the actual relative paths to theNotificationContext.js
andNotification.js
files in your project. Additionally, ensure that the folder structure and file names match those mentioned in step 1.
-
uuid: This library is used to generate unique identifiers for notifications.
You can install it via npm:
npm install uuid
To send notifications using the React Notification component, follow these steps:
-
Import the
useNotifications
custom hook fromNotificationContext.js
to access the notification functionality:import { useNotifications } from '../Notification/NotificationContext';
-
Inside the component, initialize the addNotification function using the useNotifications hook:
const { addNotification } = useNotifications();
-
Use the addNotification function to send notifications with desired configurations. The format for adding notifications is as follows:
addNotification( { title: "Title", // Default is null description: "Description", // Default is null text: "Text", // // Default is null status: "success" || "failed" || "warning", // Default is null background: "purple" || "#800080", // Can use any color name or hex code color: "white" || "#FFFFFF", // Can use any color name or hex code icon: "✅" || <FaCheckCircle />, // Can be a string representing a unicode character or a React component duration : '5s', // Default is 5s iconColor: "white" || "#FFFFFF", // Can use any color name or hex code textsColor: "white" || "#FFFFFF", // Can use any color name or hex code titleColor: "white" || "#FFFFFF", // Can use any color name or hex code textColor: "white" || "#FFFFFF", // Can use any color name or hex code descriptionColor: "white" || "#FFFFFF", // Can use any color name or hex code fixed: true, // If true, the notification remains fixed and does not automatically clear progressColor: "gray" || "#808080", // Can use any color name or hex code closeButtonRotate: true, // If true, the close button rotates on hover } );
-
Use the
addNotification
function from useNotifications to add notifications with desired configurations. Example usage:import React from 'react'; import { useNotifications } from './components/Notification/NotificationContext'; const ExampleComponent = () => { const { addNotification } = useNotifications(); const handleClick = () => { addNotification({ title: "Title", description: "Description", text: "Text", status: "success", background: "purple", color: "white", icon: "✅", duration: '5s', iconColor: "white", titleColor: "white", textColor: "white", descriptionColor: "white", fixed: true, progressColor: "gray", closeButtonRotate: true, }); }; return ( <button onClick={handleClick}>Add Notification</button> ); }; export default ExampleComponent;
Please make sure to replace '../Notification/NotificationContext'
with the actual relative path to the NotificationContext.js
file in your project. Additionally, ensure that the useNotifications
hook and addNotification
function are correctly imported and used within your components.
Contributions are welcome! If you'd like to contribute to this project, feel free to fork this repository, make your changes, and submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.