| 1 |
import isModifierRequired from '../utils/isModifierRequired'; |
| 2 |
import find from '../utils/find'; |
| 3 |
|
| 4 |
/** |
| 5 |
* @function |
| 6 |
* @memberof Modifiers |
| 7 |
* @argument {Object} data - The data object generated by update method |
| 8 |
* @argument {Object} options - Modifiers configuration and options |
| 9 |
* @returns {Object} The data object, properly modified |
| 10 |
*/ |
| 11 |
export default function hide(data) { |
| 12 |
if (!isModifierRequired(data.instance.modifiers, 'hide', 'preventOverflow')) { |
| 13 |
return data; |
| 14 |
} |
| 15 |
|
| 16 |
const refRect = data.offsets.reference; |
| 17 |
const bound = find( |
| 18 |
data.instance.modifiers, |
| 19 |
modifier => modifier.name === 'preventOverflow' |
| 20 |
).boundaries; |
| 21 |
|
| 22 |
if ( |
| 23 |
refRect.bottom < bound.top || |
| 24 |
refRect.left > bound.right || |
| 25 |
refRect.top > bound.bottom || |
| 26 |
refRect.right < bound.left |
| 27 |
) { |
| 28 |
// Avoid unnecessary DOM access if visibility hasn't changed |
| 29 |
if (data.hide === true) { |
| 30 |
return data; |
| 31 |
} |
| 32 |
|
| 33 |
data.hide = true; |
| 34 |
data.attributes['x-out-of-boundaries'] = ''; |
| 35 |
} else { |
| 36 |
// Avoid unnecessary DOM access if visibility hasn't changed |
| 37 |
if (data.hide === false) { |
| 38 |
return data; |
| 39 |
} |
| 40 |
|
| 41 |
data.hide = false; |
| 42 |
data.attributes['x-out-of-boundaries'] = false; |
| 43 |
} |
| 44 |
|
| 45 |
return data; |
| 46 |
} |