| 1 |
import computeAutoPlacement from '../utils/computeAutoPlacement'; |
| 2 |
import getReferenceOffsets from '../utils/getReferenceOffsets'; |
| 3 |
import getPopperOffsets from '../utils/getPopperOffsets'; |
| 4 |
import runModifiers from '../utils/runModifiers'; |
| 5 |
|
| 6 |
/** |
| 7 |
* Updates the position of the popper, computing the new offsets and applying |
| 8 |
* the new style.<br /> |
| 9 |
* Prefer `scheduleUpdate` over `update` because of performance reasons. |
| 10 |
* @method |
| 11 |
* @memberof Popper |
| 12 |
*/ |
| 13 |
export default function update() { |
| 14 |
// if popper is destroyed, don't perform any further update |
| 15 |
if (this.state.isDestroyed) { |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
let data = { |
| 20 |
instance: this, |
| 21 |
styles: {}, |
| 22 |
arrowStyles: {}, |
| 23 |
attributes: {}, |
| 24 |
flipped: false, |
| 25 |
offsets: {}, |
| 26 |
}; |
| 27 |
|
| 28 |
// compute reference element offsets |
| 29 |
data.offsets.reference = getReferenceOffsets( |
| 30 |
this.state, |
| 31 |
this.popper, |
| 32 |
this.reference, |
| 33 |
this.options.positionFixed |
| 34 |
); |
| 35 |
|
| 36 |
// compute auto placement, store placement inside the data object, |
| 37 |
// modifiers will be able to edit `placement` if needed |
| 38 |
// and refer to originalPlacement to know the original value |
| 39 |
data.placement = computeAutoPlacement( |
| 40 |
this.options.placement, |
| 41 |
data.offsets.reference, |
| 42 |
this.popper, |
| 43 |
this.reference, |
| 44 |
this.options.modifiers.flip.boundariesElement, |
| 45 |
this.options.modifiers.flip.padding |
| 46 |
); |
| 47 |
|
| 48 |
// store the computed placement inside `originalPlacement` |
| 49 |
data.originalPlacement = data.placement; |
| 50 |
|
| 51 |
data.positionFixed = this.options.positionFixed; |
| 52 |
|
| 53 |
// compute the popper offsets |
| 54 |
data.offsets.popper = getPopperOffsets( |
| 55 |
this.popper, |
| 56 |
data.offsets.reference, |
| 57 |
data.placement |
| 58 |
); |
| 59 |
|
| 60 |
data.offsets.popper.position = this.options.positionFixed |
| 61 |
? 'fixed' |
| 62 |
: 'absolute'; |
| 63 |
|
| 64 |
// run the modifiers |
| 65 |
data = runModifiers(this.modifiers, data); |
| 66 |
|
| 67 |
// the first `update` will call `onCreate` callback |
| 68 |
// the other ones will call `onUpdate` callback |
| 69 |
if (!this.state.isCreated) { |
| 70 |
this.state.isCreated = true; |
| 71 |
this.options.onCreate(data); |
| 72 |
} else { |
| 73 |
this.options.onUpdate(data); |
| 74 |
} |
| 75 |
} |