A ultra-lightweight, zero-dependency Promise-based programmatic alert and confirmation popup utility for React. Built exclusively with core React Hooks.
Everything you need for modern alert management
No bundlers, external utilities, or styling preprocessors. Pure React Hooks – that's all you need.
Handle confirmations natively using async/await. Write cleaner, more intuitive code.
Pass async handlers to display reactive loader spinners and prevent double-submits automatically.
Customize colors, fonts, margins, and alignments via declarative element-targeted config objects.
Built on useState, useRef, and useContext. No class components, no complexity.
Ultra-minimal footprint with clean layout out of the box. No bloat.
Install via npm:
npm install alertbox-for-react
Requirements: React version ^16.8.0, ^17.0.0, ^18.0.0, or ^19.0.0 as a peer dependency.
Wrap your application tree inside the AlertProvider:
import React from 'react';
import { AlertProvider } from 'alertbox-for-react';
import Dashboard from './Dashboard';
function App() {
return (
<AlertProvider>
<Dashboard />
</AlertProvider>
);
}
export default App;
Ideal for standard notifications or confirmation modals requiring a simple close or action choice.
import { useAlert } from 'alertbox-for-react';
export default function SimpleDemo() {
const alert = useAlert();
const handleOpenAlert = () => {
alert.show({
title: "Discard Draft?",
message: "Are you sure...",
confirmText: "Discard",
cancelText: "Keep Editing",
showCancel: true
});
};
return (
<button onClick={handleOpenAlert}>
Delete Draft
</button>
);
}
Output: A simple alert with confirmation and cancel buttons.
When you map an async function block to onConfirm, the system intercepts the lifecycle, locks down buttons, and shows a spinner.
const handleDeleteDB = async () => {
const confirmed = await alert.show({
title: "Purge Cluster",
message: "This will disconnect...",
confirmText: "Purge Cluster",
onConfirm: async () => {
await new Promise(
(resolve) => setTimeout(
resolve, 3000
)
);
console.log("Purged!");
}
});
if (confirmed) {
alert.show({
title: "Success!"
});
}
};
Output: Loading spinner appears during async operation. Auto-dismiss on completion.
The package uses zero external stylesheets. Style adjustments are handled via inline mappings and custom style objects.
const handleDarkTheme = () => {
alert.show({
title: "Threat Blocked",
message: "Packet mitigated.",
style: {
overlay: {
backgroundColor: 'rgba(255, 255, 255, 0.50)'
},
box: {
backgroundColor: '#1e293b',
border: '1px solid #334155',
},
title: {
color: '#ef4444',
fontWeight: 'bold'
},
message: {
color: '#334155'
},
confirmBtn: {
backgroundColor: '#ef4444',
color: '#ffffff'
}
}
});
};
Output: Dark-themed alert with custom colors and styling.
| Parameter | Type | Default | Description |
|---|---|---|---|
| title | String | 'Alert' | Prominent header text at the top |
| message | String | '' | Descriptive message or body text |
| confirmText | String | 'Confirm' | Confirmation button label |
| cancelText | String | 'Cancel' | Cancel button label |
| showCancel | Boolean | true | Show/hide cancel button |
| onConfirm | Function | undefined | Callback executed on confirm (sync/async) |
| style | Object | {} | Custom inline CSS declarations |
Pass CSS properties within these dictionary target keys:
Alert message goes here
Install react-alert-box and streamline your alert management in seconds.