Skip to content

Commit

Permalink
Avoid multiple event listeners and fix IE minimizing bug (#299) (@Vic…
Browse files Browse the repository at this point in the history
…torCazanave)

* Use named toggle events to avoid multiple listeners

* Prevent IE bug when body is active element
  • Loading branch information
VictorCazanave authored and euvl committed Aug 27, 2018
1 parent 13499d3 commit 51501b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/Modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export default {
default: false
},
overlayTransition: {
type: String,
default: 'overlay-fade'
type: String,
default: 'overlay-fade'
},
transition: {
type: String
Expand Down Expand Up @@ -199,14 +199,12 @@ export default {
* Sets global listeners
*/
beforeMount () {
Modal.event.$on('toggle', (name, state, params) => {
if (name === this.name) {
if (typeof state === 'undefined') {
state = !this.visible
}
this.toggle(state, params)
Modal.event.$on(`toggle-${this.name}`, (state, params) => {
if (typeof state === 'undefined') {
state = !this.visible
}
this.toggle(state, params)
})
window.addEventListener('resize', this.onWindowResize)
Expand Down Expand Up @@ -258,9 +256,10 @@ export default {
}
},
/**
* Removes "resize" window listener
* Removes global listeners
*/
beforeDestroy () {
Modal.event.$off(`toggle-${this.name}`)
window.removeEventListener('resize', this.onWindowResize)
if (this.clickToClose) {
Expand Down Expand Up @@ -446,7 +445,9 @@ export default {
* Need to unfocus previously focused element, otherwise
* all keypress events (ESC press, for example) will trigger on that element.
*/
if (document.activeElement && typeof document.activeElement.blur === 'function') {
if (document.activeElement &&
document.activeElement.tagName.toLowerCase() !== 'body' &&
typeof document.activeElement.blur === 'function') {
document.activeElement.blur()
}
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Plugin = {
Vue.prototype.$modal = {
show (modal, paramsOrProps, params, events = {}) {
if (typeof modal === 'string') {
Plugin.event.$emit('toggle', modal, true, paramsOrProps)
Plugin.event.$emit(`toggle-${modal}`, true, paramsOrProps)
} else {
let root = Plugin.rootInstance
if (params && params.root) {
Expand All @@ -39,11 +39,11 @@ const Plugin = {
}
},
hide (name, params) {
Plugin.event.$emit('toggle', name, false, params)
Plugin.event.$emit(`toggle-${name}`, false, params)
},

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

0 comments on commit 51501b1

Please sign in to comment.