Skip to content

Commit b709c61

Browse files
committed
Merge branch 'docs' into develop
2 parents 4282f71 + 4af2ef8 commit b709c61

File tree

2 files changed

+126
-7
lines changed

2 files changed

+126
-7
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: 125 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,28 @@
11
# useModal
22

3-
![banner](https://user-images.githubusercontent.com/13068594/226100508-84efce17-4f13-4800-a507-ba9ac42e8f26.jpg)
3+
![banner2](https://user-images.githubusercontent.com/13068594/226136792-623a7e44-d93a-4b04-b8b4-2ff7c6f40336.jpg)
44

55
react-use-modal is a custom hook that provides a flexible and reusable way to manage modals in React applications.
66

7+
Use a single line of `jsx` tag to insert your `Modal component` in page and use `useModal` configuration as props. Then go from there: update the same modal with dynamic content with functions provided by the hook.
8+
9+
In its basic implementation `useModal` lets you manage multiple modals in page in a reusable way without the need to write complex jsx code with as many Modal components (and relative states) as the number of different modals you want.
10+
711
## Highlights
812

9-
- Great **flexibiliy** with single _jsx_ tag updated dynamically
10-
- Small and minified **bundle size**
13+
- Great **flexibiliy**: use a single `jsx` tag updated dynamically for all the modals in you page...
14+
- ...or decouple part of the logic from the hook and manage it yourself in your page/component
15+
- **Small** and minified **bundle size**
1116
- Type safe with **TypeScript**
1217

1318
## Table of Contents
1419

1520
- [Installation](#installation)
1621
- [Usage](#usage)
22+
- [Basic](#basic)
23+
- [Alternative](#alternative)
24+
- [Partial config update](#update-current-config-partially)
25+
- [Decoupled logic](#decoupled-logic)
1726
- [API](#api)
1827
- [Properties and methods](#properties-and-methods)
1928
- [Hook configuration / Modal props](#hook-configuration--modal-props)
@@ -32,7 +41,9 @@ npm install react-use-modal
3241

3342
## Usage
3443

35-
1. Import the `useModal` hook
44+
### Basic
45+
46+
1. **Import** the `useModal` hook
3647

3748
```jsx
3849
import { useModal } from "react-use-modal";
@@ -82,13 +93,120 @@ const MyPageComponent = () => {
8293
return (
8394
<>
8495
...Some page content here...
85-
<button onClick={handleOpenModal}>Open Modal</button>
96+
<div onClick={handleOpenModal} className="btn">
97+
Show modal
98+
</div>
8699
<Modal {...modalConfig} />
87100
</>
88101
);
89102
};
90103
```
91104

105+
![example1](https://user-images.githubusercontent.com/13068594/226113275-1ede9847-f6e8-4e97-87d0-d353bee5f4e0.jpg)
106+
107+
> _Basic example: click on "Show modal" button will execute `setModalConfig` with the config we defined_.
108+
109+
### Alternative
110+
111+
#### **Update** current config **partially**
112+
113+
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
114+
115+
```jsx
116+
const MyPageComponent = () => {
117+
const { setModalConfig, modalConfig, updateModalConfig } = useModal();
118+
119+
const handleOpenModal = () => {
120+
setModalConfig({
121+
open: true,
122+
title: "I'm the first modal",
123+
children:
124+
"Content message of the modal inside a component of your choice",
125+
buttons: [
126+
{
127+
text: "Cancel",
128+
},
129+
{
130+
text: "Confirm",
131+
disableClose: true,
132+
onClick: showSecondModal,
133+
},
134+
],
135+
});
136+
137+
const showSecondModal = () => {
138+
updateModalConfig({
139+
title: "I'm the second modal",
140+
children:
141+
"Are you sure you want to proceed?",
142+
buttons: [
143+
{
144+
text: "Yes, I am!",
145+
},
146+
],
147+
});
148+
}
149+
150+
151+
// ... same jsx content as seen above
152+
153+
};
154+
```
155+
156+
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.
157+
158+
![example2](https://user-images.githubusercontent.com/13068594/226114740-d2d81e7c-ca27-4b9e-a055-0eb1de71a757.jpg)
159+
160+
> _Alternative usage: here you can see `updateModalConfig` in action, passing from previous config (left) to partially modified one (right)_.
161+
162+
#### Decoupled logic
163+
164+
If you have some complex, maybe verbose piece of `jsx` (eg. for `children` or `buttons`) you can decouple some props handling from the hook thus leveraging from it only part of the configuration while managing the other part manually in your page/component, let's see an example for state dependent children.
165+
166+
```jsx
167+
const initialModalConfig = {
168+
title: "I'm a complex modal!",
169+
};
170+
171+
const MyPageComponent = () => {
172+
const { modalConfig, showModal } = useModal();
173+
const [isSomethingActive, setIsSomethingActive] = useState < boolean > false;
174+
175+
const currentButtons: IModalButton[] = isSomethingActive
176+
? [{ text: "Button for active state" }]
177+
: [{ text: "Inactive btn 1" }, { text: "Inactive btn 2" }];
178+
179+
return (
180+
<>
181+
<div onClick={showModal} className="btn">
182+
Show modal
183+
</div>
184+
185+
<div
186+
onClick={() => setIsSomethingActive((prev) => !prev)}
187+
className="btn"
188+
>
189+
Toggle active state
190+
</div>
191+
192+
<Modal {...modalConfig} buttons={currentButtons}>
193+
{isSomethingActive ? (
194+
<div>Some jsx here for the active state</div>
195+
) : (
196+
<div>Some other jsx for inactive state</div>
197+
)}
198+
</Modal>
199+
</>
200+
);
201+
};
202+
```
203+
204+
Here yoy can see an example of `useModal` usage with non-empty initial configuration, showing the modal through `showModal` function and more importantly the decoupled/rewritten props `children` and `buttons`. This pattern can be used for complex, verbose or state dependent content.
205+
206+
![example3](https://user-images.githubusercontent.com/13068594/226135165-5812a194-bb99-4a2c-bc17-936604a7c2ef.jpg)
207+
208+
> _Alternative usage: config through hook inital config, modal shown with `showModal`, rewritten `buttons` and `children` Modal props_.
209+
92210
## API
93211
94212
### Properties and methods
@@ -115,7 +233,8 @@ Your modal is supposed to have (or extend) the same shape of hook configuration
115233
| ---------------- | ---------------- | ----------- | --------------------------------------- |
116234
| `open` | `boolean` | `false` | Whether the modal is currently open |
117235
| `title?` | `string` | `undefined` | The title of the modal |
118-
| `showCloseIcon?` | `boolean` | `undefined` | The content to display within the modal |
236+
| `showCloseIcon?` | `boolean` | `undefined` | Whether to show top right close icon |
237+
| `children?` | `ReactNode` | `undefined` | The content to display within the modal |
119238
| `buttons?` | `IModalButton[]` | `undefined` | Whether the modal is currently open |
120239
121240
### Buttons

0 commit comments

Comments
 (0)