import {iPopupRefreshBuildParams} from "../utils/popup.utils"; import {handleDirection} from "../utils/handleDirection"; import {getComponentCls} from "@peryl/react-compose"; /** * 更新箭头定位信息 * @author 韦胜健 * @date 2023/5/16 16:17 */ export function refreshArrow(buildParams: iPopupRefreshBuildParams) { const { refs, direction, option } = buildParams; const popupArrow = refs.popupArrow; if (!popupArrow) {return;} Object.assign(popupArrow.style as any, { top: "", left: "", right: "", bottom: "", transform: "", border: "", borderTopColor: "", borderBottomColor: "", borderLeftColor: "", borderRightColor: "", }); handleDirection(direction, { top: () => { Object.assign(popupArrow.style as any, { ...getVerticalPosition(buildParams), top: `calc(100%)`, transform: `scaleX(${option.arrowScale})`, border: `solid ${option.arrowSize}px transparent`, borderTopColor: `var(--${getComponentCls('popup-item-background')})`, }); }, bottom: () => { Object.assign(popupArrow.style as any, { ...getVerticalPosition(buildParams), bottom: `calc(100%)`, transform: `scaleX(${option.arrowScale})`, border: `solid ${option.arrowSize}px transparent`, borderBottomColor: `var(--${getComponentCls('popup-item-background')})`, }); }, left: () => { Object.assign(popupArrow.style as any, { ...getHorizontalAlignAttrs(buildParams), left: `calc(100%)`, transform: `scaleY(${option.arrowScale})`, border: `solid ${option.arrowSize}px transparent`, borderLeftColor: `var(--${getComponentCls('popup-item-background')})`, }); }, right: () => { Object.assign(popupArrow.style as any, { ...getHorizontalAlignAttrs(buildParams), right: `calc(100%)`, transform: `scaleY(${option.arrowScale})`, border: `solid ${option.arrowSize}px transparent`, borderRightColor: `var(--${getComponentCls('popup-item-background')})`, }); }, }); } /** * align为end的时候,用right定位arrow * @author 韦胜健 * @date 2022.5.10 10:45 */ function getVerticalPosition(buildParams: iPopupRefreshBuildParams) { const left = getArrowLeft(buildParams); return buildParams.align === 'start' || buildParams.align === 'center' ? { left: `${left}px`, } : { right: `${buildParams.rects.popupEl.width - left - buildParams.option.arrowSize * 2}px` }; } /** * align为end的时候,用bottom定位arrow * @author 韦胜健 * @date 2022.5.10 10:45 */ function getHorizontalAlignAttrs(buildParams: iPopupRefreshBuildParams) { const top = getArrowTop(buildParams); return buildParams.align === 'start' || buildParams.align === 'center' ? { top: `${top}px`, } : { bottom: `${buildParams.rects.popupEl.height - top - buildParams.option.arrowSize * 2}px` }; } export function getArrowLeft({ rects, option }: iPopupRefreshBuildParams) { const leftStart = Math.max(rects.alignReference.left, rects.popupEl.left); const leftEnd = Math.min(rects.alignReference.left + rects.alignReference.width, rects.popupEl.left + rects.popupEl.width); let left = leftStart + (leftEnd - leftStart) / 2 - option.arrowSize; left -= rects.popupEl.left; return Math.max(4, Math.min(rects.popupEl.width - option.arrowSize * 2 - 4, left)); } export function getArrowTop({ rects, option }: iPopupRefreshBuildParams) { const topStart = Math.max(rects.alignReference.top, rects.popupEl.top); const topEnd = Math.min(rects.alignReference.top + rects.alignReference.height, rects.popupEl.top + rects.popupEl.height); let top = topStart + (topEnd - topStart) / 2 - option.arrowSize; top -= rects.popupEl.top; return Math.max(4, Math.min(rects.popupEl.height - option.arrowSize * 2 - 4, top)); }