Skip to content

Commit 4201a30

Browse files
webdevanvalorkin
authored andcommitted
fix(positioning): browser rounding width/height bug (#4328)
fix #4322
1 parent cc9d0e3 commit 4201a30

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

src/positioning/ng-positioning.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,6 @@ export class Positioning {
9494
? this.offset(hostElement, false)
9595
: this.position(hostElement, false);
9696
const targetElStyles = this.getAllStyles(targetElement);
97-
const shiftWidth: any = {
98-
left: hostElPosition.left,
99-
center:
100-
hostElPosition.left +
101-
hostElPosition.width / 2 -
102-
targetElement.offsetWidth / 2,
103-
right: hostElPosition.left + hostElPosition.width
104-
};
105-
const shiftHeight: any = {
106-
top: hostElPosition.top,
107-
center:
108-
hostElPosition.top +
109-
hostElPosition.height / 2 -
110-
targetElement.offsetHeight / 2,
111-
bottom: hostElPosition.top + hostElPosition.height
112-
};
11397
const targetElBCR = targetElement.getBoundingClientRect();
11498
let placementPrimary = placement.split(' ')[0] || 'top';
11599
const placementSecondary = placement.split(' ')[1] || 'center';
@@ -123,6 +107,23 @@ export class Positioning {
123107
right: targetElBCR.width || targetElement.offsetWidth
124108
};
125109

110+
const shiftHeight: any = {
111+
top: hostElPosition.top,
112+
center:
113+
hostElPosition.top +
114+
hostElPosition.height / 2 -
115+
targetElPosition.height / 2,
116+
bottom: hostElPosition.top + hostElPosition.height
117+
};
118+
const shiftWidth: any = {
119+
left: hostElPosition.left,
120+
center:
121+
hostElPosition.left +
122+
hostElPosition.width / 2 -
123+
targetElPosition.width / 2,
124+
right: hostElPosition.left + hostElPosition.width
125+
};
126+
126127
if (placementPrimary === 'auto') {
127128
let newPlacementPrimary = this.autoPosition(
128129
targetElPosition,
@@ -144,10 +145,10 @@ export class Positioning {
144145
case 'top':
145146
targetElPosition.top =
146147
hostElPosition.top -
147-
(targetElement.offsetHeight +
148+
(targetElPosition.height +
148149
parseFloat(targetElStyles.marginBottom));
149150
targetElPosition.bottom +=
150-
hostElPosition.top - targetElement.offsetHeight;
151+
hostElPosition.top - targetElPosition.height;
151152
targetElPosition.left = shiftWidth[placementSecondary];
152153
targetElPosition.right += shiftWidth[placementSecondary];
153154
break;
@@ -162,9 +163,9 @@ export class Positioning {
162163
targetElPosition.bottom += shiftHeight[placementSecondary];
163164
targetElPosition.left =
164165
hostElPosition.left -
165-
(targetElement.offsetWidth + parseFloat(targetElStyles.marginRight));
166+
(targetElPosition.width + parseFloat(targetElStyles.marginRight));
166167
targetElPosition.right +=
167-
hostElPosition.left - targetElement.offsetWidth;
168+
hostElPosition.left - targetElPosition.width;
168169
break;
169170
case 'right':
170171
targetElPosition.top = shiftHeight[placementSecondary];
@@ -190,28 +191,28 @@ export class Positioning {
190191
) {
191192
if (
192193
(!preferredPosition || preferredPosition === 'right') &&
193-
targetElPosition.left + hostElPosition.left - targetElement.offsetWidth <
194+
targetElPosition.left + hostElPosition.left - targetElPosition.width <
194195
0
195196
) {
196197
return 'right';
197198
} else if (
198199
(!preferredPosition || preferredPosition === 'top') &&
199200
targetElPosition.bottom +
200201
hostElPosition.bottom +
201-
targetElement.offsetHeight >
202+
targetElPosition.height >
202203
window.innerHeight
203204
) {
204205
return 'top';
205206
} else if (
206207
(!preferredPosition || preferredPosition === 'bottom') &&
207-
targetElPosition.top + hostElPosition.top - targetElement.offsetHeight < 0
208+
targetElPosition.top + hostElPosition.top - targetElPosition.height < 0
208209
) {
209210
return 'bottom';
210211
} else if (
211212
(!preferredPosition || preferredPosition === 'left') &&
212213
targetElPosition.right +
213214
hostElPosition.right +
214-
targetElement.offsetWidth >
215+
targetElPosition.width >
215216
window.innerWidth
216217
) {
217218
return 'left';

0 commit comments

Comments
 (0)