it's a reusable custom alert component!
🔖 We often need to implement modals or notification alerts to users... Why not do it simply and with a little fun?
- success,
- info,
- warning
- & error,
- you can put a personal image or keep the default emojis for each alert.
- you can choose if you want overlay effect or not
- you can change the modal alert dimensions (little, medium), if you don't write any props the default dimension is normal
- you can change default emoji with custom picture
- you can customize the modal background color for each type of alert
- you can change the title text & color
- you can change the message text color aswell
- you can make appear or not the button (if the button is appear, you can customize the button text, color text button & background color button. If the button do not appear, you can change the time of automatic closing)
Desktop view
Tablets view
mobile view
You can test My Awesome Custom Alert demo here,
success, // BOOLEAN - for appear or not the success window
successMessage, // STRING - message success
error, //BOOLEAN - for appear or not the error window
errorMessage, // STRING - message error
warning, // BOOLEAN - for appear or not the warning window
warningMessage, // STRING - message warning
info, // BOOLEAN - for appear or not the info window
infoMessage, // STRING - message info
// GLOBAL MODAL STYLES PROPS
overlay, // BOOLEAN - if you want overlay effect or not
little, // predefined values - the default size of the alert is divided by 2
medium, // predefined values - the default size of the alert is divided by 1/3
autoClose, // NUMBER - time in ms for auto closes alert when you don't have button
customStyleColor, // HEX, rgba, hsl, gradient, etc... - Background color value for modal
titleText, // STRING - title for each alert
colorTitle, // HEX, rgba, hsl, etc... - color value for title text
messageColor, // HEX, rgba, hsl, gradient, etc... - color value for message text
withButton, // BOOLEAN - value for appear or desappear the button window
buttonText, // STRING - button content
colorTextButton, // HEX, rgba, hsl, etc... - color value for button text
buttonBackground, // HEX, rgba, hsl, gradient, etc... - Background color value for button
alertImg, // IMAGE (svg, png, jpg...) - for replace all default emoji by your own pictures.
✔️ Important! When you do not display a button in the alert window, the modal closes automatically after 4 seconds.
This component does not use any external library, only React.
First of all, you have to copy the customAlert folder (which is in the components folder)... Then paste this folder and its content into the components folder of your application!
OK, the interesting part begins!
✔️ All of this examples are the most simple form, of course you can use this component at your fetch/axios responses or more complexe usecases.
Let's start with the Success option:
import { useState } from "react";
import CustomAlertComponent from "./components/customAlert/CustomAlertComponent";
import Me from "./assets/perso.png";
const ExampleComponent = () => {
const [displaySuccess, setDisplaySuccess] = useState(false);
// This function display the success alert
const handleClickSuccess = () => {
setDisplaySuccess(true);
// Your code here
};
// Component with Custom alert success
const SuccessAlert = () => (
<CustomAlertComponent
success
successMessage="yay, everything is working."
// If you want normal size alert modal remove this line below.
medium
// If you dont want custom picture, remove this line below. the default emoji will be chosed.
alertImg={Me}
// If you dont want custom background color, just remove this line below, the default color will be chosed.
customStyleColor="#ef8f9e" // Background value for the modal (ex: HEX, rgba, hsl, gradient, etc...)
// if you don't want overlay effect remove this line below
overlay
// If you dont want button, just remove this two lines below.
withButton
buttonText="Great, it's cool!"
/>
);
return (
<div>
{displaySuccess && (
<div className="slide-in-top">
<SuccessAlert />
</div>
)}
<div className="button-container">
<button className="button-boxApp" onClick={handleClickSuccess}>
Success Alert
</button>
</div>
</div>
)
};
export default ExampleComponent;
✔️ The Result
Next, the Info option:
import { useState } from "react";
import CustomAlertComponent from "./components/customAlert/CustomAlertComponent";
const ExampleComponent = () => {
const [displayInfo, setDisplayInfo] = useState(false);
// This function display the Info alert
const handleClickInfo = () => {
setDisplayInfo(true);
};
// Component with Custom alert info
const InfoAlert = () => (
<CustomAlertComponent
info
infoMessage="info! be read carefully."
little
// If you dont want custom background color, just remove this line below, the default gradient color will be choosed.
customStyleColor="rgba(255, 255,255, 0.7)" // Background value for the modal (ex: HEX, rgba, hsl, gradient, etc...)
// if you don't want overlay effect remove this line below
overlay
// If you dont want button, remove this line below.
withButton
// If you dont write buttonText props, the default text is choosed.
/>
);
return (
<div>
{displayInfo && (
<div className="slide-in-top">
<InfoAlert />
</div>
)}
<div className="button-container">
<button className="button-boxApp" onClick={handleClickInfo}>
Info Alert
</button>
</div>
</div>
)
};
export default ExampleComponent;
✔️ The Result
Another Option, the warning alert:
import { useState } from "react";
import CustomAlertComponent from "./components/customAlert/CustomAlertComponent";
const ExampleComponent = () => {
const [displayWarning, setDisplayWarning] = useState(false);
// This function display the warning alert
const handleClickWarning = () => {
setDisplayWarning(true);
};
// Component with Custom alert warning
const WarningAlert = () => (
<CustomAlertComponent
warning
warningMessage="Be careful what you ask for, you might end up with this!"
// If you dont write messageColor props, the default color is chosed.
messageColor="#fff"
// If you dont want custom background color, just remove this line below, the default color will be chosed.
customStyleColor="hsl(351, 75%, 75%)" // Background value for the modal (ex: HEX, rgba, hsl, gradient, etc...)
// if you don't want overlay effect remove this line below
overlay
// If you dont want custom timeout, remove this line below (the default value is 4000ms).
autoClose={5000}
/>
);
return (
<div>
{displayWarning && (
<div className="bounce-in-top">
<WarningAlert />
</div>
)}
<div className="button-container">
<button className="button-boxApp" onClick={handleClickWarning}>
Warning Alert
</button>
</div>
</div>
)
};
export default ExampleComponent;
✔️ The Result
And Then, finally the Error alert:
import { useState } from "react";
import CustomAlertComponent from "./components/customAlert/CustomAlertComponent";
const ExampleComponent = () => {
const [displayError, setDisplayError] = useState(false);
// This function display the Error alert
const handleClickError = () => {
setDisplayError(true);
};
// Component with Custom alert Error
const ErrorAlert = () => (
<CustomAlertComponent
error
// If you dont write title props, the default text is chosed.
title="OUPS, Not working..."
// If you dont write colorTitle props, the default color is chosed.
colorTitle="#000"
errorMessage="oh no, something went wrong."
// If you dont want custom background color, just remove this line below, the default color will be chosed.
customStyleColor="red" // Background value for the modal (ex: HEX, rgba, hsl, gradient, etc...)
// if you don't want overlay effect remove this line below
overlay
// If you dont want button, just remove this two lines below.
withButton
buttonText="No worry, try again!"
/>
);
return (
<div>
{displayError && (
<div className="bounce-in-top">
<ErrorAlert />
</div>
)}
<div className="button-container">
<button className="button-boxApp" onClick={handleClickError}>
Error Alert
</button>
</div>
</div>
)
};
export default ExampleComponent;
✔️ The Result
you can change more styles if you want...
For that you must go to the
customeAlertStyle.css
, all css styles for all usercase are here and commented!
The procedure and code implementation is similar to the other component!
Copy/paste the offlineMessage folder into your application...
You have two props:
type (string with five choices - danger - warning - success - info - primary)
content (string - content message to display)
... import OfflineMessage from "./components/bonus/offlineMessage/OfflineMessage"; ... const demoOfflineMessage = "You are offline or there is a connection problem!"; const demoOnlineMessage = "You are now back online!"; const demoInfoAndPrimaryMessage = "You are back online, but you should check your connection!"; <div className="bonus-section"> <h2>Bonus - another simple reusable alert component</h2> <OfflineMessage type="danger" content={`${demoOfflineMessage}`} /> <OfflineMessage type="warning" content={`${demoOfflineMessage}`} /> <OfflineMessage type="success" content={`${demoOnlineMessage}`} /> <OfflineMessage type="info" content={`${demoInfoAndPrimaryMessage}`} /> <OfflineMessage type="primary" content={`${demoInfoAndPrimaryMessage}`} /> </div> ...
- Thought, designed and developed with 💜 by Rodolphe Augusto
Enjoy the World 😏
I put almost everything open-source I can, and try to accommodate anyone who needs help using these projects. Obviously, this takes time. You can use this service for free.
However, if you are using this project and are happy with it or just want to encourage me to keep creating: -
- Put a star and share the project 🚀
Thank you! ❤️
MIT
https://github.com/rodolphe37/halloween2021-bat-tuto-youtube-video
https://github.com/rodolphe37/cra-template-github-my-profile
https://github.com/rodolphe37/react-native_geolocation-tracker
https://github.com/rodolphe37/cra-pwa-react-ultimate-messenger