UNPKG

zent

Version:

一套前端设计语言和基于React的实现

44 lines (43 loc) 1.44 kB
import getViewportSize from '../../utils/dom/getViewportSize'; import { BottomLeft } from './bottom-left'; import { BottomRight } from './bottom-right'; import { BottomCenter } from './bottom-center'; import { TopLeft } from './top-left'; import { TopRight } from './top-right'; import { TopCenter } from './top-center'; var positionMap = { BottomLeft: BottomLeft, BottomRight: BottomRight, BottomCenter: BottomCenter, TopLeft: TopLeft, TopRight: TopRight, TopCenter: TopCenter, }; export var AutoTopCenter = function (props) { var contentRect = props.contentRect, cushion = props.cushion, anchorRect = props.anchorRect; var viewport = getViewportSize(); var horizontal; var vertical; var mid = (anchorRect.left + anchorRect.right) / 2; var halfWidth = contentRect.width / 2; if (mid + halfWidth > viewport.width && anchorRect.right - contentRect.width > 0) { horizontal = 'Right'; } else if (mid - halfWidth < 0 && anchorRect.left + contentRect.width < viewport.width) { horizontal = 'Left'; } else { horizontal = 'Center'; } if (anchorRect.top - cushion - contentRect.height < 0 && anchorRect.bottom + cushion + contentRect.height < viewport.height) { vertical = 'Bottom'; } else { vertical = 'Top'; } var key = "" + vertical + horizontal; return positionMap[key](props); };