-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add useResizeObserver option (#55)
* feat: add useResizeObserver option * test: test useResizeObserver * fix: fix typescript version * skip: apply review
- Loading branch information
Showing
9 changed files
with
225 additions
and
65 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import Component from "@egjs/component"; | ||
import { SizeRect } from "./types"; | ||
import { isString } from "./utils"; | ||
|
||
export interface ResizeWatherOptions { | ||
resizeDebounce?: number; | ||
maxResizeDebounce?: number; | ||
useResizeObserver?: boolean; | ||
useWindowResize?: boolean; | ||
watchDirection?: "width" | "height" | "box" | false; | ||
rectBox?: "border-box" | "content-box"; | ||
} | ||
|
||
export class ResizeWatcher { | ||
private _resizeTimer = 0; | ||
private _maxResizeDebounceTimer = 0; | ||
private _emitter: Component<{ resize: void }>; | ||
private _observer: ResizeObserver | null; | ||
protected container: HTMLElement; | ||
protected rect: SizeRect = { width: 0, height: 0 }; | ||
private _options!: Required<ResizeWatherOptions>; | ||
|
||
constructor(container: HTMLElement | string, options: ResizeWatherOptions = {}) { | ||
this._options = { | ||
resizeDebounce: 100, | ||
maxResizeDebounce: 0, | ||
useResizeObserver: false, | ||
useWindowResize: true, | ||
watchDirection: false, | ||
rectBox: "content-box", | ||
...options, | ||
}; | ||
|
||
this.container = isString(container) ? document.querySelector<HTMLElement>(container)! : container; | ||
this._init(); | ||
} | ||
public getRect() { | ||
return this.rect; | ||
} | ||
public setRect(rect: SizeRect) { | ||
this.rect = { ...rect }; | ||
} | ||
public resize() { | ||
const container = this.container; | ||
|
||
this.setRect(this._options.rectBox === "border-box" ? { | ||
width: container.offsetWidth, | ||
height: container.offsetHeight, | ||
} : { | ||
width: container.clientWidth, | ||
height: container.clientHeight, | ||
}); | ||
} | ||
public listen(callback: () => void) { | ||
this._emitter.on("resize", callback); | ||
return this; | ||
} | ||
public destroy() { | ||
this._observer?.disconnect(); | ||
if (this._options.useWindowResize) { | ||
window.removeEventListener("reisze", this._onResize); | ||
} | ||
} | ||
private _init() { | ||
const container = this.container; | ||
const options = this._options; | ||
|
||
this._emitter = new Component(); | ||
if (options.useResizeObserver && !!window.ResizeObserver) { | ||
this._observer = new window.ResizeObserver(this._scheduleResize); | ||
this._observer.observe(container, { | ||
box: options.rectBox, | ||
}); | ||
} | ||
if (options.useWindowResize) { | ||
window.addEventListener("resize", this._scheduleResize); | ||
} | ||
this.resize(); | ||
} | ||
private _onResize = () => { | ||
clearTimeout(this._resizeTimer); | ||
clearTimeout(this._maxResizeDebounceTimer); | ||
|
||
this._maxResizeDebounceTimer = 0; | ||
this._resizeTimer = 0; | ||
|
||
const watchDirection = this._options.watchDirection; | ||
const prevRect = this.rect; | ||
this.resize(); | ||
const rect = this.rect; | ||
const isWatchWidth = watchDirection === "box" || watchDirection === "width"; | ||
const isWatchHeight = watchDirection === "box" || watchDirection === "height"; | ||
const isResize = !watchDirection | ||
|| (isWatchWidth && prevRect.width !== rect.width) | ||
|| (isWatchHeight && prevRect.height !== rect.height); | ||
|
||
if (isResize) { | ||
this._emitter.trigger("resize"); | ||
} | ||
} | ||
private _scheduleResize = () => { | ||
const { | ||
resizeDebounce, | ||
maxResizeDebounce, | ||
} = this._options; | ||
|
||
|
||
if (!this._maxResizeDebounceTimer && maxResizeDebounce >= resizeDebounce) { | ||
this._maxResizeDebounceTimer = window.setTimeout(this._onResize, maxResizeDebounce); | ||
} | ||
if (this._resizeTimer) { | ||
clearTimeout(this._resizeTimer); | ||
this._resizeTimer = 0; | ||
} | ||
this._resizeTimer = window.setTimeout(this._onResize, resizeDebounce); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.