Skip to content

Commit

Permalink
feat(rx): add directive which uses resize-observer
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbe812 committed Jan 15, 2023
1 parent 49647c6 commit 4fee038
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {ObserveResizeDirective, ObserveResizeDirectiveModule} from './observe-resize.directive';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {mockResizeObserver} from '@angular-kit/testing';

describe('ObserveResizeDirective', () => {
it('should create an instance', async () => {
const { testComponent } = await create();
expect(testComponent).toBeTruthy();
});
});

async function create() {
@Component({
template: `
<section style="position: relative; height: 200px; overflow: auto;">
<h1
id="resize_elem"
style="position: absolute; top: 200px; height: 200px;"
observeResize
(resizeEvent)="onResize()"
>
I'm being observed
</h1>
</section>
`,
})
class TestComponent {
onResize = jest.fn();
observe = true;
}

let fixture: ComponentFixture<TestComponent>;
let testComponent: TestComponent;

TestBed.configureTestingModule({
imports: [ObserveResizeDirectiveModule],
declarations: [TestComponent],
});
mockResizeObserver();
fixture = TestBed.createComponent(TestComponent);
testComponent = fixture.componentInstance;
fixture.detectChanges();

return { fixture, testComponent };
}
41 changes: 41 additions & 0 deletions libs/rx/platform/src/lib/directives/observe-resize.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {Directive, ElementRef, Input, NgModule, OnDestroy, Output} from '@angular/core';
import {CommonModule} from '@angular/common';
import {Subscription} from 'rxjs';
import {createResizeObserver, ResizeObserverConfig} from '../create-resize-observer';
import {createSignal} from '@code-workers.io/angular-kit/rx/signal';
import {Nullable} from '@code-workers.io/angular-kit/cdk/types';

@Directive({
selector: '[observeResize]',
})
export class ObserveResizeDirective implements OnDestroy {
private subscription = new Subscription();
private configSignal = createSignal<ResizeObserverConfig | null>();

@Input() set resizeObserverConfig(config: ResizeObserverConfig | null) {
this.configSignal.send(config);
}

private resizeObserver$ = createResizeObserver(this.element);

@Output() resizeEvent = this.resizeObserver$;

constructor(private element: ElementRef) {
this.subscription.add(
this.configSignal.$.subscribe((config: Nullable<ResizeObserverConfig>) => {
this.resizeObserver$ = createResizeObserver(this.element, config ?? undefined);
})
);
}

ngOnDestroy() {
this.subscription.unsubscribe();
}
}

@NgModule({
imports: [CommonModule],
declarations: [ObserveResizeDirective],
exports: [ObserveResizeDirective],
})
export class ObserveResizeDirectiveModule {}

0 comments on commit 4fee038

Please sign in to comment.