Skip to content

Commit 4fee038

Browse files
committed
feat(rx): add directive which uses resize-observer
1 parent 49647c6 commit 4fee038

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import {ObserveResizeDirective, ObserveResizeDirectiveModule} from './observe-resize.directive';
2+
import {Component} from '@angular/core';
3+
import {ComponentFixture, TestBed} from '@angular/core/testing';
4+
import {mockResizeObserver} from '@angular-kit/testing';
5+
6+
describe('ObserveResizeDirective', () => {
7+
it('should create an instance', async () => {
8+
const { testComponent } = await create();
9+
expect(testComponent).toBeTruthy();
10+
});
11+
});
12+
13+
async function create() {
14+
@Component({
15+
template: `
16+
<section style="position: relative; height: 200px; overflow: auto;">
17+
<h1
18+
id="resize_elem"
19+
style="position: absolute; top: 200px; height: 200px;"
20+
observeResize
21+
(resizeEvent)="onResize()"
22+
>
23+
I'm being observed
24+
</h1>
25+
</section>
26+
`,
27+
})
28+
class TestComponent {
29+
onResize = jest.fn();
30+
observe = true;
31+
}
32+
33+
let fixture: ComponentFixture<TestComponent>;
34+
let testComponent: TestComponent;
35+
36+
TestBed.configureTestingModule({
37+
imports: [ObserveResizeDirectiveModule],
38+
declarations: [TestComponent],
39+
});
40+
mockResizeObserver();
41+
fixture = TestBed.createComponent(TestComponent);
42+
testComponent = fixture.componentInstance;
43+
fixture.detectChanges();
44+
45+
return { fixture, testComponent };
46+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {Directive, ElementRef, Input, NgModule, OnDestroy, Output} from '@angular/core';
2+
import {CommonModule} from '@angular/common';
3+
import {Subscription} from 'rxjs';
4+
import {createResizeObserver, ResizeObserverConfig} from '../create-resize-observer';
5+
import {createSignal} from '@code-workers.io/angular-kit/rx/signal';
6+
import {Nullable} from '@code-workers.io/angular-kit/cdk/types';
7+
8+
@Directive({
9+
selector: '[observeResize]',
10+
})
11+
export class ObserveResizeDirective implements OnDestroy {
12+
private subscription = new Subscription();
13+
private configSignal = createSignal<ResizeObserverConfig | null>();
14+
15+
@Input() set resizeObserverConfig(config: ResizeObserverConfig | null) {
16+
this.configSignal.send(config);
17+
}
18+
19+
private resizeObserver$ = createResizeObserver(this.element);
20+
21+
@Output() resizeEvent = this.resizeObserver$;
22+
23+
constructor(private element: ElementRef) {
24+
this.subscription.add(
25+
this.configSignal.$.subscribe((config: Nullable<ResizeObserverConfig>) => {
26+
this.resizeObserver$ = createResizeObserver(this.element, config ?? undefined);
27+
})
28+
);
29+
}
30+
31+
ngOnDestroy() {
32+
this.subscription.unsubscribe();
33+
}
34+
}
35+
36+
@NgModule({
37+
imports: [CommonModule],
38+
declarations: [ObserveResizeDirective],
39+
exports: [ObserveResizeDirective],
40+
})
41+
export class ObserveResizeDirectiveModule {}

0 commit comments

Comments
 (0)