Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
fix: use component lifecycle hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
MitanOmar committed Mar 22, 2023
1 parent bd78911 commit 4229bb8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
11 changes: 5 additions & 6 deletions app/components/in-viewport/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@ import { tracked } from "@glimmer/tracking";
export default class InViewport extends Component {
@tracked rootSelector = "body";
@tracked rootMargin = 0;
@tracked onEnterViewport = this.args["on-enter-viewport"];
@tracked onExitViewport = this.args["on-exit-viewport"];
_observer = null;

@action
registerObserver(element) {
const observer = new IntersectionObserver(
([{ isIntersecting }]) => {
if (isIntersecting) {
return (this.onEnterViewport ?? (() => {}))();
return (this.args["on-enter-viewport"] ?? (() => {}))();
}

return (this.onExitViewport ?? (() => {}))();
return (this.args["on-exit-viewport"] ?? (() => {}))();
},
{
root: document.querySelector(this.rootSelector),
Expand All @@ -32,7 +30,8 @@ export default class InViewport extends Component {
}

@action
disconnectObserver() {
this._observer.disconnect();
willDestroy(...args) {
super.willDestroy(...args);
this._observer?.disconnect();
}
}
3 changes: 1 addition & 2 deletions app/components/in-viewport/template.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<div
{{did-insert this.registerObserver}}
{{will-destroy this.disconnectObserver}}>{{yield}}</div>
{{did-insert this.registerObserver}}>{{yield}}</div>

0 comments on commit 4229bb8

Please sign in to comment.