UNPKG

@nex-ui/react

Version:

🎉 A beautiful, modern, and reliable React component library.

54 lines (50 loc) • 2.31 kB
'use strict'; var utils = require('@nex-ui/utils'); var dom = require('./dom.cjs'); var getOffsetParent = require('./getOffsetParent.cjs'); var getOverflowAncestors = require('./getOverflowAncestors.cjs'); const detectOverflow = (state)=>{ const { elements, rects } = state; const win = utils.ownerWindow(elements.popper); const clippingAncestors = getOverflowAncestors.getOverflowAncestors(elements.popper).filter(dom.isHTMLElement); const clippingAncestorRects = clippingAncestors.map((el)=>dom.toClientRect(el.getBoundingClientRect())); const { visualViewport } = win; if (visualViewport) { clippingAncestorRects.push(dom.toClientRect({ x: visualViewport.offsetLeft, y: visualViewport.offsetTop, width: visualViewport.width, height: visualViewport.height })); } // Compute the actually visible clipping region of the element under the combined effect // of all clipping ancestors by taking intersections sequentially. const clippingRect = clippingAncestorRects.reduce((accRect, rect)=>{ accRect.top = Math.max(rect.top, accRect.top); accRect.right = Math.min(rect.right, accRect.right); accRect.bottom = Math.min(rect.bottom, accRect.bottom); accRect.left = Math.max(rect.left, accRect.left); return accRect; }, { top: -Infinity, right: Infinity, bottom: Infinity, left: -Infinity }); const clippingClientRect = dom.toClientRect({ x: clippingRect.left, y: clippingRect.top, width: clippingRect.right - clippingRect.left, height: clippingRect.bottom - clippingRect.top }); const elementClientRect = dom.toClientRect(dom.getRectRelativeToViewport(rects.popper, getOffsetParent.getOffsetParent(elements.popper))); // The overflow situation of the element in the four directions (top, bottom, left, right) // relative to the clipping boundary. return { top: clippingClientRect.top - elementClientRect.top, bottom: elementClientRect.bottom - clippingClientRect.bottom, left: clippingClientRect.left - elementClientRect.left, right: elementClientRect.right - clippingClientRect.right }; }; exports.detectOverflow = detectOverflow;