Lightweight React modal component built on Bootstrap 5, easy to use, supporting:
size,centered,backdrop,scrollable,fullscreen- Custom
modalClassName - ModalProvider for centralized state management
useModalanduseActiveModalhooks for flexible modal control
npm install @sentuh/react-bootstrap-modalYou can try a live demo with example code here: https://stackblitz.com/edit/react-bootstrap-modal
The ModalProvider manages all modal state in your app. Wrap it around your root component:
import { ModalProvider } from "@sentuh/react-bootstrap-modal";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import App from "./App.tsx";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<ModalProvider>
<App />
</ModalProvider>
</StrictMode>
);There are two main ways to open a modal:
- Passing the component class/function (recommended for reusability and automatic props handling)
- Passing a JSX element directly (for quick one-off modals)
import { useModal } from "@sentuh/react-bootstrap-modal";
import { ExampleModal } from "./ExampleModal";
function App() {
const modal = useModal();
const handleOpen = () => {
// Pass the component itself, modal will render it internally
const result = await modal.open(ExampleModal);
console.log(result);
};
return (
<button className="btn btn-primary" onClick={handleOpen}>
Open Modal
</button>
);
}ExampleModal Example:
import type { ModalProps } from "@sentuh/react-bootstrap-modal";
interface Props extends ModalProps {}
export const ExampleModal = ({ onClose, onDismiss }: Props) => {
return (
<>
<div className="modal-header">
<h4>Example Modal Header</h4>
</div>
<div className="modal-body">
<p>This is an example reusable modal component.</p>
</div>
<div className="modal-footer">
<button className="btn btn-secondary" onClick={onDismiss}>
Cancel
</button>
<button className="btn btn-primary" onClick={onClose}>
Save
</button>
</div>
</>
);
};Note: In this approach, useModal.open() will internally create and manage the modal instance.
You can pass dynamic data to your modal using the model argument:
const model = { id: 1, name: "Jane Doe" };
const result = await modal.open(ExampleModal, model);
console.log(result); // { name: 'John Doe'}Inside ExampleModal, access the passed data via the model prop:
import type { ModalProps } from "@sentuh/react-bootstrap-modal";
import { useEffect } from "react";
interface InputProps {
id: string;
name: string;
}
interface OutputProps {
name: string;
}
interface Props extends ModalProps<InputProps, OutputProps> {}
export const ExampleModal = ({ onClose, onDismiss, model }: Props) => {
useEffect(() => {
console.log(model); // input: { id: 1, name: 'Jane Doe'}
}, []);
const handleSave = () => {
// return output to parent
onClose({ name: "John Doe" });
};
return (
<>
<div className="modal-header">
<h4>Example Modal Header</h4>
</div>
<div className="modal-body">
<p>This is an example reusable modal component.</p>
</div>
<div className="modal-footer">
<button className="btn btn-secondary" onClick={onDismiss}>
Cancel
</button>
<button className="btn btn-primary" onClick={handleSave}>
Save
</button>
</div>
</>
);
};import { useModal } from "@sentuh/react-bootstrap-modal";
import { ExampleModal } from "./ExampleModal";
function App() {
const modal = useModal();
const handleChange = (result: { name: string }) => {
console.log(result); // output: { name: 'John Doe' }
}
const handleOpen = () => {
// Pass the JSX element directly
modal.open(<ExampleModal onChange={handleChange}/>);
};
return (
<button className="btn btn-primary" onClick={handleOpen}>
Open Modal
</button>
);
}Difference from Method 1: In this approach, you pass a JSX element instead of the component reference. This is convenient for quick, one-off modals with inline props, but it is less reusable.
useActiveModal allows the modal itself to control its lifecycle, such as closing itself.
import { useActiveModal } from "@sentuh/react-bootstrap-modal";
interface Props {
onChange: (result: { name: string }) => void;
}
export const ExampleModal = ({ onChange }: Props) => {
const activeModal = useActiveModal();
const handleSave = () => {
activeModal.dismiss();
onChange({ name: "John Doe" });
};
return (
<>
<div className="modal-header">
<h4>Example Modal Header</h4>
</div>
<div className="modal-body">
<p>This is an example reusable modal component.</p>
</div>
<div className="modal-footer">
<button className="btn btn-secondary" onClick={activeModal.dismiss}>
Cancel
</button>
<button className="btn btn-primary" onClick={handleSave}>
Save
</button>
</div>
</>
);
};This approach is ideal for reusable modal components that need to self-manage close actions.
To provide visual feedback when a user clicks the backdrop of a static modal (where clicking outside does not close it), you can add a custom animation to your global CSS file.
The library automatically applies the .modal-static-shake class when a static backdrop is clicked.
.modal-static-shake {
animation: shake 0.3s ease-in-out;
}
@keyframes shake {
0%,
100% {
transform: scale(1);
}
50% {
transform: scale(1.02);
}
}Note: The CSS above is just a reference for the default behavior. You can override the .modal-static-shake class with any custom transition or animation you prefer.
This section provides detailed information about the props and configuration options available for the Modal system.
The useModal hook returns an object containing methods to control the modal stack programmatically.
| Prop | Type | Description |
|---|---|---|
open |
(component: ModalComponentType, model?: any, options?: ModalOptionsProps) => Promise<any> |
Opens a modal. Resolves with data passed from the modal. |
close |
(result?: any) => void |
Close current active modal |
dismissAll() |
() => void |
Close all active modals. |
When calling modal.open(), you can pass the following arguments to define what to render and how it behaves.
| Prop | Type | Description |
|---|---|---|
| component | JSX.Element or ((props: any) => JSX.Element) |
The modal component to render. Can be a JSX element or a component function. |
| model | any |
Optional data passed to the modal component as props. Useful for pre-filling forms or passing contextual info. |
| options | ModalOptionsProps |
Optional modal configuration (size, backdrop, centered, scrollable, fullscreen, custom class). |
The options object is the third argument of the open() method. It allows you to customize the behavior and appearance of each specific modal instance.
| Prop | Type | Default | Description |
|---|---|---|---|
| backdrop | boolean or static |
true | Whether a backdrop is shown. "static" disables closing on click outside. |
| keyboard | boolean |
false | Allow ESC key to close modal. |
| size | sm, lg, xl, fullscreen |
md | Sets the modal size. |
| centered | boolean |
false | Vertically center the modal. |
| scrollable | boolean |
false | Enable scrolling within modal body. |
| modalClassName | string |
"" | Add custom CSS class to the modal container |
This hook is intended for use inside the component being rendered as a modal (the child component). It provides access to the current modal instance's lifecycle controls.
| Prop | Type | Description |
|---|---|---|
close() |
(result?: any) => void |
Closes the modal and returns a success result to the caller. Use this for "Submit," "Save," or "OK" actions. |
dismiss() |
() => void |
Closes the modal without a result, typically triggering a cancel or rejection. Use this for "Cancel" buttons |