all files / popper/utils/ computeAutoPlacement.js

100% Statements 8/8
75% Branches 3/4
100% Functions 2/2
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31                        210× 206×                   20×      
import getBoundaries from '../utils/getBoundaries';
 
/**
 * Utility used to transform the `auto` placement to the placement with more
 * available space.
 * @method
 * @memberof Popper.Utils
 * @argument {Object} data - The data object generated by update method
 * @argument {Object} options - Modifiers configuration and options
 * @returns {Object} The data object, properly modified
 */
export default function computeAutoPlacement(placement, refRect, popper) {
    if (placement.indexOf('auto') === -1) {
        return placement;
    }
 
    const boundaries = getBoundaries(popper, 0, 'scrollParent');
 
    const sides = {
        top: refRect.top - boundaries.top,
        right: boundaries.right - refRect.right,
        bottom: boundaries.bottom - refRect.bottom,
        left: refRect.left - boundaries.left,
    };
 
    const computedPlacement = Object.keys(sides).sort((a, b) => sides[b] - sides[a])[0];
    const variation = placement.split('-')[1];
 
    return computedPlacement + (variation ? `-${variation}` : '');
}