Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7a306a9

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

File tree

Expand file treeCollapse file tree

1 file changed

+21
-16
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+21
-16
lines changed
+21-16Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.