React-Morphing-Modal
- React-Morphing-Modal
- Demo
- Installation
- Features
- Usage
- Api
- Browser Support
- Release Notes
- Contribute
- License
Demo
Installation
$ npm install --save react-morphing-modal
//or
$ yarn add react-morphing-modal
Features
- Easy to setup for real, you can make it work in less than 10sec! 🚀
- Super easy to customize 👌
- Fancy 😎
Usage
The library expose 2 ways to display the modal:
getTriggerProps
andopen
. For the basic use casegetTriggerProps
is fine. But for most of the cases usingopen
is the way to go. Please look at the api for more details.
Basic example
;;; { const modalProps getTriggerProps = ; return <div> <button ...>Show modal</button> <Modal ...modalProps>Hello World</Modal> </div> ;}
With a component
Simple case
If you just want to open the modal you can stick with getTriggerProps
.
;;; const Button = <button ...props>Show modal</button>; { const modalProps getTriggerProps = ; return <div> <Button getTriggerProps=getTriggerProps /> <Modal ...modalProps>Hello World</Modal> </div> ;}
Real app use case
Most of the time you need to perform different task when a user click a button like calling an api. In that case
use the open
method as follow.
;;; const Button = { const btnRef = ; { // do some complicated stuff ; } return <button ref=btnRef onClick=handleClick> Show modal </button> ;}; { const modalProps open = ; return <div> <Button openModal=open /> <Modal ...modalProps>Hello World</Modal> </div> ;}
Use different trigger for the same modal
;;; { const modalProps getTriggerProps activeModal open = ; const triggerRef = ; const handleTrigger3 = ; return <div> <button ...>Trigger 1</button> <button ...>Trigger 2</button> <button ref=triggerRef onClick=handleTrigger3> Trigger 3 </button> <Modal ...modalProps>Hello World</Modal> </div> ;}
Attribute an id to the trigger
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ...>Trigger 1</button> <button ...>Trigger 2</button> /* You can also pass an object */ <button ...>Trigger 3</button> <span>activeModal</span> <Modal ...modalProps>Hello World</Modal> </div> ;}
Define onOpen and onClose callback
Gloabaly
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ...>Trigger</button> <Modal ...modalProps>Hello World</Modal> </div> ;}
Per trigger
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ... > Trigger </button> <Modal ...modalProps>Hello World</Modal> </div> ;}
Define background
By default, the modal background is the same as the trigger one. However, you are free to define yours.
Gloabaly
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ...>Trigger</button> <Modal ...modalProps>Hello World</Modal> </div> ;}
Per trigger
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ... > Trigger </button> <Modal ...modalProps>Hello World</Modal> </div> ;}
Use another event to trigger the modal
By default, the onClick
event is used on the trigger.
Gloabaly
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ...>Trigger</button> <Modal ...modalProps>Hello World</Modal> </div> ;}
Per trigger
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ... > Trigger </button> <Modal ...modalProps>Hello World</Modal> </div> ;}
Hide the close button
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ...>Trigger</button> <Modal ...modalProps closeButton=false> Hello World </Modal> </div> ;}
Remove body padding
;;; { const modalProps getTriggerProps activeModal = ; return <div> <button ...>Trigger</button> <Modal ...modalProps padding=false> Hello World </Modal> </div> ;}
Api
useModal
HookOptions
; const open close activeModal modalProps getTriggerProps = ;
Props | Type | Default | Description |
---|---|---|---|
event | string | onClick | Any valid react dom event: onClick, onDoubleClick, etc... |
onOpen | function | - | A function to call when the modal will open |
onClose | function | - | A function to call when the modal will close |
background | string | - | Any valid css background: #fffff, rgb(1,1,1), etc |
open
open
have 2 signatures
; const open = ;// pass a ref to your triggerconst myRef = React; //somewhere in your app<MyComponent ref=myRef />; ;;
Props | Type | Default | Description |
---|---|---|---|
id | string | number | symbol | null | - | Specify a modal id. It will be assigned to activeModal |
onOpen | function | - | A function to call when the modal will open |
onClose | function | - | A function to call when the modal will close |
background | string | - | Any valid css background: #fffff, rgb(1,1,1), etc |
close
close
does not require any options.
; const close = ; ;
activeModal
activeModal
hold the displayed modalId. activeModal
is set to null
if not id has been used.
; const open activeModal = ; ;console ; // print modalId
modalProps
modalProps
hold the props that must be passed to the Modal component.
; const modalProps = ; <Modal ...modalProps />;
getTriggerProps
getTriggerProps
is a convenient method for the simple use case. Under the hood a ref is created and bound to open
.
getTriggerProps
has also 2 signatures.
; const getTriggerProps = ; <button ...>trigger</button>;<button ...>trigger</button>
Props | Type | Default | Description |
---|---|---|---|
id | string | number | symbol | null | - | Specify a modal id. It will be assigned to activeModal |
event | string | onClick | Any valid react dom event: onClick, onDoubleClick, etc... |
onOpen | function | - | A function to call when the modal will open |
onClose | function | - | A function to call when the modal will close |
background | string | - | Any valid css background: #fffff, rgb(1,1,1), etc |
Modal Component
; <Modal closeButton=true padding=true> hello</Modal>;
Props | Type | Default | Description |
---|---|---|---|
closeButton | boolean | true | Display a close button |
padding | boolean | true | Remove the default padding. Useful when doing some customisation |
Browser Support
![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|
❌ | ✅ | ✅ | ✅ | ✅ |
Release Notes
You can find the release note for the latest release here
You can browse them all here
Contribute
Show your ❤️ and support by giving a ⭐. Any suggestions are welcome ! Take a look at the contributing guide.
You can also find me on reactiflux. My pseudo is Fadi.
License
Licensed under MIT