-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
126 additions
and
28 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
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,2 @@ | ||
export * from './visibility.interfaces' | ||
export * from './visibility.decorator' |
36 changes: 36 additions & 0 deletions
36
packages/core/src/utils/visibility/visibility.decorator.ts
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,36 @@ | ||
import { ComponentInterface } from '@stencil/core' | ||
import { BalVisibilityObserver } from './visibility.interfaces' | ||
import { BalVisibilitySubject } from './visibility.subject' | ||
|
||
export function ListenToVisibility() { | ||
return function ( | ||
target: ComponentInterface & BalVisibilityObserver, | ||
_propertyKey: string, | ||
_descriptor: PropertyDescriptor, | ||
) { | ||
const { connectedCallback, componentDidLoad, disconnectedCallback } = target | ||
|
||
target.connectedCallback = function () { | ||
if (!this._balVisibilitySubject) { | ||
this._balVisibilitySubject = new BalVisibilitySubject() | ||
} | ||
|
||
return connectedCallback && connectedCallback.call(this) | ||
} | ||
|
||
target.componentDidLoad = function () { | ||
this._balVisibilitySubject.attach(this) | ||
|
||
return componentDidLoad && componentDidLoad.call(this) | ||
} | ||
|
||
target.disconnectedCallback = function () { | ||
if (this._balVisibilitySubject) { | ||
this._balVisibilitySubject.detach() | ||
this._balVisibilitySubject = undefined | ||
} | ||
|
||
return disconnectedCallback && disconnectedCallback.call(this) | ||
} | ||
} | ||
} |
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,4 @@ | ||
export interface BalVisibilityObserver { | ||
el: HTMLElement | ||
visibilityListener(): void | ||
} |
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,43 @@ | ||
import { deepReady, waitAfterFramePaint } from '../helpers' | ||
import { ListenerAbstract } from '../types/listener' | ||
|
||
export class BalVisibilityListener extends ListenerAbstract { | ||
private waitAfterFramePrint = false | ||
private intersectionObserver: IntersectionObserver | undefined = undefined | ||
|
||
async connect(el: HTMLElement) { | ||
super.connect(el) | ||
if (typeof IntersectionObserver === 'undefined') { | ||
return | ||
} | ||
if (this.waitAfterFramePrint) { | ||
await deepReady(el) | ||
await waitAfterFramePaint() | ||
} | ||
this.destroyMutationObserver() | ||
this.intersectionObserver = new IntersectionObserver(this.intersectionCallback, { | ||
root: null, // Use the viewport as the container | ||
threshold: 0.5, // Trigger when 50% of the element is visible | ||
}) | ||
this.intersectionObserver.observe(el) | ||
} | ||
|
||
disconnect(): void { | ||
super.disconnect() | ||
this.destroyMutationObserver() | ||
} | ||
|
||
private intersectionCallback = (records: IntersectionObserverEntry[]) => { | ||
const isIntersecting = records.some(record => record.isIntersecting) | ||
if (isIntersecting) { | ||
this.notify() | ||
} | ||
} | ||
|
||
private destroyMutationObserver() { | ||
if (this.intersectionObserver !== undefined) { | ||
this.intersectionObserver.disconnect() | ||
this.intersectionObserver = undefined | ||
} | ||
} | ||
} |
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,27 @@ | ||
import { debounce } from '../helpers' | ||
import { SingleSubject } from '../types/signal' | ||
import { BalVisibilityObserver } from './visibility.interfaces' | ||
import { BalVisibilityListener } from './visibility.listener' | ||
|
||
export class BalVisibilitySubject extends SingleSubject<BalVisibilityObserver> { | ||
private listener?: BalVisibilityListener | ||
private debouncedNotify = debounce(() => this.notify(), 50) | ||
|
||
constructor() { | ||
super(observer => { | ||
observer.visibilityListener() | ||
}) | ||
this.listener = new BalVisibilityListener() | ||
} | ||
|
||
override attach(observer: BalVisibilityObserver): void { | ||
super.attach(observer) | ||
this.listener?.connect(observer.el) | ||
this.listener?.add(() => this.debouncedNotify()) | ||
} | ||
|
||
override detach(): void { | ||
super.detach() | ||
this.listener?.disconnect() | ||
} | ||
} |