Skip to content

Commit 4274af0

Browse files
committed
feat(utilities): shadow-on-scroll directive
1 parent b3444b2 commit 4274af0

File tree

4 files changed

+83
-21
lines changed

4 files changed

+83
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
export { AlignDirective } from './align.directive';
12
export { BgColorDirective } from './bg-color.directive';
23
export { BorderDirective } from './border.directive';
3-
export { TextColorDirective } from './text-color.directive';
44
export { RoundedDirective } from './rounded.directive';
5-
export { AlignDirective } from './align.directive';
5+
export { ShadowOnScrollDirective } from './shadow-on-scroll.directive';
6+
export { TextColorDirective } from './text-color.directive';
67
export { UtilitiesModule } from './utilities.module';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ElementRef } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
3+
import { ShadowOnScrollDirective } from './shadow-on-scroll.directive';
4+
5+
class MockElementRef extends ElementRef {}
6+
7+
describe('ShadowOnScrollDirective', () => {
8+
9+
beforeEach(() => {
10+
TestBed.configureTestingModule({
11+
imports: [ShadowOnScrollDirective],
12+
providers: [{ provide: ElementRef, useClass: MockElementRef }]
13+
});
14+
});
15+
16+
it('should create an instance', () => {
17+
TestBed.runInInjectionContext(() => {
18+
const directive = new ShadowOnScrollDirective();
19+
expect(directive).toBeTruthy();
20+
});
21+
});
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { DOCUMENT } from '@angular/common';
2+
import { DestroyRef, Directive, effect, ElementRef, inject, Input, signal, WritableSignal } from '@angular/core';
3+
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
4+
import { fromEvent, Subscription } from 'rxjs';
5+
6+
@Directive({
7+
selector: '[cShadowOnScroll]',
8+
standalone: true
9+
})
10+
export class ShadowOnScrollDirective {
11+
12+
readonly #destroyRef: DestroyRef = inject(DestroyRef);
13+
readonly #document: Document = inject(DOCUMENT);
14+
readonly #elementRef: ElementRef = inject(ElementRef);
15+
readonly #scrolled: WritableSignal<boolean> = signal(false);
16+
17+
readonly #scrollEffect = effect(() => {
18+
this.#elementRef.nativeElement.classList.toggle(this.#shadowClass, this.#scrolled());
19+
});
20+
21+
#observable!: Subscription;
22+
#shadowClass = 'shadow-sm';
23+
24+
constructor() {
25+
this.#destroyRef.onDestroy(() => {
26+
this.#scrollEffect?.destroy();
27+
});
28+
}
29+
30+
@Input()
31+
set cShadowOnScroll(value: 'sm' | 'lg' | 'none' | boolean) {
32+
this.#scrolled.set(false);
33+
if (value) {
34+
this.#shadowClass = value === true ? 'shadow' : `shadow-${value}`;
35+
this.#observable = fromEvent(this.#document, 'scroll')
36+
.pipe(
37+
takeUntilDestroyed(this.#destroyRef)
38+
)
39+
.subscribe(scrolled => {
40+
this.#scrolled.set(this.#document.documentElement.scrollTop > 0);
41+
});
42+
} else {
43+
this.#observable?.unsubscribe();
44+
}
45+
};
46+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
import { NgModule } from '@angular/core';
2-
import { BgColorDirective } from './bg-color.directive';
3-
import { BorderDirective } from './border.directive';
4-
import { RoundedDirective } from './rounded.directive';
5-
import { TextColorDirective } from './text-color.directive';
6-
import { AlignDirective } from './align.directive';
2+
3+
import {
4+
AlignDirective,
5+
BgColorDirective,
6+
BorderDirective,
7+
RoundedDirective,
8+
ShadowOnScrollDirective,
9+
TextColorDirective
10+
} from './public_api';
11+
12+
const UTILITY_DIRECTIVES = [AlignDirective, BgColorDirective, BorderDirective, RoundedDirective, ShadowOnScrollDirective, TextColorDirective];
713

814
@NgModule({
9-
imports: [
10-
BgColorDirective,
11-
BorderDirective,
12-
RoundedDirective,
13-
TextColorDirective,
14-
AlignDirective
15-
],
16-
exports: [
17-
BgColorDirective,
18-
BorderDirective,
19-
RoundedDirective,
20-
TextColorDirective,
21-
AlignDirective
22-
]
15+
imports: [...UTILITY_DIRECTIVES], exports: [...UTILITY_DIRECTIVES]
2316
})
2417
export class UtilitiesModule {}

0 commit comments

Comments
 (0)