all files / popper/utils/ getBoundaries.js

88.46% Statements 23/26
80% Branches 8/10
100% Functions 1/1
88.46% Lines 23/26
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76                                          228× 228×     228× 79× 79×   79×       79× 79×   79×                     149× 124× 25×     25×       149× 107× 107× 107×       42×         228× 228× 228× 228×   228×    
import getOffsetParent from './getOffsetParent';
import getScrollParent from './getScrollParent';
import getParentNode from './getParentNode';
import getOffsetRect from './getOffsetRect';
import getPosition from './getPosition';
import getOffsetRectRelativeToCustomParent from './getOffsetRectRelativeToCustomParent';
import getTotalScroll from './getTotalScroll';
import isFixed from './isFixed';
import getWindowSizes from './getWindowSizes';
 
/**
 * Computed the boundaries limits and return them
 * @method
 * @memberof Popper.Utils
 * @param {Object} data - Object containing the property "offsets" generated by `_getOffsets`
 * @param {Number} padding - Boundaries padding
 * @param {Element} boundariesElement - Element used to define the boundaries
 * @returns {Object} Coordinates of the boundaries
 */
export default function getBoundaries(popper, padding, boundariesElement) {
    // NOTE: 1 DOM access here
    let boundaries = { top: 0, left: 0 };
    const offsetParent = getOffsetParent(popper);
 
    // Handle viewport case
    if (boundariesElement === 'viewport') {
        const { left, top } = getOffsetRect(offsetParent);
        const { clientWidth: width, clientHeight: height } = window.document.documentElement;
 
        Iif (getPosition(popper) === 'fixed') {
            boundaries.right = width;
            boundaries.bottom = height;
        } else {
            const scrollLeft = getTotalScroll(popper, 'left');
            const scrollTop = getTotalScroll(popper, 'top');
 
            boundaries = {
                top: 0 - top,
                right: width - left + scrollLeft,
                bottom: height - top + scrollTop,
                left: 0 - left,
            };
        }
    }
    // Handle other cases based on DOM element used as boundaries
    else {
        let boundariesNode;
        if (boundariesElement === 'scrollParent') {
            boundariesNode = getScrollParent(getParentNode(popper));
        } else Iif (boundariesElement === 'window') {
            boundariesNode = window.document.body;
        } else {
            boundariesNode = boundariesElement;
        }
 
        // In case of BODY, we need a different computation
        if (boundariesNode.nodeName === 'BODY') {
            const { height, width } = getWindowSizes();
            boundaries.right = width;
            boundaries.bottom = height;
        }
        // for all the other DOM elements, this one is good
        else {
            boundaries = getOffsetRectRelativeToCustomParent(boundariesNode, offsetParent, isFixed(popper));
        }
    }
 
    // Add paddings
    boundaries.left += padding;
    boundaries.top += padding;
    boundaries.right -= padding;
    boundaries.bottom -= padding;
 
    return boundaries;
}