| 1 |
import setStyles from '../utils/setStyles'; |
| 2 |
import setAttributes from '../utils/setAttributes'; |
| 3 |
import getReferenceOffsets from '../utils/getReferenceOffsets'; |
| 4 |
import computeAutoPlacement from '../utils/computeAutoPlacement'; |
| 5 |
|
| 6 |
/** |
| 7 |
* @function |
| 8 |
* @memberof Modifiers |
| 9 |
* @argument {Object} data - The data object generated by `update` method |
| 10 |
* @argument {Object} data.styles - List of style properties - values to apply to popper element |
| 11 |
* @argument {Object} data.attributes - List of attribute properties - values to apply to popper element |
| 12 |
* @argument {Object} options - Modifiers configuration and options |
| 13 |
* @returns {Object} The same data object |
| 14 |
*/ |
| 15 |
export default function applyStyle(data) { |
| 16 |
// any property present in `data.styles` will be applied to the popper, |
| 17 |
// in this way we can make the 3rd party modifiers add custom styles to it |
| 18 |
// Be aware, modifiers could override the properties defined in the previous |
| 19 |
// lines of this modifier! |
| 20 |
setStyles(data.instance.popper, data.styles); |
| 21 |
|
| 22 |
// any property present in `data.attributes` will be applied to the popper, |
| 23 |
// they will be set as HTML attributes of the element |
| 24 |
setAttributes(data.instance.popper, data.attributes); |
| 25 |
|
| 26 |
// if arrowElement is defined and arrowStyles has some properties |
| 27 |
if (data.arrowElement && Object.keys(data.arrowStyles).length) { |
| 28 |
setStyles(data.arrowElement, data.arrowStyles); |
| 29 |
} |
| 30 |
|
| 31 |
return data; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Set the x-placement attribute before everything else because it could be used |
| 36 |
* to add margins to the popper margins needs to be calculated to get the |
| 37 |
* correct popper offsets. |
| 38 |
* @method |
| 39 |
* @memberof Popper.modifiers |
| 40 |
* @param {HTMLElement} reference - The reference element used to position the popper |
| 41 |
* @param {HTMLElement} popper - The HTML element used as popper |
| 42 |
* @param {Object} options - Popper.js options |
| 43 |
*/ |
| 44 |
export function applyStyleOnLoad( |
| 45 |
reference, |
| 46 |
popper, |
| 47 |
options, |
| 48 |
modifierOptions, |
| 49 |
state |
| 50 |
) { |
| 51 |
// compute reference element offsets |
| 52 |
const referenceOffsets = getReferenceOffsets(state, popper, reference, options.positionFixed); |
| 53 |
|
| 54 |
// compute auto placement, store placement inside the data object, |
| 55 |
// modifiers will be able to edit `placement` if needed |
| 56 |
// and refer to originalPlacement to know the original value |
| 57 |
const placement = computeAutoPlacement( |
| 58 |
options.placement, |
| 59 |
referenceOffsets, |
| 60 |
popper, |
| 61 |
reference, |
| 62 |
options.modifiers.flip.boundariesElement, |
| 63 |
options.modifiers.flip.padding |
| 64 |
); |
| 65 |
|
| 66 |
popper.setAttribute('x-placement', placement); |
| 67 |
|
| 68 |
// Apply `position` to popper before anything else because |
| 69 |
// without the position applied we can't guarantee correct computations |
| 70 |
setStyles(popper, { position: options.positionFixed ? 'fixed' : 'absolute' }); |
| 71 |
|
| 72 |
return options; |
| 73 |
} |