@@ -12,9 +12,9 @@ import {
12
12
Output ,
13
13
PLATFORM_ID ,
14
14
Renderer2 ,
15
- SimpleChanges ,
15
+ SimpleChanges
16
16
} from '@angular/core' ;
17
- import { animate , state , style , transition , trigger , } from '@angular/animations' ;
17
+ import { animate , state , style , transition , trigger } from '@angular/animations' ;
18
18
import { BooleanInput , coerceBooleanProperty } from '@angular/cdk/coercion' ;
19
19
import { Subscription } from 'rxjs' ;
20
20
@@ -31,24 +31,23 @@ let nextId = 0;
31
31
state (
32
32
'true' ,
33
33
style ( {
34
- visibility : 'visible' ,
34
+ visibility : 'visible'
35
35
} )
36
36
) ,
37
37
state (
38
38
'false' ,
39
39
style ( {
40
- visibility : 'hidden' ,
40
+ visibility : 'hidden'
41
41
} )
42
42
) ,
43
- transition ( 'true => false' , [ animate ( '300ms' ) ] ) ,
44
- ] ) ,
43
+ transition ( 'true => false' , [ animate ( '300ms' ) ] )
44
+ ] )
45
45
] ,
46
46
templateUrl : './offcanvas.component.html' ,
47
47
styleUrls : [ './offcanvas.component.scss' ] ,
48
- exportAs : 'cOffcanvas' ,
48
+ exportAs : 'cOffcanvas'
49
49
} )
50
50
export class OffcanvasComponent implements OnChanges , OnInit , OnDestroy {
51
-
52
51
static ngAcceptInputType_scroll : BooleanInput ;
53
52
54
53
constructor (
@@ -58,14 +57,14 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
58
57
private hostElement : ElementRef ,
59
58
private offcanvasService : OffcanvasService ,
60
59
private backdropService : BackdropService
61
- ) { }
60
+ ) { }
62
61
63
62
/**
64
63
* Apply a backdrop on body while offcanvas is open.
65
- * @type boolean
64
+ * @type boolean | 'static'
66
65
* @default true
67
66
*/
68
- @Input ( ) backdrop = true ;
67
+ @Input ( ) backdrop : boolean | 'static' = true ;
69
68
70
69
/**
71
70
* Closes the offcanvas when escape key is pressed [docs]
@@ -88,10 +87,12 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
88
87
@Input ( )
89
88
set scroll ( value : boolean ) {
90
89
this . _scroll = coerceBooleanProperty ( value ) ;
91
- } ;
90
+ }
91
+
92
92
get scroll ( ) {
93
93
return this . _scroll ;
94
94
}
95
+
95
96
private _scroll = false ;
96
97
97
98
@Input ( ) id = `offcanvas-${ this . placement } -${ nextId ++ } ` ;
@@ -125,9 +126,11 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
125
126
this . setScroll ( ) ;
126
127
this . visibleChange . emit ( value ) ;
127
128
}
129
+
128
130
get visible ( ) : boolean {
129
131
return this . _visible ;
130
132
}
133
+
131
134
private _visible ! : boolean ;
132
135
133
136
/**
@@ -145,14 +148,14 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
145
148
return {
146
149
offcanvas : true ,
147
150
[ `offcanvas-${ this . placement } ` ] : ! ! this . placement ,
148
- show : this . visible ,
151
+ show : this . visible
149
152
} ;
150
153
}
151
154
152
155
@HostBinding ( 'attr.aria-hidden' )
153
156
get ariaHidden ( ) : boolean | null {
154
157
return this . visible ? null : true ;
155
- } ;
158
+ }
156
159
157
160
@HostBinding ( 'attr.tabindex' )
158
161
get tabIndex ( ) : string | null {
@@ -166,7 +169,12 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
166
169
167
170
@HostListener ( 'document:keydown' , [ '$event' ] )
168
171
onKeyDownHandler ( event : KeyboardEvent ) : void {
169
- if ( event . key === 'Escape' && this . keyboard && this . visible ) {
172
+ if (
173
+ event . key === 'Escape' &&
174
+ this . keyboard &&
175
+ this . visible &&
176
+ this . backdrop !== 'static'
177
+ ) {
170
178
this . offcanvasService . toggle ( { show : false , id : this . id } ) ;
171
179
}
172
180
}
@@ -189,40 +197,36 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
189
197
190
198
private stateToggleSubscribe ( subscribe : boolean = true ) : void {
191
199
if ( subscribe ) {
192
- this . stateToggleSubscription = this . offcanvasService . offcanvasState$ . subscribe (
193
- ( action ) => {
200
+ this . stateToggleSubscription =
201
+ this . offcanvasService . offcanvasState$ . subscribe ( ( action ) => {
194
202
if ( this === action . offcanvas || this . id === action . id ) {
195
203
if ( 'show' in action ) {
196
- this . visible = action ?. show === 'toggle' ? ! this . visible : action . show ;
204
+ this . visible =
205
+ action ?. show === 'toggle' ? ! this . visible : action . show ;
197
206
}
198
207
}
199
- }
200
- ) ;
208
+ } ) ;
201
209
} else {
202
210
this . stateToggleSubscription . unsubscribe ( ) ;
203
211
}
204
212
}
205
213
206
214
private backdropClickSubscribe ( subscribe : boolean = true ) : void {
207
215
if ( subscribe ) {
208
- this . backdropClickSubscription = this . backdropService . backdropClick$ . subscribe (
209
- ( clicked ) => {
216
+ this . backdropClickSubscription =
217
+ this . backdropService . backdropClick$ . subscribe ( ( clicked ) => {
210
218
this . offcanvasService . toggle ( { show : ! clicked , id : this . id } ) ;
211
- }
212
- ) ;
219
+ } ) ;
213
220
} else {
214
221
this . backdropClickSubscription ?. unsubscribe ( ) ;
215
222
}
216
223
}
217
224
218
- private setBackdrop ( setBackdrop : boolean ) : void {
219
- if ( setBackdrop ) {
220
- this . activeBackdrop = this . backdropService . setBackdrop ( 'offcanvas' ) ;
221
- this . backdropClickSubscribe ( ) ;
222
- } else {
223
- this . activeBackdrop = this . backdropService . clearBackdrop ( this . activeBackdrop ) ;
224
- this . backdropClickSubscribe ( false ) ;
225
- }
225
+ private setBackdrop ( setBackdrop : boolean | 'static' ) : void {
226
+ this . activeBackdrop = ! ! setBackdrop ? this . backdropService . setBackdrop ( 'offcanvas' )
227
+ : this . backdropService . clearBackdrop ( this . activeBackdrop ) ;
228
+ setBackdrop === true ? this . backdropClickSubscribe ( )
229
+ : this . backdropClickSubscribe ( false ) ;
226
230
}
227
231
228
232
setFocus ( ) : void {
0 commit comments