Skip to content

Commit 91cca28

Browse files
authored
fix: set dialog position on drag start (#10191)
1 parent 09e3fe2 commit 91cca28

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

packages/dialog/src/vaadin-dialog-draggable-mixin.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ export const DialogDraggableMixin = (superClass) =>
9595
window.addEventListener('touchmove', this._drag);
9696
if (this.$.overlay.$.overlay.style.position !== 'absolute') {
9797
const { top, left } = this._originalBounds;
98-
this.$.overlay.setBounds({ top, left });
98+
this.top = top;
99+
this.left = left;
99100
}
100101
}
101102
}

packages/dialog/test/draggable-resizable.test.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,9 @@ describe('draggable', () => {
422422
expect(Math.floor(draggedBounds.left)).to.be.eql(Math.floor(bounds.left + dx));
423423
});
424424

425-
it('should only change "position", "top", and "left" values on drag', () => {
425+
it('should only change "position", "top", and "left" values on drag', async () => {
426426
drag(content);
427+
await nextRender();
427428
const overlay = dialog.$.overlay.$.overlay;
428429
const style = overlay.style;
429430
expect(style.length).to.be.eql(3);
@@ -432,6 +433,18 @@ describe('draggable', () => {
432433
expect(style.left).to.be.ok;
433434
});
434435

436+
it('should assign "top", and "left" properties on drag start', async () => {
437+
const overlayBounds = dialog.$.overlay.getBounds();
438+
expect(dialog.left).to.be.undefined;
439+
expect(dialog.top).to.be.undefined;
440+
441+
dispatchMouseEvent(content, 'mousedown');
442+
await nextRender();
443+
444+
expect(dialog.left).to.be.equal(overlayBounds.left);
445+
expect(dialog.top).to.be.equal(overlayBounds.top);
446+
});
447+
435448
it('should drag and move dialog if mousedown on element with [class="draggable"] in another shadow root', async () => {
436449
drag(dialog.querySelector('internally-draggable').shadowRoot.querySelector('.draggable'));
437450
await nextRender();
@@ -514,7 +527,7 @@ describe('draggable', () => {
514527
expect(Math.floor(draggedBounds.left)).to.be.eql(Math.floor(bounds.left));
515528
});
516529

517-
it('should not drag dialog if mousedown on .resizer-container scrollbar', () => {
530+
it('should not drag dialog if mousedown on .resizer-container scrollbar', async () => {
518531
const boundsSpy = sinon.spy(dialog.$.overlay, 'setBounds');
519532
content.style.width = `${content.clientWidth * 4}px`;
520533
const scrollbarHeight = container.offsetHeight - container.clientHeight;
@@ -523,6 +536,7 @@ describe('draggable', () => {
523536
x: containerBounds.left + containerBounds.width / 2,
524537
y: containerBounds.top + containerBounds.height - scrollbarHeight / 2,
525538
});
539+
await nextRender();
526540
expect(boundsSpy.called).to.equal(!scrollbarHeight);
527541
});
528542

@@ -595,6 +609,22 @@ describe('draggable', () => {
595609
expect(detail.left).to.be.equal(dialog.left);
596610
});
597611

612+
it('should use correct coordinates in "dragged" event without moving the dialog during drag', async () => {
613+
const overlayBounds = dialog.$.overlay.getBounds();
614+
const onDragged = sinon.spy();
615+
dialog.addEventListener('dragged', onDragged);
616+
617+
dispatchMouseEvent(content, 'mousedown');
618+
await nextRender();
619+
dispatchMouseEvent(content, 'mouseup');
620+
await nextRender();
621+
622+
expect(onDragged.calledOnce).to.be.true;
623+
const { detail } = onDragged.args[0][0];
624+
expect(detail.top).to.be.equal(overlayBounds.top);
625+
expect(detail.left).to.be.equal(overlayBounds.left);
626+
});
627+
598628
it('should not set overlay max-width to none on drag', async () => {
599629
drag(container);
600630
await nextRender();

0 commit comments

Comments
 (0)