Skip to content

Commit 80f4bfa

Browse files
committed
feat: 📝 Add first documentation draft
1 parent 81e9044 commit 80f4bfa

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

README.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# react-use-modal
2+
3+
react-use-modal is a custom hook that provides a flexible and reusable way to manage modals in React applications.
4+
5+
## Highlights
6+
7+
- Great **flexibiliy** with single _jsx_ tag updated dynamically
8+
- Small and minified **bundle size**
9+
- Type safe with **TypeScript**
10+
11+
## Table of Contents
12+
13+
- [Installation](#installation)
14+
- [Usage](#usage)
15+
- [API](#api)
16+
- [Properties and methods](#properties-and-methods)
17+
- [Hook configuration / Modal props](#hook-configuration--modal-props)
18+
- [Buttons](#buttons)
19+
- [Contributors](#contributors)
20+
21+
---
22+
23+
## Installation
24+
25+
To install the package, you can use npm or yarn:
26+
27+
```bash
28+
npm install react-use-modal
29+
```
30+
31+
## Usage
32+
33+
1. Import the `useModal` hook
34+
35+
```jsx
36+
import { useModal } from "react-use-modal";
37+
```
38+
39+
2. **Call the hook** to get modal configuration and relative helper functions: `modalConfig` and `setModalConfig`.
40+
41+
```jsx
42+
const { setModalConfig, modalConfig } = useModal();
43+
```
44+
45+
3. **Spread** `modalConfig` on your Modal component (you can find an example of a custom Modal component implementation below)
46+
47+
```jsx
48+
const MyPageComponent = () => {
49+
const { setModalConfig, modalConfig } = useModal();
50+
51+
return (
52+
<>
53+
...Some page content here...
54+
<Modal {...modalConfig} />
55+
</>
56+
);
57+
};
58+
```
59+
60+
4. Use the `setModalConfig` function wherever you want passing a **configuration object** to it.
61+
62+
```jsx
63+
const MyPageComponent = () => {
64+
const { setModalConfig, modalConfig } = useModal();
65+
66+
const handleOpenModal = () => {
67+
setModalConfig({
68+
open: true,
69+
title: "I'm the first modal",
70+
children:
71+
"Content message of the modal inside a component of your choice",
72+
buttons: [
73+
{
74+
text: "Confirm",
75+
},
76+
],
77+
});
78+
};
79+
80+
return (
81+
<>
82+
...Some page content here...
83+
<button onClick={handleOpenModal}>Open Modal</button>
84+
<Modal {...modalConfig} />
85+
</>
86+
);
87+
};
88+
```
89+
90+
## API
91+
92+
### Properties and methods
93+
94+
`IUseModalReturn`
95+
96+
| Property | Type | Default | Description |
97+
| ------------------- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ----------- |
98+
| `modalConfig` | `IExtendedModalConfig` | The current configuration of the modal (usable to as Modal props), auto inject handleClose function |
99+
| `setModalConfig` | `Dispatch<SetStateAction<IModalConfig>>` | Replace the entire configuration of the modal with the `IModalConfig object` provided as the argument |
100+
| `updateModalConfig` | `(config: Partial<IModalConfig>) => void` | Updates the configuration of the modal with the properties provided in the config object (merge with current state) |
101+
| `toggleModal` | `() => void` | Toggles the visibility of the modal |
102+
| `showModal` | `() => void` | Shows the modal |
103+
| `hideModal` | `() => void ` | Hides the modal |
104+
105+
### Hook configuration / Modal props
106+
107+
`IModalConfig` / `IExtendedModalConfig`
108+
109+
The following properties can be passed as **configuration options** when calling the useModal hook, or as props to your modal component.
110+
Your modal is supposed to have (or extend) the same shape of hook configuration interface: in particular the hook implement `IModalConfig` interface while the modal is supposed to extend the already _extended_ `IExtendedModalConfig` the automatically injects also and `handleClose` function to close the modal when clicking on backdrop or non-disabled buttons.
111+
112+
| Property | Type | Default | Description |
113+
| ---------------- | ---------------- | ----------- | --------------------------------------- |
114+
| `open` | `boolean` | `false` | Whether the modal is currently open |
115+
| `title?` | `string` | `undefined` | The title of the modal |
116+
| `showCloseIcon?` | `boolean` | `undefined` | The content to display within the modal |
117+
| `buttons?` | `IModalButton[]` | `undefined` | Whether the modal is currently open |
118+
119+
### Buttons
120+
121+
`IModalButton`
122+
123+
| Property | Type | Default | Description |
124+
| --------------- | --------------- | ----------- | --------------------------------------------------------------- |
125+
| `text` | `string` | `` | The text to display on the button |
126+
| `style?` | `CSSProperties` | `{}` | Custom styles to apply to the button |
127+
| `onClick?` | `() => void ` | `undefined` | A function to execute when the button is clicked |
128+
| `disabled?` | `boolean` | `undefined` | Whether the button should be disabled |
129+
| `disableClose?` | `boolean` | `undefined` | Whether the modal should be kept open after clicking the button |
130+
131+
## Contributors
132+
133+
Although I am committing the code of this project, life is always a team effort. Here you can find a list of people who contributed to this repo.
134+
135+
- [dennib](https://github.com/dennib)
136+
- [PierluigiBarocci](https://github.com/PierluigiBarocci)

0 commit comments

Comments
 (0)