Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Drop-modal with portal

Benjamin Bädorf72a8976ab548
diff --git a/frontend/src/app/core/setup/global-dynamic-components.const.ts b/frontend/src/app/core/setup/global-dynamic-components.const.ts
index 3d3c5684539..75135990a86 100644
--- a/frontend/src/app/core/setup/global-dynamic-components.const.ts
+++ b/frontend/src/app/core/setup/global-dynamic-components.const.ts
@@ -211,6 +211,7 @@ import {
OpSingleDatePickerComponent,
opSingleDatePickerSelector,
} from 'core-app/shared/components/datepicker/single-date-picker/single-date-picker.component';
+import { SpotDropModalPortalComponent, spotDropModalPortalComponentSelector } from 'core-app/spot/components/drop-modal/drop-modal-portal.component';
export const globalDynamicComponents:OptionalBootstrapDefinition[] = [
{ selector: appBaseSelector, cls: ApplicationBaseComponent },
@@ -269,4 +270,5 @@ export const globalDynamicComponents:OptionalBootstrapDefinition[] = [
{ selector: ianMenuSelector, cls: IanMenuComponent },
{ selector: opModalOverlaySelector, cls: OpModalOverlayComponent },
{ selector: opSingleDatePickerSelector, cls: OpSingleDatePickerComponent },
+ { selector: spotDropModalPortalComponentSelector, cls: SpotDropModalPortalComponent },
];
diff --git a/frontend/src/app/shared/components/datepicker/multi-date-picker/multi-date-picker.component.html b/frontend/src/app/shared/components/datepicker/multi-date-picker/multi-date-picker.component.html
index 68c235d5fa5..c463488987d 100644
--- a/frontend/src/app/shared/components/datepicker/multi-date-picker/multi-date-picker.component.html
+++ b/frontend/src/app/shared/components/datepicker/multi-date-picker/multi-date-picker.component.html
@@ -1,5 +1,5 @@
<spot-drop-modal
- [open]="isOpened"
+ [opened]="isOpened"
(closed)="close()"
>
<button
diff --git a/frontend/src/app/shared/components/datepicker/single-date-picker/single-date-picker.component.html b/frontend/src/app/shared/components/datepicker/single-date-picker/single-date-picker.component.html
index 0676efae350..c3779d50e73 100644
--- a/frontend/src/app/shared/components/datepicker/single-date-picker/single-date-picker.component.html
+++ b/frontend/src/app/shared/components/datepicker/single-date-picker/single-date-picker.component.html
@@ -1,5 +1,5 @@
<spot-drop-modal
- [open]="opened"
+ [opened]="opened"
(closed)="opened = false"
>
<ng-content
diff --git a/frontend/src/app/shared/components/fields/edit/field-types/combined-date-edit-field.component.html b/frontend/src/app/shared/components/fields/edit/field-types/combined-date-edit-field.component.html
index 853b63421e4..4e6e753e6d7 100644
--- a/frontend/src/app/shared/components/fields/edit/field-types/combined-date-edit-field.component.html
+++ b/frontend/src/app/shared/components/fields/edit/field-types/combined-date-edit-field.component.html
@@ -1,5 +1,5 @@
<spot-drop-modal
- [open]="opened"
+ [opened]="opened"
(closed)="cancel()"
alignment="bottom-center"
>
diff --git a/frontend/src/app/shared/components/fields/edit/field-types/days-duration-edit-field.component.html b/frontend/src/app/shared/components/fields/edit/field-types/days-duration-edit-field.component.html
index ab95275b409..1e64d5f96df 100644
--- a/frontend/src/app/shared/components/fields/edit/field-types/days-duration-edit-field.component.html
+++ b/frontend/src/app/shared/components/fields/edit/field-types/days-duration-edit-field.component.html
@@ -1,5 +1,5 @@
<spot-drop-modal
- [open]="opened"
+ [opened]="opened"
(closed)="cancel()"
alignment="bottom-center"
>
diff --git a/frontend/src/app/shared/components/header-project-select/header-project-select.component.html b/frontend/src/app/shared/components/header-project-select/header-project-select.component.html
index 07bb8deb070..55f87bb83be 100644
--- a/frontend/src/app/shared/components/header-project-select/header-project-select.component.html
+++ b/frontend/src/app/shared/components/header-project-select/header-project-select.component.html
@@ -1,5 +1,5 @@
<spot-drop-modal
- [open]="dropModalOpen"
+ [opened]="dropModalOpen"
[allowRepositioning]="false"
(closed)="close()"
alignment="bottom-left"
diff --git a/frontend/src/app/shared/components/project-include/project-include.component.html b/frontend/src/app/shared/components/project-include/project-include.component.html
index f3c9bfbaa7f..ab85fa17e08 100644
--- a/frontend/src/app/shared/components/project-include/project-include.component.html
+++ b/frontend/src/app/shared/components/project-include/project-include.component.html
@@ -1,5 +1,5 @@
<spot-drop-modal
- [open]="opened"
+ [opened]="opened"
alignment="bottom-right"
(closed)="opened = false"
class="op-project-list-modal"
diff --git a/frontend/src/app/spot/components/drop-modal/drop-modal-portal.component.ts b/frontend/src/app/spot/components/drop-modal/drop-modal-portal.component.ts
new file mode 100644
index 00000000000..bf2d0324f2d
--- /dev/null
+++ b/frontend/src/app/spot/components/drop-modal/drop-modal-portal.component.ts
@@ -0,0 +1,21 @@
+import {
+ AfterViewChecked,
+ Component,
+} from '@angular/core';
+import { SpotDropModalTeleportationService } from './drop-modal-teleportation.service';
+
+export const spotDropModalPortalComponentSelector = 'spot-drop-modal-portal';
+
+@Component({
+ selector: spotDropModalPortalComponentSelector,
+ template: '<ng-container *ngTemplateOutlet="template$ | async"></ng-container>',
+})
+export class SpotDropModalPortalComponent implements AfterViewChecked {
+ constructor(
+ readonly template$: SpotDropModalTeleportationService,
+ ) { }
+
+ ngAfterViewChecked(): void {
+ this.template$.hasRendered$.next();
+ }
+}
diff --git a/frontend/src/app/spot/components/drop-modal/drop-modal-teleport.directive.ts b/frontend/src/app/spot/components/drop-modal/drop-modal-teleport.directive.ts
new file mode 100644
index 00000000000..fff38f58ca1
--- /dev/null
+++ b/frontend/src/app/spot/components/drop-modal/drop-modal-teleport.directive.ts
@@ -0,0 +1,29 @@
+import {
+ Directive,
+ OnDestroy,
+ OnInit,
+ TemplateRef,
+} from "@angular/core";
+import { SpotDropModalTeleportationService } from "./drop-modal-teleportation.service";
+
+@Directive({
+ selector: 'ng-template[spotDropModalTeleport]'
+})
+export class SpotDropModalTeleportDirective implements OnInit, OnDestroy {
+ constructor(
+ private teleportationService: SpotDropModalTeleportationService,
+ private template: TemplateRef<any>,
+ ) { }
+
+ ngOnInit() {
+ // Teleports the template to the new target portal
+ setTimeout(() => {
+ this.teleportationService.activate(this.template);
+ });
+ }
+
+ ngOnDestroy() {
+ // Clears the portal on destroy whenever the target is defined
+ this.teleportationService.clear();
+ }
+}
diff --git a/frontend/src/app/spot/components/drop-modal/drop-modal-teleportation.service.ts b/frontend/src/app/spot/components/drop-modal/drop-modal-teleportation.service.ts
new file mode 100644
index 00000000000..31773f6637e
--- /dev/null
+++ b/frontend/src/app/spot/components/drop-modal/drop-modal-teleportation.service.ts
@@ -0,0 +1,25 @@
+import {
+ Injectable,
+ TemplateRef,
+} from '@angular/core';
+import { BehaviorSubject, Subject } from 'rxjs';
+
+export type TeleportInstance = TemplateRef<any>;
+
+@Injectable({ providedIn: 'root' })
+export class SpotDropModalTeleportationService extends BehaviorSubject<TeleportInstance|null> {
+
+ public hasRendered$ = new Subject();
+
+ constructor() {
+ super(null);
+ }
+
+ public activate(instance: TeleportInstance) {
+ this.next(instance);
+ }
+
+ public clear() {
+ this.next(null);
+ }
+}
diff --git a/frontend/src/app/spot/components/drop-modal/drop-modal.component.html b/frontend/src/app/spot/components/drop-modal/drop-modal.component.html
index 7c9801251b8..cf090a8ab11 100644
--- a/frontend/src/app/spot/components/drop-modal/drop-modal.component.html
+++ b/frontend/src/app/spot/components/drop-modal/drop-modal.component.html
@@ -1,23 +1,30 @@
<ng-content select="[slot=trigger]"></ng-content>
-<div
- *ngIf="open"
- [ngClass]="['spot-drop-modal--body', 'spot-container', alignmentClass]"
- (click)="onBodyClick($event)"
+<ng-template #body>
+ <div
+ *ngIf="opened"
+ class="spot-drop-modal--anchor"
+ [ngStyle]="anchorStyles"
+ [attr.id]="id"
+ >
+ <div
+ [ngClass]="['spot-drop-modal--body', 'spot-container', alignmentClass]"
- cdkTrapFocus
- tabindex="0"
+ (click)="onBodyClick($event)"
- #modalBody
->
- <ng-content select="[slot=body]"></ng-content>
+ cdkTrapFocus
+ tabindex="0"
+ >
+ <ng-content select="[slot=body]"></ng-content>
- <button
- class="spot-drop-modal--close-button spot-button"
- type="button"
- [attr.aria-label]="text.close"
- (click)="close()"
- >
- <span class="spot-icon spot-icon_close"></span>
- </button>
-</div>
+ <button
+ class="spot-drop-modal--close-button spot-button"
+ type="button"
+ [attr.aria-label]="text.close"
+ (click)="close()"
+ >
+ <span class="spot-icon spot-icon_close"></span>
+ </button>
+ </div>
+ </div>
+</ng-template>
diff --git a/frontend/src/app/spot/components/drop-modal/drop-modal.component.ts b/frontend/src/app/spot/components/drop-modal/drop-modal.component.ts
index 9df47b1c176..08750db34a5 100644
--- a/frontend/src/app/spot/components/drop-modal/drop-modal.component.ts
+++ b/frontend/src/app/spot/components/drop-modal/drop-modal.component.ts
@@ -8,12 +8,16 @@ import {
Input,
OnDestroy,
Output,
+ TemplateRef,
ViewChild,
} from '@angular/core';
import { KeyCodes } from 'core-app/shared/helpers/keyCodes.enum';
import { I18nService } from 'core-app/core/i18n/i18n.service';
import { findAllFocusableElementsWithin } from 'core-app/shared/helpers/focus-helpers';
import SpotDropAlignmentOption from '../../drop-alignment-options';
+import { SpotDropModalTeleportationService } from './drop-modal-teleportation.service';
+import { take } from 'rxjs/operators';
+import { debounce } from 'lodash';
const findClippingParent = (el:HTMLElement):HTMLElement => {
const parent = el.parentElement;
@@ -54,62 +58,32 @@ export class SpotDropModalComponent implements OnDestroy {
return `spot-drop-modal--body_${this.allowRepositioning ? this.calculatedAlignment : this.alignment}`;
}
- public _open = false;
+ public _opened = false;
/**
* Boolean indicating whether the modal should be opened
*/
/* eslint-disable-next-line @angular-eslint/no-input-rename */
- @Input('open')
+ @Input('opened')
@HostBinding('class.spot-drop-modal_opened')
- set open(value:boolean) {
- if (this._open === !!value) {
+ set opened(value:boolean) {
+ console.log('opened change', this._opened, !!value);
+
+ if (this._opened === !!value) {
return;
}
- this._open = !!value;
+ this._opened = !!value;
- if (this._open) {
- /* We have to set these listeners next tick, because they're so far up the tree.
- * If the open value was set because of a click listener in the trigger slot,
- * that event would reach the event listener added here and close the modal right away.
- */
- setTimeout(() => {
- document.body.addEventListener('click', this.closeEventListener);
- document.body.addEventListener('keydown', this.escapeListener);
- window.addEventListener('resize', this.appHeightListener);
- window.addEventListener('orientationchange', this.appHeightListener);
- this.appHeightListener();
-
- if (this.allowRepositioning) {
- this.recalculateAlignment();
- }
-
- // If we already have focus within the modal, don't move it
- if (this.elementRef.nativeElement.contains(document.activeElement)) {
- return;
- }
-
- const focusCatcherContainer = document.querySelectorAll("[data-modal-focus-catcher-container='true']")[0];
- if (focusCatcherContainer) {
- (findAllFocusableElementsWithin(focusCatcherContainer as HTMLElement)[0] as HTMLElement).focus();
- } else {
- // Index 1 because the element at index 0 is the trigger button to open the modal
- (findAllFocusableElementsWithin(this.elementRef.nativeElement)[1] as HTMLElement).focus();
- }
- });
+ if (this._opened) {
+ this.open();
} else {
- document.body.removeEventListener('click', this.closeEventListener);
- document.body.removeEventListener('keydown', this.escapeListener);
- window.removeEventListener('resize', this.appHeightListener);
- window.removeEventListener('orientationchange', this.appHeightListener);
-
- this.closed.emit();
+ this.close();
}
}
- get open():boolean {
- return this._open;
+ get opened():boolean {
+ return this._opened;
}
/**
@@ -132,23 +106,95 @@ export class SpotDropModalComponent implements OnDestroy {
*/
@Output() closed = new EventEmitter<void>();
+ public anchorStyles = {
+ top: '0',
+ left: '0',
+ width: '0',
+ height: '0',
+ };
+
+ public id = `drop-modal-${Math.round(Math.random() * 10000)}`;
+
public text = {
close: this.i18n.t('js.spot.drop_modal.close'),
};
- @ViewChild('modalBody') modalBody:ElementRef;
+ @ViewChild('body') body:TemplateRef<any>;
constructor(
readonly i18n:I18nService,
readonly elementRef:ElementRef,
readonly cdRef:ChangeDetectorRef,
+ private teleportationService:SpotDropModalTeleportationService,
) {}
+ open() {
+ this._opened = true;
+ this.repositionAnchor();
+ this.updateAppHeight();
+
+ /**
+ * If we don't activate the body after one tick, angular will complain because
+ * it already rendered a `null` template, but then gets an update to that
+ * template in the same tick.
+ * To make it happy, we update afterwards
+ */
+ setTimeout(() => {
+ /* We have to set these listeners next tick, because they're so far up the tree.
+ * If the open value was set because of a click listener in the trigger slot,
+ * that event would reach the event listener added here and close the modal right away.
+ */
+ this.teleportationService.activate(this.body)
+ this.teleportationService.hasRendered$
+ .pipe(take(1))
+ .subscribe(() => {
+ document.body.addEventListener('click', this.close);
+ document.body.addEventListener('keydown', this.onEscape);
+ document.body.addEventListener('scroll', this.repositionAnchor);
+ window.addEventListener('resize', this.onResizeDebounced);
+ window.addEventListener('orientationchange', this.onResizeDebounced);
+
+ setTimeout(() => {
+ this.recalculateAlignment();
+ });
+
+ // If we already have focus within the modal, don't move it
+ if (this.elementRef.nativeElement.contains(document.activeElement)) {
+ return;
+ }
+
+ const focusCatcherContainer = document.querySelectorAll("[data-modal-focus-catcher-container='true']")[0];
+ if (focusCatcherContainer) {
+ (findAllFocusableElementsWithin(focusCatcherContainer as HTMLElement)[0] as HTMLElement).focus();
+ } else {
+ // Index 1 because the element at index 0 is the trigger button to open the modal
+ (findAllFocusableElementsWithin(this.elementRef.nativeElement)[1] as HTMLElement).focus();
+ }
+ });
+ });
+ }
+
close():void {
- this.open = false;
+ console.log('close');
+ this.teleportationService.clear();
+
+ this._opened = false;
+ document.body.removeEventListener('click', this.close);
+ document.body.removeEventListener('keydown', this.onEscape);
+ document.body.removeEventListener('scroll', this.repositionAnchor);
+ window.removeEventListener('resize', this.onResizeDebounced);
+ window.removeEventListener('orientationchange', this.onResizeDebounced);
+
+ this.closed.emit();
}
- private closeEventListener = this.close.bind(this) as () => void;
+ ngOnDestroy():void {
+ document.body.removeEventListener('click', this.close);
+ document.body.removeEventListener('keydown', this.onEscape);
+ document.body.removeEventListener('scroll', this.repositionAnchor);
+ window.removeEventListener('resize', this.onResizeDebounced);
+ window.removeEventListener('orientationchange', this.onResizeDebounced);
+ }
onBodyClick(e:MouseEvent):void {
// We stop propagation here so that clicks inside the body do not
@@ -157,42 +203,38 @@ export class SpotDropModalComponent implements OnDestroy {
}
private recalculateAlignment(): void {
- const clippingParent = findClippingParent(this.elementRef.nativeElement);
- const parentRect = clippingParent.getBoundingClientRect();
+ const anchor = document.getElementById(this.id);
+ if (!anchor) { return; }
+
+ const modalBody = anchor.querySelector('.spot-drop-modal--body');
+ if (!modalBody) { return; }
const alignments = Object.values(SpotDropAlignmentOption) as SpotDropAlignmentOption[];
const index = alignments.indexOf(this.alignment);
- const possibleAlignments = [
+ const possibleAlignment = [
...alignments.splice(index),
...alignments.splice(0, index),
- ].filter((alignment:SpotDropAlignmentOption) => {
- this.modalBody.nativeElement.classList.remove(this.alignmentClass);
+ ].find((alignment:SpotDropAlignmentOption) => {
+ modalBody.classList.remove(this.alignmentClass);
this.calculatedAlignment = alignment;
- this.modalBody.nativeElement.classList.add(this.alignmentClass);
- const rect = this.modalBody.nativeElement.getBoundingClientRect();
+ modalBody.classList.add(this.alignmentClass);
+ const rect = modalBody.getBoundingClientRect();
- const spaceOnLeft = parentRect.left <= rect.left;
- const spaceOnRight = parentRect.right >= rect.right;
- const spaceOnTop = parentRect.top <= rect.top;
- const spaceOnBottom = parentRect.bottom >= rect.bottom;
+ const spaceOnLeft = rect.left >= 0;
+ const spaceOnRight = rect.right <= window.innerWidth;
+ const spaceOnTop = rect.top >= 0;
+ const spaceOnBottom = rect.bottom <= window.innerHeight;
return spaceOnLeft && spaceOnRight && spaceOnTop && spaceOnBottom;
});
- if (possibleAlignments.length) {
- this.calculatedAlignment = possibleAlignments[0];
+ if (possibleAlignment) {
+ this.calculatedAlignment = possibleAlignment;
} else {
this.calculatedAlignment = this.alignment;
}
- this.cdRef.markForCheck();
- }
-
- ngOnDestroy():void {
- document.body.removeEventListener('click', this.closeEventListener);
- document.body.removeEventListener('keydown', this.escapeListener);
- window.removeEventListener('resize', this.appHeightListener);
- window.removeEventListener('orientationchange', this.appHeightListener);
+ this.cdRef.detectChanges();
}
private onEscape = (evt:KeyboardEvent) => {
@@ -201,10 +243,24 @@ export class SpotDropModalComponent implements OnDestroy {
}
};
- private escapeListener = this.onEscape.bind(this) as () => void;
+ private onResize():void {
+ this.updateAppHeight();
+ this.repositionAnchor();
+ this.recalculateAlignment();
+ }
- private appHeightListener = () => {
+ private onResizeDebounced = debounce(this.onResize.bind(this), 10);
+
+ private updateAppHeight = () => {
const doc = document.documentElement;
doc.style.setProperty('--app-height', `${window.innerHeight}px`);
};
+
+ private repositionAnchor():void {
+ const elementRect = (this.elementRef.nativeElement as HTMLElement).getBoundingClientRect();
+ this.anchorStyles.top = `${elementRect.top}px`;
+ this.anchorStyles.left = `${elementRect.left}px`;
+ this.anchorStyles.width = `${elementRect.width}px`;
+ this.anchorStyles.height = `${elementRect.height}px`;
+ }
}
diff --git a/frontend/src/app/spot/components/drop-modal/stories/DropModalList.component.html b/frontend/src/app/spot/components/drop-modal/stories/DropModalList.component.html
index 282f436ca98..e94783b3a1d 100644
--- a/frontend/src/app/spot/components/drop-modal/stories/DropModalList.component.html
+++ b/frontend/src/app/spot/components/drop-modal/stories/DropModalList.component.html
@@ -1,5 +1,5 @@
<spot-drop-modal
- [open]="dropModalOpen"
+ [opened]="dropModalOpen"
(closed)="close()"
[alignment]="alignment"
class="op-project-list-modal"
diff --git a/frontend/src/app/spot/spot.module.ts b/frontend/src/app/spot/spot.module.ts
index 8ab5413d3db..2645e63dd65 100644
--- a/frontend/src/app/spot/spot.module.ts
+++ b/frontend/src/app/spot/spot.module.ts
@@ -18,6 +18,8 @@ import { SpotFormFieldComponent } from './components/form-field/form-field.compo
import { SpotFormBindingDirective } from './components/form-field/form-binding.directive';
import { SpotBreadcrumbsComponent } from './components/breadcrumbs/breadcrumbs.component';
import { SpotSelectorFieldComponent } from './components/selector-field/selector-field.component';
+import { SpotDropModalTeleportationService } from './components/drop-modal/drop-modal-teleportation.service';
+import { SpotDropModalPortalComponent } from './components/drop-modal/drop-modal-portal.component';
@NgModule({
imports: [
@@ -29,6 +31,7 @@ import { SpotSelectorFieldComponent } from './components/selector-field/selector
providers: [
I18nService,
+ SpotDropModalTeleportationService,
],
declarations: [
@@ -39,6 +42,8 @@ import { SpotSelectorFieldComponent } from './components/selector-field/selector
SpotTextFieldComponent,
SpotFilterChipComponent,
SpotDropModalComponent,
+ SpotDropModalPortalComponent,
+ SpotDropModalPortalComponent,
SpotFormFieldComponent,
SpotFormBindingDirective,
SpotTooltipComponent,
@@ -53,6 +58,8 @@ import { SpotSelectorFieldComponent } from './components/selector-field/selector
SpotTextFieldComponent,
SpotFilterChipComponent,
SpotDropModalComponent,
+ SpotDropModalPortalComponent,
+ SpotDropModalPortalComponent,
SpotFormFieldComponent,
SpotFormBindingDirective,
SpotTooltipComponent,
diff --git a/frontend/src/app/spot/styles/sass/components/drop-modal.sass b/frontend/src/app/spot/styles/sass/components/drop-modal.sass
index 401138bca08..8a4387dd903 100644
--- a/frontend/src/app/spot/styles/sass/components/drop-modal.sass
+++ b/frontend/src/app/spot/styles/sass/components/drop-modal.sass
@@ -1,11 +1,14 @@
.spot-drop-modal
- position: relative
display: inline-flex
+ &--anchor
+ position: absolute
+ @include spot-z-index("drop-modal", 1)
+ pointer-events: none
+
&--body
$border-radius: 5px
- @include spot-z-index("drop-modal", 1)
pointer-events: all
opacity: 1
diff --git a/modules/github_integration/frontend/module/git-actions/git-actions.service.ts b/modules/github_integration/frontend/module/git-actions/git-actions.service.ts
index c06b82c7ed7..5bd8a60422a 100644
--- a/modules/github_integration/frontend/module/git-actions/git-actions.service.ts
+++ b/modules/github_integration/frontend/module/git-actions/git-actions.service.ts
@@ -30,9 +30,7 @@ import { Injectable } from '@angular/core';
import { WorkPackageResource } from "core-app/features/hal/resources/work-package-resource";
// probably not providable in root when we want to cache the formatter and set custom templates
-@Injectable({
- providedIn: 'root',
-})
+@Injectable({ providedIn: 'root' })
export class GitActionsService {
private sanitizeBranchString(str:string):string {
// See https://stackoverflow.com/a/3651867 for how these rules came in.