Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Portal Logic for UpWindowAngularComponent #13

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,36 @@ describe('UpWindowAngularComponent', () => {
expect(subtitleElement.textContent).toContain('Test Subtitle');
});

it('should append modal to body when isOpen is true', fakeAsync(() => {
component.isOpen.set(false);
fixture.detectChanges();
expect(document.body.querySelector('.up-window')).toBeNull();

component.isOpen.set(true);
fixture.detectChanges();
tick(100);
expect(document.body.querySelector('.up-window')).toBeTruthy();

component.isOpen.set(false);
fixture.detectChanges();
tick(400);
expect(document.body.querySelector('.up-window')).toBeNull();
}));

it('should not reappend modal if it is already in the body', fakeAsync(() => {
component.isOpen.set(true);
fixture.detectChanges();
tick(600);

spyOn(component, 'addModalToBody').and.callThrough();

component.addModalToBody();
fixture.detectChanges();

expect(component.addModalToBody).toHaveBeenCalledTimes(1);
expect(document.body.querySelectorAll('.up-window').length).toBe(1);
}));

it('should apply the correct animation class when opening and closing the window', fakeAsync(() => {
component.animation = 'slide';

Expand Down
25 changes: 22 additions & 3 deletions projects/up-window-angular/src/lib/up-window-angular.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ export class UpWindowAngularComponent implements OnInit, OnDestroy {

constructor() {
effect(() => {
this.isOpen()
? this.startOpeningAnimation()
: this.startClosingAnimation();
if (this.isOpen()) {
this.addModalToBody();
this.startOpeningAnimation();
} else {
this.startClosingAnimation();
this.removeModalFromBody();
}
});
}

Expand All @@ -67,6 +71,8 @@ export class UpWindowAngularComponent implements OnInit, OnDestroy {

ngAfterViewInit(): void {
if (this.modal) {
document.body.appendChild(this.modal.nativeElement);

this.focusableElements = this.modal.nativeElement.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
Expand All @@ -78,6 +84,18 @@ export class UpWindowAngularComponent implements OnInit, OnDestroy {
}
}

addModalToBody() {
if (this.modal && this.modal.nativeElement.parentNode !== document.body) {
document.body.appendChild(this.modal.nativeElement);
}
}

removeModalFromBody() {
if (this.modal && this.modal.nativeElement.parentNode === document.body) {
document.body.removeChild(this.modal.nativeElement);
}
}

handleKeydown(event: KeyboardEvent) {
if (event.key === 'Escape' && this.isOpen() && !this.restrictMode) {
this.closeWindow();
Expand Down Expand Up @@ -111,6 +129,7 @@ export class UpWindowAngularComponent implements OnInit, OnDestroy {
}

ngOnDestroy(): void {
this.removeModalFromBody();
document.removeEventListener('keydown', this.handleKeydown.bind(this));
}

Expand Down