| 1 |
/** |
| 2 |
* @function |
| 3 |
* @memberof Popper.Utils |
| 4 |
* @argument {Object} data - The data object generated by `update` method |
| 5 |
* @argument {Boolean} shouldRound - If the offsets should be rounded at all |
| 6 |
* @returns {Object} The popper's position offsets rounded |
| 7 |
* |
| 8 |
* The tale of pixel-perfect positioning. It's still not 100% perfect, but as |
| 9 |
* good as it can be within reason. |
| 10 |
* Discussion here: https://github.com/FezVrasta/popper.js/pull/715 |
| 11 |
* |
| 12 |
* Low DPI screens cause a popper to be blurry if not using full pixels (Safari |
| 13 |
* as well on High DPI screens). |
| 14 |
* |
| 15 |
* Firefox prefers no rounding for positioning and does not have blurriness on |
| 16 |
* high DPI screens. |
| 17 |
* |
| 18 |
* Only horizontal placement and left/right values need to be considered. |
| 19 |
*/ |
| 20 |
export default function getRoundedOffsets(data, shouldRound) { |
| 21 |
const { popper, reference } = data.offsets; |
| 22 |
const { round, floor } = Math; |
| 23 |
const noRound = v => v; |
| 24 |
|
| 25 |
const referenceWidth = round(reference.width); |
| 26 |
const popperWidth = round(popper.width); |
| 27 |
|
| 28 |
const isVertical = ['left', 'right'].indexOf(data.placement) !== -1; |
| 29 |
const isVariation = data.placement.indexOf('-') !== -1; |
| 30 |
const sameWidthParity = referenceWidth % 2 === popperWidth % 2; |
| 31 |
const bothOddWidth = referenceWidth % 2 === 1 && popperWidth % 2 === 1; |
| 32 |
|
| 33 |
const horizontalToInteger = !shouldRound |
| 34 |
? noRound |
| 35 |
: isVertical || isVariation || sameWidthParity |
| 36 |
? round |
| 37 |
: floor; |
| 38 |
const verticalToInteger = !shouldRound ? noRound : round; |
| 39 |
|
| 40 |
return { |
| 41 |
left: horizontalToInteger( |
| 42 |
bothOddWidth && !isVariation && shouldRound |
| 43 |
? popper.left - 1 |
| 44 |
: popper.left |
| 45 |
), |
| 46 |
top: verticalToInteger(popper.top), |
| 47 |
bottom: verticalToInteger(popper.bottom), |
| 48 |
right: horizontalToInteger(popper.right), |
| 49 |
}; |
| 50 |
} |