Skip to content

Commit f52d876

Browse files
committed
feat(offcanvas): add backdrop static option support
1 parent fb6d041 commit f52d876

File tree

1 file changed

+36
-32
lines changed

1 file changed

+36
-32
lines changed

projects/coreui-angular/src/lib/offcanvas/offcanvas/offcanvas.component.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
Output,
1313
PLATFORM_ID,
1414
Renderer2,
15-
SimpleChanges,
15+
SimpleChanges
1616
} from '@angular/core';
17-
import { animate, state, style, transition, trigger, } from '@angular/animations';
17+
import { animate, state, style, transition, trigger } from '@angular/animations';
1818
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
1919
import { Subscription } from 'rxjs';
2020

@@ -31,24 +31,23 @@ let nextId = 0;
3131
state(
3232
'true',
3333
style({
34-
visibility: 'visible',
34+
visibility: 'visible'
3535
})
3636
),
3737
state(
3838
'false',
3939
style({
40-
visibility: 'hidden',
40+
visibility: 'hidden'
4141
})
4242
),
43-
transition('true => false', [animate('300ms')]),
44-
]),
43+
transition('true => false', [animate('300ms')])
44+
])
4545
],
4646
templateUrl: './offcanvas.component.html',
4747
styleUrls: ['./offcanvas.component.scss'],
48-
exportAs: 'cOffcanvas',
48+
exportAs: 'cOffcanvas'
4949
})
5050
export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
51-
5251
static ngAcceptInputType_scroll: BooleanInput;
5352

5453
constructor(
@@ -58,14 +57,14 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
5857
private hostElement: ElementRef,
5958
private offcanvasService: OffcanvasService,
6059
private backdropService: BackdropService
61-
) { }
60+
) {}
6261

6362
/**
6463
* Apply a backdrop on body while offcanvas is open.
65-
* @type boolean
64+
* @type boolean | 'static'
6665
* @default true
6766
*/
68-
@Input() backdrop = true;
67+
@Input() backdrop: boolean | 'static' = true;
6968

7069
/**
7170
* Closes the offcanvas when escape key is pressed [docs]
@@ -88,10 +87,12 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
8887
@Input()
8988
set scroll(value: boolean) {
9089
this._scroll = coerceBooleanProperty(value);
91-
};
90+
}
91+
9292
get scroll() {
9393
return this._scroll;
9494
}
95+
9596
private _scroll = false;
9697

9798
@Input() id = `offcanvas-${this.placement}-${nextId++}`;
@@ -125,9 +126,11 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
125126
this.setScroll();
126127
this.visibleChange.emit(value);
127128
}
129+
128130
get visible(): boolean {
129131
return this._visible;
130132
}
133+
131134
private _visible!: boolean;
132135

133136
/**
@@ -145,14 +148,14 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
145148
return {
146149
offcanvas: true,
147150
[`offcanvas-${this.placement}`]: !!this.placement,
148-
show: this.visible,
151+
show: this.visible
149152
};
150153
}
151154

152155
@HostBinding('attr.aria-hidden')
153156
get ariaHidden(): boolean | null {
154157
return this.visible ? null : true;
155-
};
158+
}
156159

157160
@HostBinding('attr.tabindex')
158161
get tabIndex(): string | null {
@@ -166,7 +169,12 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
166169

167170
@HostListener('document:keydown', ['$event'])
168171
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+
) {
170178
this.offcanvasService.toggle({ show: false, id: this.id });
171179
}
172180
}
@@ -189,40 +197,36 @@ export class OffcanvasComponent implements OnChanges, OnInit, OnDestroy {
189197

190198
private stateToggleSubscribe(subscribe: boolean = true): void {
191199
if (subscribe) {
192-
this.stateToggleSubscription = this.offcanvasService.offcanvasState$.subscribe(
193-
(action) => {
200+
this.stateToggleSubscription =
201+
this.offcanvasService.offcanvasState$.subscribe((action) => {
194202
if (this === action.offcanvas || this.id === action.id) {
195203
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;
197206
}
198207
}
199-
}
200-
);
208+
});
201209
} else {
202210
this.stateToggleSubscription.unsubscribe();
203211
}
204212
}
205213

206214
private backdropClickSubscribe(subscribe: boolean = true): void {
207215
if (subscribe) {
208-
this.backdropClickSubscription = this.backdropService.backdropClick$.subscribe(
209-
(clicked) => {
216+
this.backdropClickSubscription =
217+
this.backdropService.backdropClick$.subscribe((clicked) => {
210218
this.offcanvasService.toggle({ show: !clicked, id: this.id });
211-
}
212-
);
219+
});
213220
} else {
214221
this.backdropClickSubscription?.unsubscribe();
215222
}
216223
}
217224

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);
226230
}
227231

228232
setFocus(): void {

0 commit comments

Comments
 (0)