Skip to content
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
16 changes: 7 additions & 9 deletions packages/dialog/src/vaadin-dialog-draggable-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,14 @@ export const DialogDraggableMixin = (superClass) =>
let left = this._originalBounds.left + (event.pageX - this._originalMouseCoords.left);

if (this.keepInViewport) {
// Constrain the dialog position so that it stays within the overlay host bounds,
// respecting the `--vaadin-overlay-viewport-inset` (offset from the viewport edges).
const { width, height } = this._originalBounds;
// Get the overlay container's position to account for its offset from the viewport
const containerBounds = this.$.overlay.getBoundingClientRect();
// Calculate bounds so the dialog's visual edges stay within the viewport
const minLeft = -containerBounds.left;
const maxLeft = window.innerWidth - containerBounds.left - width;
const minTop = -containerBounds.top;
const maxTop = window.innerHeight - containerBounds.top - height;
left = Math.max(minLeft, Math.min(left, maxLeft));
top = Math.max(minTop, Math.min(top, maxTop));
const overlayHostBounds = this.$.overlay.getBoundingClientRect();
const maxLeft = overlayHostBounds.right - overlayHostBounds.left - width;
const maxTop = overlayHostBounds.bottom - overlayHostBounds.top - height;
left = Math.max(0, Math.min(left, maxLeft));
top = Math.max(0, Math.min(top, maxTop));
}

this.top = top;
Expand Down
14 changes: 10 additions & 4 deletions packages/dialog/test/draggable-resizable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,8 @@ describe('draggable', () => {
});

describe('keepInViewport', () => {
let overlayHostBounds;

// Helper to drag to absolute coordinates within the viewport
function dragTo(target, toX, toY) {
const targetBounds = target.getBoundingClientRect();
Expand All @@ -700,6 +702,10 @@ describe('draggable', () => {
}

beforeEach(async () => {
// Re-enable inset on overlay host to verify that keepInViewport respects `--vaadin-overlay-viewport-inset`
const overlayHost = dialog.$.overlay;
overlayHost.style.inset = '10px';
overlayHostBounds = overlayHost.getBoundingClientRect();
dialog.keepInViewport = true;
await nextUpdate(dialog);
});
Expand All @@ -709,31 +715,31 @@ describe('draggable', () => {
await nextRender();

const draggedBounds = container.getBoundingClientRect();
expect(Math.floor(draggedBounds.left)).to.be.closeTo(0, 1);
expect(Math.floor(draggedBounds.left)).to.be.closeTo(overlayHostBounds.left, 1);
});

it('should not drag dialog past top viewport edge', async () => {
dragTo(container, bounds.left + bounds.width / 2, 0);
await nextRender();

const draggedBounds = container.getBoundingClientRect();
expect(Math.floor(draggedBounds.top)).to.closeTo(0, 1);
expect(Math.floor(draggedBounds.top)).to.closeTo(overlayHostBounds.top, 1);
});

it('should not drag dialog past right viewport edge', async () => {
dragTo(container, window.innerWidth, bounds.top + bounds.height / 2);
await nextRender();

const draggedBounds = container.getBoundingClientRect();
expect(Math.floor(draggedBounds.right)).to.closeTo(window.innerWidth, 1);
expect(Math.floor(draggedBounds.right)).to.closeTo(overlayHostBounds.right, 1);
});

it('should not drag dialog past bottom viewport edge', async () => {
dragTo(container, bounds.left + bounds.width / 2, window.innerHeight);
await nextRender();

const draggedBounds = container.getBoundingClientRect();
expect(Math.floor(draggedBounds.bottom)).to.closeTo(window.innerHeight, 1);
expect(Math.floor(draggedBounds.bottom)).to.closeTo(overlayHostBounds.bottom, 1);
});

it('should allow normal dragging within viewport', async () => {
Expand Down