-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Expand file tree
/
Copy pathsnack-bar-ref.ts
More file actions
131 lines (109 loc) · 3.99 KB
/
snack-bar-ref.ts
File metadata and controls
131 lines (109 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {OverlayRef} from '@angular/cdk/overlay';
import {Observable, Subject} from 'rxjs';
import {MatSnackBarContainer} from './snack-bar-container';
/** Event that is emitted when a snack bar is dismissed. */
export interface MatSnackBarDismiss {
/** Whether the snack bar was dismissed using the action button. */
dismissedByAction: boolean;
}
/** Maximum amount of milliseconds that can be passed into setTimeout. */
const MAX_TIMEOUT = Math.pow(2, 31) - 1;
/**
* Reference to a snack bar dispatched from the snack bar service.
*/
export class MatSnackBarRef<T, D = unknown> {
/** The instance of the component making up the content of the snack bar. */
instance!: T;
/**
* The instance of the component making up the content of the snack bar.
* @docs-private
*/
containerInstance: MatSnackBarContainer<D>;
/** Subject for notifying the user that the snack bar has been dismissed. */
private readonly _afterDismissed = new Subject<MatSnackBarDismiss>();
/** Subject for notifying the user that the snack bar has opened and appeared. */
private readonly _afterOpened = new Subject<void>();
/** Subject for notifying the user that the snack bar action was called. */
private readonly _onAction = new Subject<void>();
/**
* Timeout ID for the duration setTimeout call. Used to clear the timeout if the snackbar is
* dismissed before the duration passes.
*/
private _durationTimeoutId: ReturnType<typeof setTimeout> | undefined;
/** Whether the snack bar was dismissed using the action button. */
private _dismissedByAction = false;
constructor(
containerInstance: MatSnackBarContainer<D>,
private _overlayRef: OverlayRef,
) {
this.containerInstance = containerInstance;
containerInstance._onExit.subscribe(() => this._finishDismiss());
}
/** Dismisses the snack bar. */
dismiss(): void {
if (!this._afterDismissed.closed) {
this.containerInstance.exit();
}
clearTimeout(this._durationTimeoutId);
}
/** Marks the snackbar action clicked. */
dismissWithAction(): void {
if (!this._onAction.closed) {
this._dismissedByAction = true;
this._onAction.next();
this._onAction.complete();
this.dismiss();
}
clearTimeout(this._durationTimeoutId);
}
/**
* Marks the snackbar action clicked.
* @deprecated Use `dismissWithAction` instead.
* @breaking-change 8.0.0
*/
closeWithAction(): void {
this.dismissWithAction();
}
/** Dismisses the snack bar after some duration */
_dismissAfter(duration: number): void {
// Note that we need to cap the duration to the maximum value for setTimeout, because
// it'll revert to 1 if somebody passes in something greater (e.g. `Infinity`). See #17234.
this._durationTimeoutId = setTimeout(() => this.dismiss(), Math.min(duration, MAX_TIMEOUT));
}
/** Marks the snackbar as opened */
_open(): void {
if (!this._afterOpened.closed) {
this._afterOpened.next();
this._afterOpened.complete();
}
}
/** Cleans up the DOM after closing. */
private _finishDismiss(): void {
this._overlayRef.dispose();
if (!this._onAction.closed) {
this._onAction.complete();
}
this._afterDismissed.next({dismissedByAction: this._dismissedByAction});
this._afterDismissed.complete();
this._dismissedByAction = false;
}
/** Gets an observable that is notified when the snack bar is finished closing. */
afterDismissed(): Observable<MatSnackBarDismiss> {
return this._afterDismissed;
}
/** Gets an observable that is notified when the snack bar has opened and appeared. */
afterOpened(): Observable<void> {
return this.containerInstance._onEnter;
}
/** Gets an observable that is notified when the snack bar action is called. */
onAction(): Observable<void> {
return this._onAction;
}
}