11import {
2+ afterNextRender ,
23 ChangeDetectorRef ,
34 Directive ,
45 ElementRef ,
56 EventEmitter ,
6- HostBinding ,
77 HostListener ,
8+ inject ,
89 Inject ,
10+ Injector ,
911 Input ,
1012 OnDestroy ,
1113 OnInit ,
1214 Optional ,
13- Output
15+ Output ,
16+ runInInjectionContext
1417} from '@angular/core' ;
1518import { AbsoluteScrollStrategy } from '../../services/overlay/scroll/absolute-scroll-strategy' ;
1619import { CancelableBrowserEventArgs , IBaseEventArgs , PlatformUtil } from '../../core/utils' ;
@@ -34,7 +37,13 @@ export interface ToggleViewCancelableEventArgs extends ToggleViewEventArgs, Canc
3437@Directive ( {
3538 exportAs : 'toggle' ,
3639 selector : '[igxToggle]' ,
37- standalone : true
40+ standalone : true ,
41+ host : {
42+ '[class.igx-toggle--hidden]' : 'hiddenClass' ,
43+ '[attr.aria-hidden]' : 'hiddenClass' ,
44+ '[class.igx-toggle--hidden-webkit]' : 'hiddenWebkitClass' ,
45+ '[class.igx-toggle]' : 'defaultClass'
46+ }
3847} )
3948export class IgxToggleDirective implements IToggleView , OnInit , OnDestroy {
4049 /**
@@ -159,13 +168,13 @@ export class IgxToggleDirective implements IToggleView, OnInit, OnDestroy {
159168 /**
160169 * @hidden
161170 */
162- @HostBinding ( 'class.igx-toggle--hidden' )
163- @HostBinding ( 'attr.aria-hidden' )
164171 public get hiddenClass ( ) {
165172 return this . collapsed ;
166173 }
167174
168- @HostBinding ( 'class.igx-toggle--hidden-webkit' )
175+ /**
176+ * @hidden
177+ */
169178 public get hiddenWebkitClass ( ) {
170179 const isSafari = this . platform ?. isSafari ;
171180 const browserVersion = this . platform ?. browserVersion ;
@@ -176,7 +185,6 @@ export class IgxToggleDirective implements IToggleView, OnInit, OnDestroy {
176185 /**
177186 * @hidden
178187 */
179- @HostBinding ( 'class.igx-toggle' )
180188 public get defaultClass ( ) {
181189 return ! this . collapsed ;
182190 }
@@ -193,6 +201,7 @@ export class IgxToggleDirective implements IToggleView, OnInit, OnDestroy {
193201 private _overlayClosingSub : Subscription ;
194202 private _overlayClosedSub : Subscription ;
195203 private _overlayContentAppendedSub : Subscription ;
204+ private _injector = inject ( Injector ) ;
196205
197206 /**
198207 * @hidden
@@ -224,25 +233,28 @@ export class IgxToggleDirective implements IToggleView, OnInit, OnDestroy {
224233 }
225234
226235 this . _collapsed = false ;
227- this . cdr . detectChanges ( ) ;
228236
229- if ( ! info ) {
230- this . unsubscribe ( ) ;
231- this . subscribe ( ) ;
232- this . _overlayId = this . overlayService . attach ( this . elementRef , overlaySettings ) ;
233- }
237+ runInInjectionContext ( this . _injector , ( ) => {
238+ afterNextRender ( ( ) => {
239+ if ( ! info ) {
240+ this . unsubscribe ( ) ;
241+ this . subscribe ( ) ;
242+ this . _overlayId = this . overlayService . attach ( this . elementRef , overlaySettings ) ;
243+ }
234244
235- const args : ToggleViewCancelableEventArgs = { cancel : false , owner : this , id : this . _overlayId } ;
236- this . opening . emit ( args ) ;
237- if ( args . cancel ) {
238- this . unsubscribe ( ) ;
239- this . overlayService . detach ( this . _overlayId ) ;
240- this . _collapsed = true ;
241- delete this . _overlayId ;
242- this . cdr . detectChanges ( ) ;
243- return ;
244- }
245- this . overlayService . show ( this . _overlayId , overlaySettings ) ;
245+ const args : ToggleViewCancelableEventArgs = { cancel : false , owner : this , id : this . _overlayId } ;
246+ this . opening . emit ( args ) ;
247+ if ( args . cancel ) {
248+ this . unsubscribe ( ) ;
249+ this . overlayService . detach ( this . _overlayId ) ;
250+ this . _collapsed = true ;
251+ delete this . _overlayId ;
252+ this . cdr . detectChanges ( ) ;
253+ return ;
254+ }
255+ this . overlayService . show ( this . _overlayId , overlaySettings ) ;
256+ } ) ;
257+ } ) ;
246258 }
247259
248260 /**
0 commit comments