https://github.com/rkdrnf/react-modal-state
Easily manipulate modals in react
https://github.com/rkdrnf/react-modal-state
contextapi hooks modal react state
Last synced: 7 months ago
JSON representation
Easily manipulate modals in react
- Host: GitHub
- URL: https://github.com/rkdrnf/react-modal-state
- Owner: rkdrnf
- Created: 2023-10-12T15:30:14.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-19T08:28:34.000Z (over 2 years ago)
- Last Synced: 2024-11-17T10:24:18.834Z (about 1 year ago)
- Topics: contextapi, hooks, modal, react, state
- Language: TypeScript
- Homepage:
- Size: 111 KB
- Stars: 14
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# React Modal State
This is a tiny library that manages modal in react with minimal dependency.
> [!IMPORTANT]
> This library is in an early stage. Any issue or feature request would be very helpful to develop this library further!
# Installation
```bash
npm i react-modal-state
```
# Usage
### Place ModalProvider and ModalRenderer
```JSX
import { ModalProvider, ModalRenderer } from "react-modal-state";
...
// Optionally provide modals prop to open modal by id or to use self-nested modals
```
> [!NOTE]
> `` does not have to be the direct child of the ``. You can place `` in any node under `` component tree if needed.
### Open from anywhere
```JSX
import { useModal } from "react-modal-state";
function Content() {
const { open: openNewUser } = useModal(NewUserModal);
const { open: openUser } = useModal(UserModal);
// or alternatively, you can open modal by id defined in ModalProvider
// const { open: openUser } = useModal("users/:id");
return (
<>
openNewUser()}>New User
openUser({ id: 1, name: "Steve" })}>User 1
>
);
}
```
### Declare modal component
```JSX
import { useModalInstance } from "react-modal-state";
const UserModal = () => {
const { data, isOpen, close } = useModalInstance<{
id: number;
name: string;
}>();
return (
User {data.id}
{data.name}'s profile
);
};
```
# Features
- Very small bundle footprint
- Allow to write modal component with caller interface ignorant manner.
- UI Framework independant. Can be used with any UI framework such as MUI or Headless UI
- Supports nested modal of the same component using parameterized path(`users/:id`). for example, `users/1` can be opened over `users/2`
# Backgrounds
There are many awkward and repetitive points when using modals naively and this library tries to solve these problems.
Typical scenarios are as below.
- Many modals require extra data to be passed when opening modal. If you start to manage more and more modals, component state which opens modal soon become very reptitive with declarations of modal state managing its open state and extra data.
- When you need to open a parent modal from a deeply nested component, you typically have choices below.
- Pass props to open modal down to the child component. which usually causes a problem known as a prop drilling.
- Provide methods to manage a modal via Context API or other state management libraries. which also became tedious if you need to manage multiple modals.
Using this library, you can solve above situations elegantly.
Below is an minimal example code using this library (with dialog component from MUI)
```JSX
const NewUserModal = () => {
const { isOpen, close } = useModalInstance();
return (
New User
Create new user
);
};
const UserModal = () => {
const { data, isOpen, close } = useModalInstance<{
id: number;
name: string;
}>();
const { open: openUser } = useModal(`users/:id`);
return (
User {data.id}
{data.name}'s profile
);
};
function Content() {
const { open: openNewUser } = useModal("new-user");
const { open: openUser } = useModal("users/:id");
return (
openNewUser()}>New User
openUser({ id: 1, name: "Steve" })}>User 1
);
}
function App() {
return (
);
}
```
In the above code, you can see benefits like
- A component which is responsible to open modal does not need to manage any extra state which is needed in modal. It only calls open (and close if needed)
- Modal component can access methods and custom properties need to render modal by itself. No need to manage props to provide data, which enables modals to be declared and implemented fully indepenently without worrying about the interface it has to provide to opening components.
# Demo and Development
This is a typical vite project.
```bash
npm install
npm run dev
```
to run demo page.