Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

fix: clamp popup/dropdown positions to viewport bounds

esengine55dcad438e69
diff --git a/editor/src/panels/AddComponentPopup.ts b/editor/src/panels/AddComponentPopup.ts
index 99857503d..455e75ff8 100644
--- a/editor/src/panels/AddComponentPopup.ts
+++ b/editor/src/panels/AddComponentPopup.ts
@@ -484,17 +484,30 @@ export function showAddComponentPopup(
const panelHeight = container.offsetHeight;
const spaceAbove = rect.top;
const spaceBelow = window.innerHeight - rect.bottom;
+ const gap = 4;
+ const margin = 5;
- container.style.left = `${rect.left}px`;
- container.style.width = `${Math.max(rect.width, 320)}px`;
+ const width = Math.max(rect.width, 320);
+ let left = rect.left;
+ if (left + width > window.innerWidth - margin) {
+ left = window.innerWidth - width - margin;
+ }
+ if (left < margin) {
+ left = margin;
+ }
+
+ container.style.left = `${left}px`;
+ container.style.width = `${width}px`;
if (spaceBelow >= panelHeight || spaceBelow >= spaceAbove) {
- container.style.top = `${rect.bottom + 4}px`;
+ container.style.top = `${rect.bottom + gap}px`;
container.style.bottom = '';
+ container.style.maxHeight = `${spaceBelow - gap - margin}px`;
container.classList.remove('es-popup-above');
} else {
- container.style.bottom = `${window.innerHeight - rect.top + 4}px`;
+ container.style.bottom = `${window.innerHeight - rect.top + gap}px`;
container.style.top = '';
+ container.style.maxHeight = `${spaceAbove - gap - margin}px`;
container.classList.add('es-popup-above');
}
};
diff --git a/editor/src/panels/timeline/TimelineAddTrackWizard.ts b/editor/src/panels/timeline/TimelineAddTrackWizard.ts
index 15af17dd8..1d722b1ab 100644
--- a/editor/src/panels/timeline/TimelineAddTrackWizard.ts
+++ b/editor/src/panels/timeline/TimelineAddTrackWizard.ts
@@ -76,10 +76,9 @@ export class TimelineAddTrackWizard {
this.el_ = document.createElement('div');
this.el_.className = 'es-timeline-dropdown';
- const rect = anchor.getBoundingClientRect();
+ this.anchor_ = anchor;
this.el_.style.position = 'fixed';
- this.el_.style.left = `${rect.left}px`;
- this.el_.style.top = `${rect.bottom + 2}px`;
+ this.el_.style.visibility = 'hidden';
document.body.appendChild(this.el_);
this.renderStep();
@@ -101,8 +100,11 @@ export class TimelineAddTrackWizard {
this.el_.remove();
this.el_ = null;
}
+ this.anchor_ = null;
}
+ private anchor_: HTMLElement | null = null;
+
private renderStep(): void {
if (!this.el_) return;
@@ -112,6 +114,46 @@ export class TimelineAddTrackWizard {
case 'component': this.renderComponentStep(); break;
case 'property': this.renderPropertyStep(); break;
}
+
+ if (this.anchor_) {
+ this.clampToViewport(this.anchor_);
+ }
+ }
+
+ private clampToViewport(anchor: HTMLElement): void {
+ if (!this.el_) return;
+
+ const anchorRect = anchor.getBoundingClientRect();
+ const elRect = this.el_.getBoundingClientRect();
+ const gap = 2;
+ const margin = 5;
+
+ const spaceBelow = window.innerHeight - anchorRect.bottom - gap;
+ const spaceAbove = anchorRect.top - gap;
+
+ let top: number;
+ let maxHeight: number;
+
+ if (spaceBelow >= elRect.height || spaceBelow >= spaceAbove) {
+ top = anchorRect.bottom + gap;
+ maxHeight = window.innerHeight - top - margin;
+ } else {
+ top = anchorRect.top - gap - Math.min(elRect.height, spaceAbove);
+ maxHeight = spaceAbove;
+ }
+
+ let left = anchorRect.left;
+ if (left + elRect.width > window.innerWidth - margin) {
+ left = window.innerWidth - elRect.width - margin;
+ }
+ if (left < margin) {
+ left = margin;
+ }
+
+ this.el_.style.left = `${left}px`;
+ this.el_.style.top = `${top}px`;
+ this.el_.style.maxHeight = `${Math.max(maxHeight, 100)}px`;
+ this.el_.style.visibility = '';
}
private renderTypeStep(): void {