11import { DOCUMENT } from '@angular/common' ;
2- import { DestroyRef , Directive , effect , ElementRef , inject , Input , signal , WritableSignal } from '@angular/core' ;
2+ import { DestroyRef , Directive , effect , ElementRef , inject , input , signal , untracked , WritableSignal } from '@angular/core' ;
33import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
44import { fromEvent , Subscription } from 'rxjs' ;
55
66@Directive ( {
7- selector : '[cShadowOnScroll]'
7+ selector : '[cShadowOnScroll]' ,
8+ exportAs : 'cShadowOnScroll'
89} )
910export class ShadowOnScrollDirective {
1011 readonly #destroyRef: DestroyRef = inject ( DestroyRef ) ;
@@ -25,18 +26,22 @@ export class ShadowOnScrollDirective {
2526 } ) ;
2627 }
2728
28- @Input ( )
29- set cShadowOnScroll ( value : 'sm' | 'lg' | 'none' | boolean ) {
30- this . #scrolled. set ( false ) ;
31- if ( value ) {
32- this . #shadowClass = value === true ? 'shadow' : `shadow-${ value } ` ;
33- this . #observable = fromEvent ( this . #document, 'scroll' )
34- . pipe ( takeUntilDestroyed ( this . #destroyRef) )
35- . subscribe ( ( scrolled ) => {
36- this . #scrolled. set ( this . #document. documentElement . scrollTop > 0 ) ;
37- } ) ;
38- } else {
39- this . #observable?. unsubscribe ( ) ;
40- }
41- }
29+ readonly cShadowOnScroll = input < 'sm' | 'lg' | 'none' | boolean > ( true ) ;
30+
31+ readonly #shadowOnScrollEffect = effect ( ( ) => {
32+ const value = this . cShadowOnScroll ( ) ;
33+ untracked ( ( ) => {
34+ this . #scrolled. set ( false ) ;
35+ if ( value ) {
36+ this . #shadowClass = value === true ? 'shadow' : `shadow-${ value } ` ;
37+ this . #observable = fromEvent ( this . #document, 'scroll' )
38+ . pipe ( takeUntilDestroyed ( this . #destroyRef) )
39+ . subscribe ( ( scrolled ) => {
40+ this . #scrolled. set ( this . #document. documentElement . scrollTop > 0 ) ;
41+ } ) ;
42+ } else {
43+ this . #observable?. unsubscribe ( ) ;
44+ }
45+ } ) ;
46+ } ) ;
4247}
0 commit comments