|
1 | 1 | # react-confirm
|
2 |
| -Small library which makes your Dialog component callable. |
| 2 | +react-confirm is a lightweight library that simplifies the implementation of confirmation dialogs in React applications by offering a Promise-based API that works seamlessly with async/await syntax, similar to `window.confirm`. |
3 | 3 |
|
4 |
| -This library does not provide any view component. Just adds a callable functionality to your Dialog component like `window.confirm`. |
| 4 | +Another key feature of react-confirm is that it doesn't provide a specific view or component for the confirmation dialog, which allows you to easily customize the appearance of the dialog to match your application's design. |
5 | 5 |
|
6 |
| -In the [example](https://github.com/haradakunihiko/react-confirm/tree/master/example), [react-bootstrap](https://react-bootstrap.github.io/components.html#modals) and [material-ui](http://www.material-ui.com/#/components/dialog) are used with. |
| 6 | +In the [example](https://github.com/haradakunihiko/react-confirm/tree/master/example), [react-bootstrap](https://react-bootstrap-v3.netlify.app/components/modal/) and [material-ui](http://www.material-ui.com/#/components/dialog) are used with. |
7 | 7 |
|
8 | 8 | [](https://badge.fury.io/js/react-confirm)
|
9 | 9 |
|
10 | 10 | ## Motivation
|
11 |
| - React is great. And I respect the concept to render the view reactively only by it's state. However, it easily becomes too complex to manage all temporary states like confirmation dialog. The question is... Is it worth to manage them inside your app? I guess the answer is not always yes. |
| 11 | +React is a powerful library that allows for reactive rendering based on component state. However, managing temporary states like confirmation dialogs can quickly become complex. The question is: is it worth implementing these states within your app? The answer is not always a clear yes. |
12 | 12 |
|
13 | 13 | ## What you can do
|
14 |
| - With this library, |
15 |
| - - You can open a dialog component by calling function and it will be rendered outside your application. The function returns promise so that you can define callbacks to handle the confirmation result. |
16 |
| - - You can pass arguments to the function and use them inside the dialog component. |
17 |
| - - You can get values from the component in the promise. |
18 |
| - - There is no limitation in the dialog. You can use input forms, multiple buttons, whatever you want (see demo site). |
| 14 | +react-confirm library offers several benefits: |
| 15 | + |
| 16 | +- You can open a dialog component by calling a function without appending it into your React tree. The function returns a promise, allowing you to handle confirmation results with callbacks. |
| 17 | +- You can pass arguments to the function and use them inside the dialog component. |
| 18 | +- You can retrieve values from the component in the promise. |
| 19 | +- The library provides flexibility in designing the dialog. There is no limitation in the type of components you can use, whether it be input forms or multiple buttons. You can even check out the demo site to see examples of how to customize the dialog. |
19 | 20 |
|
20 | 21 | ## Demo
|
21 | 22 | https://codesandbox.io/s/react-confirm-with-react-bootstrap-kjju1
|
22 | 23 |
|
23 | 24 | ## Versions
|
24 | 25 |
|
25 |
| -- React 18+ users should use `react-confirm` version 0.2.x |
| 26 | +- React 18+ users should use `react-confirm` version 0.2.x or 0.3.x |
26 | 27 | - React <=17 users should stick to `react-confirm` version 0.1.x
|
27 | 28 |
|
28 | 29 | ## Usage
|
@@ -109,29 +110,37 @@ const handleOnClick2 = async () => {
|
109 | 110 | You can check more complex example in [codesandbox](https://codesandbox.io/s/react-confirm-with-react-bootstrap-kjju1)
|
110 | 111 |
|
111 | 112 | ## Using with Context
|
112 |
| -By default, this library appends your component to outside of your app's React component tree. To consume context in your component, you need to put `MountComponent` to your app's tree. |
| 113 | +By default, this library renders the confirmation dialog without appending the component to your app's React component tree. While this can be useful, it may cause issues if you need to consume context in your component. To overcome this problem, you can use the `MountPoint` component to include your confirmation dialog within your app's tree, enabling it to access context and other data from the app. |
| 114 | + |
| 115 | +Create your own `createConfirmation` using `createConfirmationCreater` and `createReactTreeMounter`. |
113 | 116 |
|
114 | 117 | ```js
|
115 |
| -import { createConfirmationCreater, createReactTreeMounter, createMountComponent } from 'react-confirm'; |
| 118 | +import { createConfirmationCreater, createReactTreeMounter, createMountPoint } from 'react-confirm'; |
116 | 119 |
|
117 | 120 | const mounter = createReactTreeMounter();
|
118 | 121 |
|
119 | 122 | export const createConfirmation = createConfirmationCreater(mounter);
|
120 |
| -export const MountComponent = createMountComponent(mounter); |
| 123 | +export const MountPoint = createMountPoint(mounter); |
121 | 124 | ```
|
122 | 125 |
|
| 126 | +Put `MountPoint` into your React tree. |
123 | 127 | ```js
|
124 | 128 | const YourRootComponent = () => {
|
125 | 129 | return (
|
126 | 130 | <YourContext.Provider>
|
127 |
| - <MountComponent /> |
| 131 | + <MountPoint /> |
128 | 132 | <Toolbar />
|
129 | 133 | </YourContext.Provider>
|
130 | 134 | )
|
131 | 135 | }
|
132 | 136 | ```
|
133 | 137 |
|
134 |
| -To render into a different part of the DOM, pass dom element to `createReactTreeMounter`. This will changes the physical placement of the DOM node using `createPortal`. |
| 138 | +use your `createConfirmation` as usual. |
| 139 | +```js |
| 140 | +export const confirm = createConfirmation(YourDialog); |
| 141 | +``` |
| 142 | + |
| 143 | +To render the confirmation dialog within the React component tree but in a different part of the DOM, you can pass a DOM element to the `createReactTreeMounter` function. This will use the `createPortal` method to render the confirmation dialog in the specified DOM element while keeping it within the React component tree. |
135 | 144 |
|
136 | 145 | ```js
|
137 | 146 | const mounter = createReactTreeMounter(document.body);
|
|
0 commit comments