0.1.22 • Published 27 days ago
rc-global-modal v0.1.22
rc global modal
manage modal using global state in react project
Try it out with Storybook
Table of Contents
Installation
npm i rc-global-modal
Features
- Open and close modal from anywhere in the app(inside
ModalProvider
) - Custom CSS style for modal and overlay
- Ref for modal container and overlay
- Choose animation type and duration for modal
Props
Props | Types | Required | Default | Description |
---|---|---|---|---|
children | React.FC | ✅ | component that will be displayed inside modal | |
id | string \| number | ✅ | unique id for modal | |
closeOnOverlayClick | boolean | ❌ | true | close modal when overlay is clicked |
modalContainerClassName | string | ❌ | class name for modal container | |
overlayClassName | string | ❌ | class name for overlay | |
modalContainerStyle | React.CSSProperties | ❌ | custom style for modal container | |
overlayStyle | React.CSSProperties | ❌ | custom style for overlay | |
closeOnEsc | boolean | ❌ | true | close modal when esc key is pressed |
modalContainerRef | React.RefObject | ❌ | ref for modal container | |
overlayRef | React.RefObject | ❌ | ref for overlay | |
animationType | 'fade' \| 'slideUp' \| 'slideDown' \| 'slideLeft' \| 'slideRight' \| 'zoom' | ❌ | fade | animation type for modal |
animationDuration | number | ❌ | 300 | animation duration for modal |
Usage
- Wrap your app with
ModalProvider
component
import { ModalProvider } from 'rc-global-modal';
function App() {
return (
<ModalProvider>
<Home />
</ModalProvider>
);
}
- Use
Modal
component to display modal(must be insideModalProvider
)
import { Modal } from 'rc-global-modal';
const Home = () => {
return (
<Modal id={1}>
<h1>IdOneModal</h1>
</Modal>
);
};
- Use
openModal
andcloseModal
function to open and close modal(must be insideModalProvider
)
import { Modal, useModal } from 'rc-global-modal';
const Home = () => {
const { openModal, closeModal } = useModal();
return (
<div>
<button onClick={() => openModal(1)}>open Id One modal</button>
<Modal id={1}>
<h1>IdOneModal</h1>
<button onClick={closeModal}>close modal</button>
</Modal>
</div>
);
};
Examples
import { Modal, useModal } from 'rc-global-modal';
const Home = () => {
const { openModal, closeModal } = useModal();
return (
<div>
<button onClick={() => openModal(1)}>open Id One modal</button>
<button onClick={() => openModal(2)}>open Id Two modal</button>
<Modal id={1}>
<h1>IdOneModal</h1>
<button onClick={closeModal}>close modal</button>
</Modal>
<Modal id={2}>
<h1>IdTwoModal</h1>
<button onClick={closeModal}>close modal</button>
</Modal>
</div>
);
};
export default Home;
License
MIT
0.1.21
27 days ago
0.1.22
27 days ago
0.1.20
2 months ago
0.1.18
2 months ago
0.1.19
2 months ago
0.1.17
2 months ago
0.1.14
2 months ago
0.1.15
2 months ago
0.1.16
2 months ago
0.1.10
3 months ago
0.1.11
3 months ago
0.1.12
3 months ago
0.1.13
3 months ago
0.1.8
3 months ago
0.1.7
3 months ago
0.1.9
3 months ago
0.1.6
3 months ago
0.1.2
5 months ago
0.1.4
5 months ago
0.1.3
5 months ago
0.1.5
5 months ago
0.1.0
5 months ago
0.1.1
5 months ago
0.0.9
5 months ago
0.0.8
5 months ago
0.0.5
5 months ago
0.0.4
5 months ago
0.0.7
5 months ago
0.0.6
5 months ago
0.0.3
5 months ago
0.0.2
5 months ago
0.0.1
5 months ago
0.0.0
5 months ago