Skip to content

Commit

Permalink
Added hideAll for dynamic modals
Browse files Browse the repository at this point in the history
  • Loading branch information
euvl committed Apr 26, 2020
1 parent 50449c9 commit 2529236
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ modal transition weird stuff was happening)
* Removed "delay" property - component is relying on modal & overlay transition durations
* Added naive implementation of focus trap
* Added source-maps
* Added `hideAll` for dynamic modals
* Fix: dialogs not working when componentName is changed
* Fix: ActiveElement is blurred after before-open is fired - not it is possible to cache document.activeElement
78 changes: 47 additions & 31 deletions demo/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -206,52 +206,68 @@ export default {
this.$modal.show(
{
template: `
<div class="example-modal-content">
<h1>This is created inline</h1>
<p>{{ text }}</p>
<p>Default Property: {{ foo }}</p>
</div>
`,
props: ['text', 'foo']
<div class="example-modal-content">
<h1>This is created inline</h1>
<p>{{ text }}</p>
<p>Default Property: {{ foo }}</p>
</div>
`,
props: ['text', 'height']
},
{
text: 'This text is passed as a property'
text: 'This text is passed as a property',
height: 'auto'
}
)
},
showDynamicComponentModal() {
console.log('Showing one more')
this.$modal.show(DemoCustomComponent, {
text: 'This text is passed as a property'
})
},
showDynamicComponentModalWithModalParams() {
this.$modal.show(
{
template: `
<div class="example-modal-content">
<button class="btn" @click="closeByName">Close this using name</button>
<button class="btn" @click="closeByEvent">Close this using events</button>
</div>
`,
methods: {
closeByName() {
this.$modal.hide('dynamic-modal')
},
closeByEvent() {
this.$emit('close')
let counter = 0
const interval = setInterval(() => {
if (counter === 5) {
clearInterval(interval)
} else {
counter++
}
const name = `dynamic-modal-${Date.now()}`
this.$modal.show(
{
template: `
<div class="example-modal-content">
<button class="btn" @click="closeByName">Close this using name</button>
<button class="btn" @click="closeByEvent">Close this using events</button>
<button class="btn" @click="this.$modal.hideAll">Close all dynamic modals</button>
</div>
`,
methods: {
closeByName() {
this.$modal.hide(name)
},
closeByEvent() {
this.$emit('close')
}
}
},
null,
{
height: 'auto',
name: name,
resizable: true,
adaptive: true,
draggable: true
}
},
null,
{
name: 'dynamic-modal',
resizable: true,
adaptive: true,
draggable: true
}
)
)
}, 2000)
},
dialogEvent(eventName) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ssr.index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ssr.nocss.js

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions src/Dialog.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<template>
<component :is="$modal.context.componentName"
<component
:is="$modal.context.componentName"
name="dialog"
height="auto"
:classes="['v--modal', 'vue-dialog', this.params.class]"
Expand All @@ -11,21 +12,21 @@
@before-open="beforeOpened"
@before-close="beforeClosed"
@opened="$emit('opened', $event)"
@closed="$emit('closed', $event)">
@closed="$emit('closed', $event)"
>
<div class="dialog-content">
<div
class="dialog-c-title"
v-if="params.title"
v-html="params.title || ''"></div>
v-html="params.title || ''" />
<component
v-if="params.component"
v-bind="params.props"
:is="params.component">
</component>
:is="params.component" />
<div
class="dialog-c-text"
v-else
v-html="params.text || ''"></div>
v-html="params.text || ''" />
</div>
<div
class="vue-dialog-buttons"
Expand All @@ -37,11 +38,12 @@
:style="buttonStyle"
:key="i"
v-html="button.title"
@click.stop="click(i, $event)">
@click.stop="click(i, $event)"
>
{{button.title}}
</button>
</div>
<div v-else class="vue-dialog-buttons-none"></div>
<div v-else class="vue-dialog-buttons-none" />
</component>
</template>
<script>
Expand Down
6 changes: 6 additions & 0 deletions src/ModalsContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</div>
</template>
<script>
import Modal from './index'
import { generateId } from './utils'
const PREFIX = '_dynamic_modal_'
Expand All @@ -30,6 +31,11 @@ export default {
created() {
this.$root._dynamicContainer = this
},
mounted() {
Modal.event.$on('hide-all', () => {
this.modals = []
})
},
methods: {
add(component, componentAttrs = {}, modalAttrs = {}, modalListeners = {}) {
const id = generateId()
Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const UNSUPPORTED_ARGUMENT_ERROR =
'[vue-js-modal] ' +
'$modal() received an unsupported argument as a first argument.'

const HIDE_ALL_RESTRICTION_ERROR =
'[vue-js-modal] ' +
'$modal.hideAll() call will be ignored because dynamic modals are not enabled.'

export const getModalsContainer = (Vue, options, root) => {
if (!root._dynamicContainer && options.injectModalsContainer) {
const container = createDivInBody()
Expand Down Expand Up @@ -101,6 +105,14 @@ const Plugin = {
Plugin.event.$emit('toggle', name, false, params)
},

hideAll() {
if (options.dynamic) {
Plugin.event.$emit('hide-all')
} else {
console.warn(HIDE_ALL_RESTRICTION_ERROR)
}
},

toggle(name, params) {
Plugin.event.$emit('toggle', name, undefined, params)
}
Expand Down

0 comments on commit 2529236

Please sign in to comment.