@morganlethuaut/react-modal-component
Version:
A reusable React modal component.
145 lines (96 loc) ⢠2.83 kB
Markdown
A simple, reusable React modal component.
Install the component with:
```bash
npm install @morganlethuaut/react-modal-component
yarn add @morganlethuaut/react-modal-component
pnpm add @morganlethuaut/react-modal-component
bun add @morganlethuaut/react-modal-component
```
Once the package is installed, you can use the modal with it:
```js
import Modal from '@morganlethuaut/react-modal-component'
```
- ā
Simple and reusable
- āļø Fully typed with TypeScript
- āæļø Accessible (aria tags + focus control)
- š« Optional close button
The modal is used as a simple React component that accepts any content.
```js
<Modal>
// your code...
</Modal>
```
Some properties are mandatory:
`show` is a Boolean state indicating whether the modal is open or not.
- **type**: `boolean`
- **required**
To manipulate the state of the `show` property, the modal expects the `setShow` update function.
- **type**: `React.Dispatch<React.SetStateAction<boolean>>`
- **required**
---
Properties that are not mandatory:
Adding classes.
- **type**: `string`
The focus element when the modal window is open.<br>
By default, the modal focuses the close button.
- **type**: `React.RefObject<HTMLElement | null>`
Identifies the element (or elements) that labels the element it is applied to.
- **type**: `string`
### ariaDescribedby
Identifies the element (or elements) that describes the element on which the attribute is set.
- **type**: `string`
### disabledCloseBtn
Deactivates the default close button.
- **type**: `string`
## **Exemple**
Here's an example of how to use a React component to display a message in a modal window:
```jsx
import { useState } from 'react'
import Modal from '@morganlethuaut/react-modal-component'
export default function Welcome()
{
const [SHOW, setShow] = useState(false)
return <>
<button
type="button"
onClick={() => setShow(true)}
>
Display welcome message !
</button>
<Modal
show={SHOW}
setShow={setShow}
ariaLabelledby="label"
ariaDescribedby="description"
>
<h2
id="label"
>
Welcome !
</h2>
<p
id="description"
>
This is a message to welcome you.
</p>
</Modal>
</>
}
```
This component is distributed under the **ISC** license.
Ā© 2025 Morgan Le Thuaut