| 1 |
import getOuterSizes from './getOuterSizes'; |
| 2 |
import getOppositePlacement from './getOppositePlacement'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Get offsets to the popper |
| 6 |
* @method |
| 7 |
* @memberof Popper.Utils |
| 8 |
* @param {Object} position - CSS position the Popper will get applied |
| 9 |
* @param {HTMLElement} popper - the popper element |
| 10 |
* @param {Object} referenceOffsets - the reference offsets (the popper will be relative to this) |
| 11 |
* @param {String} placement - one of the valid placement options |
| 12 |
* @returns {Object} popperOffsets - An object containing the offsets which will be applied to the popper |
| 13 |
*/ |
| 14 |
export default function getPopperOffsets(popper, referenceOffsets, placement) { |
| 15 |
placement = placement.split('-')[0]; |
| 16 |
|
| 17 |
// Get popper node sizes |
| 18 |
const popperRect = getOuterSizes(popper); |
| 19 |
|
| 20 |
// Add position, width and height to our offsets object |
| 21 |
const popperOffsets = { |
| 22 |
width: popperRect.width, |
| 23 |
height: popperRect.height, |
| 24 |
}; |
| 25 |
|
| 26 |
// depending by the popper placement we have to compute its offsets slightly differently |
| 27 |
const isHoriz = ['right', 'left'].indexOf(placement) !== -1; |
| 28 |
const mainSide = isHoriz ? 'top' : 'left'; |
| 29 |
const secondarySide = isHoriz ? 'left' : 'top'; |
| 30 |
const measurement = isHoriz ? 'height' : 'width'; |
| 31 |
const secondaryMeasurement = !isHoriz ? 'height' : 'width'; |
| 32 |
|
| 33 |
popperOffsets[mainSide] = |
| 34 |
referenceOffsets[mainSide] + |
| 35 |
referenceOffsets[measurement] / 2 - |
| 36 |
popperRect[measurement] / 2; |
| 37 |
if (placement === secondarySide) { |
| 38 |
popperOffsets[secondarySide] = |
| 39 |
referenceOffsets[secondarySide] - popperRect[secondaryMeasurement]; |
| 40 |
} else { |
| 41 |
popperOffsets[secondarySide] = |
| 42 |
referenceOffsets[getOppositePlacement(secondarySide)]; |
| 43 |
} |
| 44 |
|
| 45 |
return popperOffsets; |
| 46 |
} |