Skip to content

Commit

Permalink
🐛 fix: optimize the resize observer to only notify when width or heig…
Browse files Browse the repository at this point in the history
…ht changes of the component (#1559)

fix: optimize the resize observer to only notify when width or height changes of the component
  • Loading branch information
hirsch88 authored Jan 7, 2025
1 parent b686745 commit 878e0a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/itchy-dogs-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baloise/ds-core': patch
---

**core**: optimize the resize observer to only notify when width or height changes of the component
11 changes: 10 additions & 1 deletion packages/core/src/utils/resize/resize.listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { BalResizeInfo } from './resize.interfaces'
export class BalResizeListener<TObserver> extends ListenerAbstract<TObserver, BalResizeInfo> {
private resizeObserver: ResizeObserver | undefined
private debouncedNotify = debounce(() => this.notify(), 10)
private lastWidth = 0
private lastHeight = 0

connect(el: HTMLElement): void {
super.connect(el)
Expand All @@ -23,7 +25,14 @@ export class BalResizeListener<TObserver> extends ListenerAbstract<TObserver, Ba
if (!Array.isArray(entries) || !entries.length) {
return
}
this.debouncedNotify()
const entry = entries[0]

// only notify if the width or the height of the component has changed
if (this.lastWidth !== entry.contentRect.width || this.lastHeight !== entry.contentRect.height) {
this.debouncedNotify()
this.lastWidth = entry.contentRect.width
this.lastHeight = entry.contentRect.height
}
})
}
})
Expand Down

0 comments on commit 878e0a7

Please sign in to comment.