1
1
import { 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' ;
3
3
import { takeUntilDestroyed } from '@angular/core/rxjs-interop' ;
4
4
import { fromEvent , Subscription } from 'rxjs' ;
5
5
6
6
@Directive ( {
7
- selector : '[cShadowOnScroll]'
7
+ selector : '[cShadowOnScroll]' ,
8
+ exportAs : 'cShadowOnScroll'
8
9
} )
9
10
export class ShadowOnScrollDirective {
10
11
readonly #destroyRef: DestroyRef = inject ( DestroyRef ) ;
@@ -25,18 +26,22 @@ export class ShadowOnScrollDirective {
25
26
} ) ;
26
27
}
27
28
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
+ } ) ;
42
47
}
0 commit comments