-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
1 parent
82881ab
commit ba7362e
Showing
11 changed files
with
129 additions
and
23 deletions.
There are no files selected for viewing
10 changes: 6 additions & 4 deletions
10
demo/src/app/components/alert/demos/selfclosing/alert-selfclosing.html
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 |
---|---|---|
@@ -1,14 +1,16 @@ | ||
<p> | ||
Static self-closing alert that disappears after 20 seconds (refresh the page if it has already disappeared) | ||
</p> | ||
<ngb-alert *ngIf="!staticAlertClosed" (close)="staticAlertClosed = true">Check out our awesome new features!</ngb-alert> | ||
<ngb-alert #staticAlert *ngIf="!staticAlertClosed" (close)="staticAlertClosed = true">Check out our awesome new | ||
features!</ngb-alert> | ||
|
||
<hr/> | ||
<hr /> | ||
|
||
<p> | ||
Show a self-closing success message that disappears after 5 seconds. | ||
</p> | ||
<ngb-alert *ngIf="successMessage" type="success" (close)="successMessage = ''">{{ successMessage }}</ngb-alert> | ||
<ngb-alert #selfClosingAlert *ngIf="successMessage" type="success" (close)="successMessage = ''">{{ successMessage }} | ||
</ngb-alert> | ||
<p> | ||
<button class="btn btn-primary" (click)="changeSuccessMessage()">Change message</button> | ||
</p> | ||
</p> |
25 changes: 13 additions & 12 deletions
25
demo/src/app/components/alert/demos/selfclosing/alert-selfclosing.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 |
---|---|---|
@@ -1,27 +1,28 @@ | ||
import {Component, OnInit} from '@angular/core'; | ||
import {Component, OnInit, ViewChild} from '@angular/core'; | ||
import {Subject} from 'rxjs'; | ||
import {debounceTime} from 'rxjs/operators'; | ||
import {NgbAlert} from '@ng-bootstrap/ng-bootstrap'; | ||
|
||
@Component({ | ||
selector: 'ngbd-alert-selfclosing', | ||
templateUrl: './alert-selfclosing.html' | ||
}) | ||
@Component({selector: 'ngbd-alert-selfclosing', templateUrl: './alert-selfclosing.html'}) | ||
export class NgbdAlertSelfclosing implements OnInit { | ||
private _success = new Subject<string>(); | ||
|
||
staticAlertClosed = false; | ||
successMessage = ''; | ||
|
||
@ViewChild('staticAlert', {static: false}) staticAlert: NgbAlert; | ||
@ViewChild('selfClosingAlert', {static: false}) selfClosingAlert: NgbAlert; | ||
|
||
ngOnInit(): void { | ||
setTimeout(() => this.staticAlertClosed = true, 20000); | ||
setTimeout(() => this.staticAlert.close(), 20000); | ||
|
||
this._success.subscribe(message => this.successMessage = message); | ||
this._success.pipe( | ||
debounceTime(5000) | ||
).subscribe(() => this.successMessage = ''); | ||
this._success.pipe(debounceTime(5000)).subscribe(() => { | ||
if (this.selfClosingAlert) { | ||
this.selfClosingAlert.close(); | ||
} | ||
}); | ||
} | ||
|
||
public changeSuccessMessage() { | ||
this._success.next(`${new Date()} - Message successfully changed.`); | ||
} | ||
public changeSuccessMessage() { this._success.next(`${new Date()} - Message successfully changed.`); } | ||
} |
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const environment = { | ||
animation: false, | ||
transitionTimerDelayMs: 500, | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const environment = { | ||
animation: true, | ||
transitionTimerDelayMs: 5, | ||
}; |
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,3 @@ | ||
export const ngbAlertFadingTransition = ({classList}: HTMLElement): void => { | ||
classList.remove('show'); | ||
}; |
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,65 @@ | ||
import {EMPTY, fromEvent, Observable, of, race, Subject, timer} from 'rxjs'; | ||
import {takeUntil} from 'rxjs/operators'; | ||
import {getTransitionDurationMs} from './util'; | ||
import {environment} from '../../environment'; | ||
|
||
export type NgbTransitionStartFn = (element: HTMLElement) => void; | ||
|
||
export interface NgbTransitionOptions { | ||
animation: boolean; | ||
runningTransition: 'continue' | 'stop'; | ||
} | ||
|
||
const {transitionTimerDelayMs} = environment; | ||
const runningTransitions = new Map<HTMLElement, Subject<any>>(); | ||
|
||
export const ngbRunTransition = | ||
(element: HTMLElement, startFn: NgbTransitionStartFn, options: NgbTransitionOptions): Observable<undefined> => { | ||
|
||
// If animations are disabled, we have to emit a value and complete the observable | ||
if (!options.animation) { | ||
return of(undefined); | ||
} | ||
|
||
// Checking if there are already running transitions on the given element. | ||
const runningTransition$ = runningTransitions.get(element); | ||
if (runningTransition$) { | ||
// If there is one running and we want for it to continue to run, we have to cancel the current one. | ||
// We're not emitting any values, but simply completing the observable (EMPTY). | ||
if (options.runningTransition === 'continue') { | ||
return EMPTY; | ||
} | ||
} | ||
|
||
// If 'prefer-reduced-motion' is enabled, the 'transition' will be set to 'none'. | ||
// In this case we have to call the start function, but can finish immediately by emitting a value | ||
// and completing the observable. | ||
const {transitionProperty} = window.getComputedStyle(element); | ||
if (transitionProperty === 'none') { | ||
startFn(element); | ||
return of(undefined); | ||
} | ||
|
||
// Starting a new transition | ||
const transition$ = new Subject<any>(); | ||
runningTransitions.set(element, transition$); | ||
|
||
const transitionDurationMs = getTransitionDurationMs(element); | ||
|
||
startFn(element); | ||
|
||
// We have to both listen for the 'transitionend' event and have a 'just-in-case' timer, | ||
// because 'transitionend' event might not be fired in some browsers, if the transitioning | ||
// element becomes invisible (ex. when scrolling, making browser tab inactive, etc.). The timer | ||
// guarantees, that we'll release the DOM element and complete 'ngbRunTransition'. | ||
const transitionEnd$ = fromEvent(element, 'transitionend').pipe(takeUntil(transition$)); | ||
const timer$ = timer(transitionDurationMs + transitionTimerDelayMs).pipe(takeUntil(transition$)); | ||
|
||
race(timer$, transitionEnd$).subscribe(() => { | ||
runningTransitions.delete(element); | ||
transition$.next(); | ||
transition$.complete(); | ||
}); | ||
|
||
return transition$.asObservable(); | ||
}; |
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,7 @@ | ||
export function getTransitionDurationMs(element: HTMLElement) { | ||
const {transitionDelay, transitionDuration} = window.getComputedStyle(element); | ||
const transitionDelaySec = parseFloat(transitionDelay); | ||
const transitionDurationSec = parseFloat(transitionDuration); | ||
|
||
return (transitionDelaySec + transitionDurationSec) * 1000; | ||
} |