Skip to content

Commit

Permalink
feat: emitOnMount prop, closes #196
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Dec 29, 2020
1 parent 8f9d800 commit 23302d6
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/components/ResizeObserver.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 23302d6

Please sign in to comment.