Skip to content

Commit 28e6eae

Browse files
authored
Merge pull request #90 from ad1tyayadav/fix/drag-modal-position
fix: modal drag jump issue
2 parents cff5d81 + c17eb92 commit 28e6eae

File tree

1 file changed

+68
-27
lines changed

1 file changed

+68
-27
lines changed

src/components/calibration/ConfigModal.vue

Lines changed: 68 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@ export default {
4343
data() {
4444
return {
4545
aDialog: false,
46-
dragging: false,
46+
isDragging: false,
47+
hasMoved: false,
4748
initialMouseX: 0,
4849
initialMouseY: 0,
49-
initialClientX: 200,
50-
initialClientY: 100,
51-
deltaX: 0,
52-
deltaY: 0,
53-
startTime: 0,
50+
initialCardX: 0,
51+
initialCardY: 0,
52+
cardWidth: 0,
5453
showInfo: true
5554
}
5655
},
@@ -60,6 +59,20 @@ export default {
6059
},
6160
aDialog(newAdialog) {
6261
this.$emit('close', newAdialog);
62+
63+
// Reset positioning when modal closes
64+
if (!newAdialog && this.$refs.draggableCard) {
65+
this.$nextTick(() => {
66+
const cardEl = this.$refs.draggableCard.$el;
67+
if (cardEl) {
68+
cardEl.style.position = '';
69+
cardEl.style.left = '';
70+
cardEl.style.top = '';
71+
cardEl.style.width = '';
72+
cardEl.style.margin = '';
73+
}
74+
});
75+
}
6376
},
6477
},
6578
computed: {
@@ -87,40 +100,68 @@ export default {
87100
this.$emit('save');
88101
},
89102
startDrag(event) {
90-
this.showInfo = false
91-
this.dragging = true;
92-
this.startTime = new Date();
103+
this.showInfo = false;
104+
this.isDragging = true;
105+
this.hasMoved = false;
106+
107+
// Store initial mouse position
93108
this.initialMouseX = event.clientX;
94109
this.initialMouseY = event.clientY;
95-
const rect = this.$refs.draggableCard.$el.getBoundingClientRect();
96-
this.initialClientX = rect.left - 20;
97-
this.initialClientY = rect.top - 240;
98110
99111
window.addEventListener("mousemove", this.handleDrag);
100112
window.addEventListener("mouseup", this.endDrag);
113+
114+
// Prevent text selection while dragging
115+
event.preventDefault();
101116
},
102117
handleDrag(event) {
103-
if (this.dragging) {
104-
this.deltaX = event.clientX - this.initialMouseX;
105-
this.deltaY = event.clientY - this.initialMouseY;
106-
this.$refs.draggableCard.$el.style.left = this.initialClientX + this.deltaX + "px";
107-
this.$refs.draggableCard.$el.style.top = this.initialClientY + this.deltaY + "px";
118+
if (!this.isDragging) return;
119+
120+
const deltaX = event.clientX - this.initialMouseX;
121+
const deltaY = event.clientY - this.initialMouseY;
122+
123+
// Only start actual dragging if moved more than 5 pixels (helps distinguish clicks from drags)
124+
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
125+
if (distance < 5) return;
126+
127+
const cardEl = this.$refs.draggableCard.$el;
128+
129+
// On first move, set up the card for dragging
130+
if (!this.hasMoved) {
131+
this.hasMoved = true;
132+
const rect = cardEl.getBoundingClientRect();
133+
this.initialCardX = rect.left;
134+
this.initialCardY = rect.top;
135+
this.cardWidth = rect.width;
136+
137+
// Apply fixed positioning
138+
cardEl.style.position = 'fixed';
139+
cardEl.style.width = this.cardWidth + 'px';
140+
cardEl.style.margin = '0';
141+
cardEl.style.left = this.initialCardX + 'px';
142+
cardEl.style.top = this.initialCardY + 'px';
108143
}
144+
145+
// Update position based on mouse movement
146+
cardEl.style.left = (this.initialCardX + deltaX) + 'px';
147+
cardEl.style.top = (this.initialCardY + deltaY) + 'px';
109148
},
110149
endDrag() {
111-
if (this.dragging) {
112-
this.dragging = false;
113-
if (this.deltaX == 0 && this.deltaY == 0) {
114-
this.handleClick()
115-
}
116-
this.deltaX = 0
117-
this.deltaY = 0
150+
window.removeEventListener("mousemove", this.handleDrag);
151+
window.removeEventListener("mouseup", this.endDrag);
152+
153+
this.isDragging = false;
154+
155+
// If we didn't move, treat it as a click (not a drag)
156+
if (!this.hasMoved) {
157+
this.handleClick();
118158
}
159+
160+
this.hasMoved = false;
119161
},
120162
handleClick(event) {
121-
if (!this.dragging) {
122-
this.$emit('click', event);
123-
}
163+
// This is called when user clicks without dragging
164+
this.$emit('click', event);
124165
},
125166
closeinfo() {
126167
this.showInfo = false;

0 commit comments

Comments
 (0)