-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix node bound displacement * Finalized fix by replacing rect observer with polling and fixing incorrect mouseHandler validity * Carry over fixes * Fix disconnect removeEventListener
- Loading branch information
Manfred Cheung
authored
Jan 19, 2021
1 parent
c15d9c3
commit 7e4465a
Showing
3 changed files
with
64 additions
and
19 deletions.
There are no files selected for viewing
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,54 @@ | ||
export type RectObserverCallback = (rect: DOMRectReadOnly) => void; | ||
|
||
const POLLING_RATE = 400; // ms | ||
|
||
export default class RectObserver { | ||
public elementTarget: HTMLElement; | ||
public callback: RectObserverCallback; | ||
public rect: DOMRectReadOnly; | ||
private poll: ReturnType<typeof setInterval>; | ||
|
||
constructor(callback: RectObserverCallback) { | ||
this.callback = callback; | ||
} | ||
|
||
public observe(element: HTMLElement): void { | ||
this.elementTarget = element; | ||
|
||
this.elementTarget.addEventListener("mouseenter", this.handleMouseEnter.bind(this), false); | ||
this.elementTarget.addEventListener("mouseleave", this.handleMouseLeave.bind(this), false); | ||
|
||
this.rect = this.elementTarget.getBoundingClientRect(); | ||
} | ||
|
||
public disconnect(): void { | ||
clearInterval(this.poll); | ||
this.elementTarget.removeEventListener("mouseenter", this.handleMouseEnter.bind(this), false); | ||
this.elementTarget.removeEventListener("mouseleave", this.handleMouseLeave.bind(this), false); | ||
} | ||
|
||
private handleMouseEnter(): void { | ||
this.pollElement(); | ||
this.poll = setInterval(this.pollElement.bind(this), POLLING_RATE); | ||
} | ||
|
||
private pollElement(): void { | ||
const rect = this.elementTarget.getBoundingClientRect(); | ||
if(!this.rectEqual(this.rect, rect)) { | ||
this.rect = rect; | ||
this.callback(this.rect); | ||
} | ||
} | ||
|
||
private handleMouseLeave(): void { | ||
this.pollElement(); | ||
clearInterval(this.poll); | ||
} | ||
|
||
public rectEqual(prev: DOMRectReadOnly, curr: DOMRectReadOnly): boolean { | ||
return prev.width === curr.width && | ||
prev.height === curr.height && | ||
prev.top === curr.top && | ||
prev.left === curr.left; | ||
} | ||
} |
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