From 23302d6ae23f3d85abf5e8dc5c6c034a84c6cceb Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Tue, 29 Dec 2020 19:07:55 +0100 Subject: [PATCH] feat: emitOnMount prop, closes #196 --- src/components/ResizeObserver.vue | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/components/ResizeObserver.vue b/src/components/ResizeObserver.vue index 80fc9ae..9f3abca 100644 --- a/src/components/ResizeObserver.vue +++ b/src/components/ResizeObserver.vue @@ -20,11 +20,21 @@ function initCompat () { export default { name: 'ResizeObserver', + props: { + emitOnMount: { + type: Boolean, + default: false, + }, + }, + mounted () { initCompat() this.$nextTick(() => { this._w = this.$el.offsetWidth this._h = this.$el.offsetHeight + if (this.emitOnMount) { + this.emitSize() + } }) const object = document.createElement('object') this._resizeObject = object @@ -50,13 +60,17 @@ export default { if (this._w !== this.$el.offsetWidth || this._h !== this.$el.offsetHeight) { this._w = this.$el.offsetWidth this._h = this.$el.offsetHeight - this.$emit('notify', { - width: this._w, - height: this._h, - }) + this.emitSize() } }, + emitSize () { + this.$emit('notify', { + width: this._w, + height: this._h, + }) + }, + addResizeHandlers () { this._resizeObject.contentDocument.defaultView.addEventListener('resize', this.compareAndNotify) this.compareAndNotify()