zent
Version:
一套前端设计语言和基于React的实现
59 lines (49 loc) • 1.59 kB
text/typescript
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';
import { IPositionFunction } from '../position-function';
const positionMap: Record<string, IPositionFunction> = {
BottomLeft,
BottomRight,
BottomCenter,
TopLeft,
TopRight,
TopCenter,
};
export const AutoTopCenter: IPositionFunction = props => {
const { contentRect, cushion, anchorRect } = props;
const viewport = getViewportSize();
let horizontal;
let vertical;
const mid = (anchorRect.left + anchorRect.right) / 2;
const 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';
}
const key = `${vertical}${horizontal}`;
return positionMap[key](props);
};