Skip to content

Commit

Permalink
fix bug that causes memory leak during SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvasilchuk committed Nov 16, 2019
1 parent 604e5a1 commit 8558bca
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 52 deletions.
7 changes: 4 additions & 3 deletions demo/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<script>
import ExampleComponent from './components/ExampleComponent.vue'
import ConfirmComponent from './components/ConfirmComponent.vue'
export default {
name: 'App',
Expand All @@ -30,12 +31,12 @@ export default {
attributes: {
id: 'bar',
},
transition: 'fade',
// transition: 'fade',
})
},
openConfirmModal() {
this.$modal
.confirm('Do you like JavaScript?')
.confirm('Do you like JavaScript?', { component: ConfirmComponent })
.then(val => {
console.log(val)
})
Expand Down Expand Up @@ -78,4 +79,4 @@ export default {
button:focus {
background: red;
}
</style>
</style>
2 changes: 0 additions & 2 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import Vue from 'vue'
import App from './App.vue'
import VueAccessibleModal from '../src'
import ConfirmComponent from './components/ConfirmComponent.vue'

Vue.config.productionTip = false

Vue.use(VueAccessibleModal, {
confirmComponent: ConfirmComponent,
transition: 'fade',
})

Expand Down
10 changes: 5 additions & 5 deletions src/components/VueAccessibleModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
</template>

<script>
import Plugin from '../'
import focusableElements from '../utils/focusable-elements'
import { Show, Close } from '../events'
export default {
name: 'VueAccessibleModal',
Expand All @@ -50,8 +50,8 @@ export default {
},
computed: {
transition() {
const { options } = this
return options.transition
const { options, $_accessibleModalOptions } = this
return options.transition || $_accessibleModalOptions.transition
},
className() {
const { options, localClasses } = this
Expand Down Expand Up @@ -94,8 +94,8 @@ export default {
},
},
created() {
Plugin.event.$on('show', this.show)
Plugin.event.$on('close', this.close)
this.$root.$on(Show, this.show)
this.$root.$on(Close, this.close)
},
methods: {
show(component, options) {
Expand Down
2 changes: 2 additions & 0 deletions src/events/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const Show = 'vue-accessible-modal:show'
export const Close = 'vue-accessible-modal:close'
84 changes: 42 additions & 42 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
import VueAccessibleModal from './components/VueAccessibleModal.vue'
import VueAccessibleModalComponent from './components/VueAccessibleModal.vue'
import { Show, Close } from './events'

const Plugin = {
install(Vue, options) {
if (this.installed) {
return
}
class VueAccessibleModal {
constructor(vm) {
this._root = vm.$root
}

this.installed = true
this.event = new Vue()
this.confirmComponent = options.confirmComponent
this.transition = options.transition
show(modal, options = {}) {
this._root.$emit(Show, modal, options)
}

Vue.prototype.$modal = {
show(modal, options = {}) {
Plugin.event.$emit('show', modal, Plugin._getOptions(modal, options))
},
close() {
Plugin.event.$emit('close')
},
confirm(message, options = {}) {
// if component is not passed pass the default
if (!options.component) {
options.component = Plugin.confirmComponent
}

return new Promise((resolve, reject) => {
Plugin.event.$emit(
'show',
options.component,
Object.assign({}, Plugin._getOptions(options.component, options), {
props: { message, resolve, reject },
})
)
close() {
this._root.$emit(Close)
}

confirm(message, options = {}) {
return new Promise((resolve, reject) => {
this._root.$emit(
Show,
options.component,
Object.assign({}, options, {
props: { message, resolve, reject },
})
)
})
}
}

/**
* Plugin install function.
* @param {Function} Vue - the Vue constructor.
*/
const Plugin = {
install(Vue, options) {
if (Vue.$_accessibleModalInstalled) return
Vue.$_accessibleModalInstalled = true

Vue.mixin({
beforeCreate() {
this.$modal = new VueAccessibleModal(this, options)
},
}
Vue.component('VueAccessibleModal', VueAccessibleModal)
},
_getOptions(component = {}, options = {}) {
if (!options.transition) {
const hasModalTransition = component.modal && component.modal.transition
})

if (!hasModalTransition) {
options.transition = this.transition
}
}
Vue.component('VueAccessibleModal', VueAccessibleModalComponent)

return options
Vue.prototype.$_accessibleModalOptions = {
transition: null || options.transition,
}
},
}

Expand Down

0 comments on commit 8558bca

Please sign in to comment.