all files / popper/modifiers/ hide.js

56.25% Statements 9/16
58.33% Branches 7/12
100% Functions 2/2
61.54% Lines 8/13
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                            103×         103× 309×   103×                         103×   103× 103×     103×    
import isModifierRequired from '../utils/isModifierRequired';
import find from '../utils/find';
 
/**
 * Modifier used to hide the popper when its reference element is outside of the
 * popper boundaries. It will set an x-hidden attribute which can be used to hide
 * the popper when its reference is out of boundaries.
 * @method
 * @memberof Modifiers
 * @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 hide(data) {
    Iif (!isModifierRequired(data.instance.modifiers, 'hide', 'preventOverflow')) {
        console.warn('WARNING: preventOverflow modifier is required by hide modifier in order to work, be sure to include it before hide!');
        return data;
    }
 
    const refRect = data.offsets.reference;
    const bound = find(data.instance.modifiers, (modifier) => modifier.name ==='preventOverflow').boundaries;
 
    Iif (
        refRect.bottom < bound.top ||
        refRect.left > bound.right ||
        refRect.top > bound.bottom ||
        refRect.right < bound.left
    ) {
        // Avoid unnecessary DOM access if visibility hasn't changed
        if (data.hide === true) { return data; }
 
        data.hide = true;
        data.attributes['x-out-of-boundaries'] = '';
    } else {
        // Avoid unnecessary DOM access if visibility hasn't changed
        Iif (data.hide === false) { return data; }
 
        data.hide = false;
        data.attributes['x-out-of-boundaries'] = false;
    }
 
    return data;
}