Skip to content

Instantly share code, notes, and snippets.

@tanishqmanuja
Created September 7, 2024 10:08
Show Gist options
  • Save tanishqmanuja/f59e1d5236e93e29024ec630f3dc262e to your computer and use it in GitHub Desktop.
Save tanishqmanuja/f59e1d5236e93e29024ec630f3dc262e to your computer and use it in GitHub Desktop.
Network Indicator using @ngxtension
import { AsyncPipe } from "@angular/common";
import { Component } from "@angular/core";
import { takeUntilDestroyed, toObservable } from "@angular/core/rxjs-interop";
import { injectNetwork } from "ngxtension/inject-network";
import {
distinctUntilChanged,
map,
skipWhile,
startWith,
switchMap,
timer,
} from "rxjs";
const INDICATOR_TIMEOUT_MS = 2 * 1000;
const INDICATOR_TRANSITION_MS = 300;
@Component({
standalone: true,
selector: "app-network-indicator",
imports: [AsyncPipe],
template: `
@let isOnline = isOnline$ | async;
<div
class="indicator"
[class.visible]="isVisible$ | async"
[style.background]="isOnline ? 'green' : 'red'"
>
{{ isOnline ? "Online" : "Offline" }}
</div>
`,
styles: `
.indicator {
text-align: center;
display: none;
opacity: 0;
transition: opacity ${INDICATOR_TRANSITION_MS}ms ease,
display ${INDICATOR_TRANSITION_MS}ms ease allow-discrete;
&.visible {
opacity: 1;
display: block;
}
}
@starting-style {
.indicator.visible {
opacity: 0;
}
}
`,
})
export class NetworkIndicatorComponent {
private readonly network = injectNetwork();
protected readonly isOnline$ = toObservable(this.network.online).pipe(
skipWhile(Boolean),
distinctUntilChanged(),
takeUntilDestroyed()
);
protected readonly isVisible$ = this.isOnline$.pipe(
switchMap(() =>
timer(INDICATOR_TIMEOUT_MS).pipe(
map(() => false),
startWith(true)
)
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment