Skip to content

Commit 1450515

Browse files
committed
docs: 📝 Update docs with first alternative usage (partial update)
1 parent 874fdb9 commit 1450515

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"editor.tokenColorCustomizations": {
3-
"comments": "#5c5c5c",
3+
"comments": "",
44
"textMateRules": []
55
}
66
}

README.md

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ react-use-modal is a custom hook that provides a flexible and reusable way to ma
1515

1616
- [Installation](#installation)
1717
- [Usage](#usage)
18+
- [Basic](#basic)
19+
- [Advanced](#advanced)
20+
- [Partial config update](#update-current-config-partially)
1821
- [API](#api)
1922
- [Properties and methods](#properties-and-methods)
2023
- [Hook configuration / Modal props](#hook-configuration--modal-props)
@@ -33,7 +36,9 @@ npm install react-use-modal
3336

3437
## Usage
3538

36-
1. Import the `useModal` hook
39+
### Basic
40+
41+
1. **Import** the `useModal` hook
3742

3843
```jsx
3944
import { useModal } from "react-use-modal";
@@ -83,16 +88,73 @@ const MyPageComponent = () => {
8388
return (
8489
<>
8590
...Some page content here...
86-
<div onClick={handleOpenModal} className="btn">Show modal</div>
91+
<div onClick={handleOpenModal} className="btn">
92+
Show modal
93+
</div>
8794
<Modal {...modalConfig} />
8895
</>
8996
);
9097
};
9198
```
9299

93100
![example1](https://user-images.githubusercontent.com/13068594/226113275-1ede9847-f6e8-4e97-87d0-d353bee5f4e0.jpg)
101+
94102
> _Basic example: click on "Show modal" button will execute `setModalConfig` with the config we defined_.
95103
104+
### Advanced
105+
106+
#### **Update** current config **partially**
107+
108+
_Maybe you want to update only part of current `modalConfig`, let's say we have an open modal and we would like to change part of it's content when the user presses one of it's buttons. We can achieve this by using the functino `updateModalConfig` returned by the hook like so_
109+
110+
```jsx
111+
const MyPageComponent = () => {
112+
const { setModalConfig, modalConfig, updateModalConfig } = useModal();
113+
114+
const handleOpenModal = () => {
115+
setModalConfig({
116+
open: true,
117+
title: "I'm the first modal",
118+
children:
119+
"Content message of the modal inside a component of your choice",
120+
buttons: [
121+
{
122+
text: "Cancel",
123+
},
124+
{
125+
text: "Confirm",
126+
disableClose: true,
127+
onClick: showSecondModal,
128+
},
129+
],
130+
});
131+
132+
const showSecondModal = () => {
133+
updateModalConfig({
134+
title: "I'm the second modal",
135+
children:
136+
"Are you sure you want to proceed?",
137+
buttons: [
138+
{
139+
text: "Yes, I am!",
140+
},
141+
],
142+
});
143+
}
144+
145+
146+
// ... same jsx content as seen above
147+
148+
};
149+
```
150+
151+
_Here we've added a second button, the cancel button simply abort the operation closing the modal (so has no need for additional handlers, the confirm button is stopped from closing the modal, we pass it a handler which calls `updteModalConfig` from the hook and we update with it only the parts of the config that we need._
152+
153+
![example2](https://user-images.githubusercontent.com/13068594/226114740-d2d81e7c-ca27-4b9e-a055-0eb1de71a757.jpg)
154+
155+
> _Advanced usage: here you can see `updateModalConfig` in action, passing from previous config (left) to partially modified one (right)_.
156+
## Examples
157+
96158
## API
97159
98160
### Properties and methods

0 commit comments

Comments
 (0)