Skip to content

Commit 4af2ef8

Browse files
committed
docs: 📝 Add decoupled usage docs and fix something here and there
1 parent 1450515 commit 4af2ef8

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

README.md

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
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

913
- Great **flexibiliy**: use a single `jsx` tag updated dynamically for all the modals in you page...
@@ -16,8 +20,9 @@ react-use-modal is a custom hook that provides a flexible and reusable way to ma
1620
- [Installation](#installation)
1721
- [Usage](#usage)
1822
- [Basic](#basic)
19-
- [Advanced](#advanced)
23+
- [Alternative](#alternative)
2024
- [Partial config update](#update-current-config-partially)
25+
- [Decoupled logic](#decoupled-logic)
2126
- [API](#api)
2227
- [Properties and methods](#properties-and-methods)
2328
- [Hook configuration / Modal props](#hook-configuration--modal-props)
@@ -101,11 +106,11 @@ const MyPageComponent = () => {
101106

102107
> _Basic example: click on "Show modal" button will execute `setModalConfig` with the config we defined_.
103108
104-
### Advanced
109+
### Alternative
105110

106111
#### **Update** current config **partially**
107112

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_
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
109114

110115
```jsx
111116
const MyPageComponent = () => {
@@ -148,12 +153,59 @@ const MyPageComponent = () => {
148153
};
149154
```
150155
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._
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.
152157
153158
![example2](https://user-images.githubusercontent.com/13068594/226114740-d2d81e7c-ca27-4b9e-a055-0eb1de71a757.jpg)
154159
155-
> _Advanced usage: here you can see `updateModalConfig` in action, passing from previous config (left) to partially modified one (right)_.
156-
## Examples
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_.
157209
158210
## API
159211
@@ -181,7 +233,8 @@ Your modal is supposed to have (or extend) the same shape of hook configuration
181233
| ---------------- | ---------------- | ----------- | --------------------------------------- |
182234
| `open` | `boolean` | `false` | Whether the modal is currently open |
183235
| `title?` | `string` | `undefined` | The title of the modal |
184-
| `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 |
185238
| `buttons?` | `IModalButton[]` | `undefined` | Whether the modal is currently open |
186239
187240
### Buttons

0 commit comments

Comments
 (0)