├── .browserslistrc
├── .eslintrc.js
├── .gitignore
├── .npmrc
├── .prettierrc
├── LICENSE
├── README.md
├── demos
└── vue3
│ ├── App.vue
│ ├── components
│ ├── Alert.vue
│ ├── EditableTable.vue
│ ├── Loader.vue
│ ├── LoginModal.vue
│ ├── PopupEdit.vue
│ └── SimpleModal.vue
│ ├── composables
│ ├── useAlertMessage.ts
│ └── useLoader.ts
│ ├── examples
│ ├── AlertExample.vue
│ ├── BasicExample.vue
│ ├── IntroductionPart.vue
│ ├── LoaderExample.vue
│ ├── PropsBehavior.vue
│ └── TableExample.vue
│ ├── main.ts
│ └── shims-vue.d.ts
├── index.html
├── jest.config.js
├── package.json
├── pnpm-lock.yaml
├── src
├── DialogsWrapper.ts
├── createConfirmDialog.ts
├── index.ts
├── shims-vue.d.ts
└── useDialogWrapper.ts
├── tests
├── components
│ └── DialogComp.ts
├── unit
│ └── test.spec.ts
└── utils
│ └── index.ts
├── tsconfig.json
└── vite.config.js
/.browserslistrc:
--------------------------------------------------------------------------------
1 | > 1%
2 | last 2 versions
3 | not dead
4 |
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | root: true,
3 | env: {
4 | es2021: true,
5 | },
6 | extends: [
7 | 'plugin:vue/vue3-essential',
8 | 'eslint:recommended',
9 | '@vue/typescript/recommended',
10 | '@vue/prettier',
11 | '@vue/prettier/@typescript-eslint',
12 | ],
13 | parserOptions: {
14 | ecmaVersion: 2020,
15 | },
16 | rules: {
17 | indent: ['error', 2],
18 | 'no-unused-vars': 'off',
19 | '@typescript-eslint/no-unused-vars': 'off',
20 | 'no-console': 'off',
21 | 'no-debugger': 'off',
22 | },
23 | overrides: [
24 | {
25 | files: [
26 | '**/__tests__/*.{j,t}s?(x)',
27 | '**/tests/unit/**/*.spec.{j,t}s?(x)',
28 | ],
29 | env: {
30 | jest: true,
31 | },
32 | },
33 | ],
34 | }
35 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | /dist
4 | pnpm-lock.yaml
5 |
6 |
7 | # local env files
8 | .env.local
9 | .env.*.local
10 |
11 | # Log files
12 | npm-debug.log*
13 | yarn-debug.log*
14 | yarn-error.log*
15 | pnpm-debug.log*
16 |
17 | # Editor directories and files
18 | .idea
19 | .vscode
20 | *.suo
21 | *.ntvs*
22 | *.njsproj
23 | *.sln
24 | *.sw?
25 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | shamefully-hoist=true
2 | strict-peer-dependencies=false
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "trailingComma": "es5",
3 | "intend": 2,
4 | "semi": false,
5 | "singleQuote": true,
6 | "useTabs": false,
7 | "endOfLine": "auto"
8 | }
9 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2022 Roman Harmyder
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # vuejs-confirm-dialog
2 |
3 | Convert your dialogs to async functions and use them all over your project!
4 |
5 | This plugin just makes it simple to create, reuse, promisify and build chains of modal dialogs in Vue.js. It provides just one function `createConfirmDialog` that does all the hard work for you.
6 |
7 | > New documentation is ready! Try a new [site](https://vcd-docs.netlify.app) that contains all the docs of the project!
8 | > [vcd-docs.netlify.app](https://vcd-docs.netlify.app)
9 |
10 | ## About
11 |
12 | How does it work? The idea is simple, this function -- `createConfirmDialog` gets a modal component and magically provides to it emits `confirm` and `cancel` and its props values. This function returns to you a dialog instance that controls the rendering of the modal component and reacts to user decisions. It reduces you to write all the boilerplate code over and over again and makes it simple to reuse your modals everywhere in your project.
13 |
14 | You can work with dialogs like with promises or with hooks that the dialog instance generates for you.
15 |
16 | ## Installation
17 |
18 | in 3 steps
19 |
20 | ### Step 0
21 |
22 | Add the plugin to your `node_modules`
23 |
24 | ```bash
25 | npm i vuejs-confirm-dialog
26 | ```
27 |
28 | ### Step 1 (optional)
29 |
30 | Install the plugin:
31 |
32 | ```js
33 | // main.js
34 | import { createApp } from 'vue'
35 | import App from './App.vue'
36 | import * as ConfirmDialog from 'vuejs-confirm-dialog'
37 |
38 | createApp(App).use(ConfirmDialog).mount('#app')
39 | ```
40 |
41 | ### Step 2
42 |
43 | Add `DialogsWrapper` to `App.vue` template:
44 |
45 | ```html
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
61 | ```
62 |
63 |
64 | And that's it. Now you can use it.
65 |
66 | ## Usage
67 |
68 | Build Modal Window. It must contain emits `confirm` and `cancel`. ~~It also must contain~~ ~~prop `show`~~. ~~Put `v-if="show"` in its template for conditional rendering~~(no longer need to).
69 |
70 | ```html
71 |
72 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 | ```
84 |
85 | Use this modal window wherever you want in your project:
86 |
87 | ```html
88 |
89 |
104 | ```
105 |
106 | ### Two ways of usage
107 |
108 | The plugin lets you decide how to use it. The first way is to use hooks:
109 |
110 | - `onConfirm` - hook gets a callback that runs after the user confirmed the modal message
111 | - `onCancel` - run callback if the user decides to click cancel
112 |
113 | The second way is promisify modal dialog. `reveal` the function returns a Promise, that resolves data and `isCanceled` boolean from the dialog after the user commits the action.
114 |
115 | for example(not real):
116 |
117 | ```html
118 |
132 | ```
133 |
134 | ## Passing data to/from the dialog
135 |
136 | It will be not so useful if we will not have the option to pass data to and from а component.
137 | There are several ways to deal with it. First of all, you can pass data to the second argument of the `createConfirmDialog` function. Data must be an object with names of properties matching to props of the component you use as dialog. For example, if a component has a prop with the name `title` we have to pass this `{ title: 'Some Title' }`. So these will be the initial props that the dialog component will receive.
138 |
139 | You can change props values during the calling `reveal` function by passing to its object with props data. So you can call the `reveal` function several times with different props. This is an excellent way to reuse the same dialog in different situations.
140 |
141 | And finally, you can pass data to emit functions inside your modal dialog component: `confirm` and `cancel`. Hooks `onConfirm` and `onCancel` will receive this data. Also, it will be passed by Promise, so you can use the async/await syntax if you prefer.
142 |
143 | The full example, that displays passing data, reusing, and modal chains:
144 |
145 | ```html
146 |
172 | ```
173 |
174 | ## Props Behavior Options
175 |
176 | Unfortunately, the way of passing props is not so clear to the developers. The problem occurs when you try to reuse a dialog instance already created with `createConfirmDialog`. See also the [issue](https://github.com/harmyderoman/vuejs-confirm-dialog/issues/21).
177 |
178 | Let's consider this process step by step. Props values are assigned three times, the first time is default props values, in the component, this plugin does not show up on them in any way. The second time occurs on creating an instance of the dialog, let's define these values as initial props. The third time passing values is possible when the user prompts the dialog using the `reveal` method. If you don't set the props behavior options, then each time you pass values, this data will be saved.
179 |
180 | For example, if you have an alert component and you called it with the message "Authorization failed!", then next time if you don't pass a new message value, it will show this message again.
181 |
182 | Perhaps it will be convenient if every time after closing the dialog, the values of the props will be reset to the initial or even default values. For this functionality, props transfer settings have been added.
183 |
184 | There are only two options:
185 |
186 | - `chore` - if `true` will tell to function reset values after closing dialog
187 | - `keepInitial` - if `true` reset props values to initial values, otherwise to default values of the component
188 |
189 | The simplest example:
190 |
191 | ```javascript
192 | const dialog = createConfirmDialog(
193 | ModalComponent,
194 | { message: 'Some message...' }, // Initial props values
195 | { chore: true, keepInitial: true }
196 | )
197 | ```
198 | ## Using inside Options API
199 |
200 | If you prefer you can use it with Options API inside methods.
201 |
202 | ```javascript
203 | import Dialog from './Dialog.vue'
204 | import { createConfirmDialog } from 'vuejs-confirm-dialog'
205 |
206 | export default {
207 | data(){
208 | return {
209 | isConfirmed: false
210 | }
211 | },
212 | methods: {
213 | showDialog(){
214 | const dialog = createConfirmDialog(Dialog)
215 |
216 | dialog.onConfirm(() => {
217 | this.isConfirmed = true
218 | })
219 |
220 | dialog.reveal()
221 | }
222 | }
223 | }
224 | ```
225 |
226 | For more info check this full Vue 3 [example](https://github.com/harmyderoman/vuejs-confirm-dialog/blob/main/demos/vue3).
227 |
228 | ## Close dialogs programmable
229 |
230 | Sometimes you need to close dialogs and don't want to wait for the user's action. For these purposes, dialog instance provides method `close`.
231 |
232 | ```javascript
233 | import Alert from './Alert.vue'
234 | import { createConfirmDialog } from 'vuejs-confirm-dialog'
235 |
236 | const dialog = createConfirmDialog(Alert)
237 |
238 | dialog.reveal()
239 |
240 | setTimeout(() => {
241 | dialog.close()
242 | }, 3000)
243 | ```
244 |
245 | It also doesn't trigger any hooks.
246 |
247 | If you need to close all dialog just call `dialog.closeAll()`.
248 |
249 |
250 | ## Demo
251 |
252 | Clone the project, install dependencies and run the following command to see the demo:
253 |
254 | ```bash
255 | pnpm run demo
256 | ```
257 |
258 | The demo is styled by beautiful [daisyUI](https://daisyui.com/).
259 |
260 | ## Roadmap
261 |
262 | * [x] Make it work!
263 |
264 | * [x] Make it work without `show` a prop
265 |
266 | * [x] TSDoc
267 |
268 | * [x] Change testing tools to Vitest
269 |
270 | * [x] Improve tests
271 |
272 | * [x] Improve docs( reuse, passing props ...)
273 |
274 | * [x] More examples
275 |
276 | ## Thanks
277 |
278 | Inspired by [`Vueuse`](https://github.com/vueuse/vueuse) and [`vue-modal-dialogs`](https://github.com/hjkcai/vue-modal-dialogs). Thanks to all creators of these projects ❤️!
279 |
280 | Keep calm and support :ukraine:!
281 |
--------------------------------------------------------------------------------
/demos/vue3/App.vue:
--------------------------------------------------------------------------------
1 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
30 | This package just makes it simple to create, reuse, promisify, and build
31 | chains of modal dialogs in Vue.js. For now, it provides just one function
32 | createConfirmDialog that does all the hard work for you.
33 |
34 |
35 |
Warning!
36 |
This package does not provide components for now. These examples are not production-ready components! Use it on your own risk!