Skip to content

Commit 7a306a9

Browse files
committed
refactor(shadow-on-scroll): signal inputs, host bindings, cleanup, tests
1 parent c648c30 commit 7a306a9

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed
Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
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';
33
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
44
import { fromEvent, Subscription } from 'rxjs';
55

66
@Directive({
7-
selector: '[cShadowOnScroll]'
7+
selector: '[cShadowOnScroll]',
8+
exportAs: 'cShadowOnScroll'
89
})
910
export 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

Comments
 (0)