Skip to content

Commit 2529236

Browse files
committed
Added hideAll for dynamic modals
1 parent 50449c9 commit 2529236

File tree

8 files changed

+79
-42
lines changed

8 files changed

+79
-42
lines changed

CHANGELOG

+1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ modal transition weird stuff was happening)
1111
* Removed "delay" property - component is relying on modal & overlay transition durations
1212
* Added naive implementation of focus trap
1313
* Added source-maps
14+
* Added `hideAll` for dynamic modals
1415
* Fix: dialogs not working when componentName is changed
1516
* Fix: ActiveElement is blurred after before-open is fired - not it is possible to cache document.activeElement

demo/src/App.vue

+47-31
Original file line numberDiff line numberDiff line change
@@ -206,52 +206,68 @@ export default {
206206
this.$modal.show(
207207
{
208208
template: `
209-
<div class="example-modal-content">
210-
<h1>This is created inline</h1>
211-
<p>{{ text }}</p>
212-
<p>Default Property: {{ foo }}</p>
213-
</div>
214-
`,
215-
props: ['text', 'foo']
209+
<div class="example-modal-content">
210+
<h1>This is created inline</h1>
211+
<p>{{ text }}</p>
212+
<p>Default Property: {{ foo }}</p>
213+
</div>
214+
`,
215+
props: ['text', 'height']
216216
},
217217
{
218-
text: 'This text is passed as a property'
218+
text: 'This text is passed as a property',
219+
height: 'auto'
219220
}
220221
)
221222
},
222223
223224
showDynamicComponentModal() {
225+
console.log('Showing one more')
224226
this.$modal.show(DemoCustomComponent, {
225227
text: 'This text is passed as a property'
226228
})
227229
},
228230
229231
showDynamicComponentModalWithModalParams() {
230-
this.$modal.show(
231-
{
232-
template: `
233-
<div class="example-modal-content">
234-
<button class="btn" @click="closeByName">Close this using name</button>
235-
<button class="btn" @click="closeByEvent">Close this using events</button>
236-
</div>
237-
`,
238-
methods: {
239-
closeByName() {
240-
this.$modal.hide('dynamic-modal')
241-
},
242-
closeByEvent() {
243-
this.$emit('close')
232+
let counter = 0
233+
234+
const interval = setInterval(() => {
235+
if (counter === 5) {
236+
clearInterval(interval)
237+
} else {
238+
counter++
239+
}
240+
241+
const name = `dynamic-modal-${Date.now()}`
242+
243+
this.$modal.show(
244+
{
245+
template: `
246+
<div class="example-modal-content">
247+
<button class="btn" @click="closeByName">Close this using name</button>
248+
<button class="btn" @click="closeByEvent">Close this using events</button>
249+
<button class="btn" @click="this.$modal.hideAll">Close all dynamic modals</button>
250+
</div>
251+
`,
252+
methods: {
253+
closeByName() {
254+
this.$modal.hide(name)
255+
},
256+
closeByEvent() {
257+
this.$emit('close')
258+
}
244259
}
260+
},
261+
null,
262+
{
263+
height: 'auto',
264+
name: name,
265+
resizable: true,
266+
adaptive: true,
267+
draggable: true
245268
}
246-
},
247-
null,
248-
{
249-
name: 'dynamic-modal',
250-
resizable: true,
251-
adaptive: true,
252-
draggable: true
253-
}
254-
)
269+
)
270+
}, 2000)
255271
},
256272
257273
dialogEvent(eventName) {

dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ssr.index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ssr.nocss.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Dialog.vue

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<template>
2-
<component :is="$modal.context.componentName"
2+
<component
3+
:is="$modal.context.componentName"
34
name="dialog"
45
height="auto"
56
:classes="['v--modal', 'vue-dialog', this.params.class]"
@@ -11,21 +12,21 @@
1112
@before-open="beforeOpened"
1213
@before-close="beforeClosed"
1314
@opened="$emit('opened', $event)"
14-
@closed="$emit('closed', $event)">
15+
@closed="$emit('closed', $event)"
16+
>
1517
<div class="dialog-content">
1618
<div
1719
class="dialog-c-title"
1820
v-if="params.title"
19-
v-html="params.title || ''"></div>
21+
v-html="params.title || ''" />
2022
<component
2123
v-if="params.component"
2224
v-bind="params.props"
23-
:is="params.component">
24-
</component>
25+
:is="params.component" />
2526
<div
2627
class="dialog-c-text"
2728
v-else
28-
v-html="params.text || ''"></div>
29+
v-html="params.text || ''" />
2930
</div>
3031
<div
3132
class="vue-dialog-buttons"
@@ -37,11 +38,12 @@
3738
:style="buttonStyle"
3839
:key="i"
3940
v-html="button.title"
40-
@click.stop="click(i, $event)">
41+
@click.stop="click(i, $event)"
42+
>
4143
{{button.title}}
4244
</button>
4345
</div>
44-
<div v-else class="vue-dialog-buttons-none"></div>
46+
<div v-else class="vue-dialog-buttons-none" />
4547
</component>
4648
</template>
4749
<script>

src/ModalsContainer.vue

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</div>
1818
</template>
1919
<script>
20+
import Modal from './index'
2021
import { generateId } from './utils'
2122
2223
const PREFIX = '_dynamic_modal_'
@@ -30,6 +31,11 @@ export default {
3031
created() {
3132
this.$root._dynamicContainer = this
3233
},
34+
mounted() {
35+
Modal.event.$on('hide-all', () => {
36+
this.modals = []
37+
})
38+
},
3339
methods: {
3440
add(component, componentAttrs = {}, modalAttrs = {}, modalListeners = {}) {
3541
const id = generateId()

src/index.js

+12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const UNSUPPORTED_ARGUMENT_ERROR =
1919
'[vue-js-modal] ' +
2020
'$modal() received an unsupported argument as a first argument.'
2121

22+
const HIDE_ALL_RESTRICTION_ERROR =
23+
'[vue-js-modal] ' +
24+
'$modal.hideAll() call will be ignored because dynamic modals are not enabled.'
25+
2226
export const getModalsContainer = (Vue, options, root) => {
2327
if (!root._dynamicContainer && options.injectModalsContainer) {
2428
const container = createDivInBody()
@@ -101,6 +105,14 @@ const Plugin = {
101105
Plugin.event.$emit('toggle', name, false, params)
102106
},
103107

108+
hideAll() {
109+
if (options.dynamic) {
110+
Plugin.event.$emit('hide-all')
111+
} else {
112+
console.warn(HIDE_ALL_RESTRICTION_ERROR)
113+
}
114+
},
115+
104116
toggle(name, params) {
105117
Plugin.event.$emit('toggle', name, undefined, params)
106118
}

0 commit comments

Comments
 (0)