react-motion-modal is a flexible modal management library built with Zustand for state management and powered by Framer Motion for animations.
Documentation: https://sonnv1912.github.io/react-motion-modal/
- Stack-based modal management
- Type-safe parameters per modal
- Auto-injection of
closeModal()into modal props - Full IntelliSense for modal names and parameters
- Position-aware default animations
npm install react-motion-modal motion zustandor:
yarn add react-motion-modal motion zustandreact and react-dom are expected to already exist in your app.
import 'react-motion-modal/style.css';import type { ModalDefinition } from 'react-motion-modal';
declare module 'react-motion-modal' {
interface ModalDefinition {
AlertModal: {
title: string;
};
ConfirmModal: {
onConfirm: () => void;
};
}
}import { ModalProvider } from 'react-motion-modal';
export const App = () => {
return (
<div>
<ModalProvider
modals={{
AlertModal,
ConfirmModal,
}}
/>
</div>
);
};import { MODAL_POSITIONS, modalStore } from 'react-motion-modal';
const { openModal } = modalStore();
openModal('ConfirmModal', {
title: 'Are you sure you want to delete?',
position: MODAL_POSITIONS.RIGHT,
closeOnClickOutside: true,
});import type { ModalParams } from 'react-motion-modal';
const ConfirmModal = ({ title, closeModal }: ModalParams<'ConfirmModal'>) => (
<div>
<h1>{title}</h1>
<button onClick={closeModal}>Close</button>
</div>
);Opens a modal by name. The params type is inferred from the modal definition.
- If
nameis provided, closes that specific modal. - If not, closes the most recent modal.
Each modal automatically receives:
| Prop | Type | Description |
|---|---|---|
closeModal |
() => void |
Close function for the modal |
onClose |
() => void |
Callback triggered when the modal closes |
closeOnClickOutside |
boolean |
Close modal when clicking outside |
closeOnPressEsc |
boolean |
Close modal when Escape is pressed |
position |
string |
Modal placement. Unknown values fall back to center. |
backdrop |
{ className?: string; style?: CSSProperties } |
Customize the backdrop layer |
body |
{ className?: string; style?: CSSProperties } |
Customize the modal body wrapper |
animate |
{ animate?: TargetAndTransition; exit?: TargetAndTransition } |
Motion config. Custom animation disables the built-in position default |
blur |
boolean |
Apply backdrop blur |
MODAL_POSITIONS.TOP;
MODAL_POSITIONS.TOP_LEFT;
MODAL_POSITIONS.TOP_RIGHT;
MODAL_POSITIONS.TOP_CENTER;
MODAL_POSITIONS.LEFT;
MODAL_POSITIONS.RIGHT;
MODAL_POSITIONS.CENTER;
MODAL_POSITIONS.CENTER_FULL;
MODAL_POSITIONS.BOTTOM;
MODAL_POSITIONS.BOTTOM_LEFT;
MODAL_POSITIONS.BOTTOM_RIGHT;
MODAL_POSITIONS.BOTTOM_CENTER;Default animation follows the selected position.
- CI runs from
.github/workflows/ci.yml. - npm publishing is defined in
.github/workflows/publish.yml. - Docs deploy to GitHub Pages from
.github/workflows/deploy-docs.yml.
apps/library: published npm packageapps/document: Docusaurus documentation sitepackages/typescript-config: shared TypeScript configs