@@ -8,13 +8,35 @@ import type {
88 AlignPointLeftRight ,
99 AlignPointTopBottom ,
1010 AlignType ,
11+ OffsetType ,
1112} from '../interface' ;
1213import { collectScroller , getVisibleArea , getWin , toNum } from '../util' ;
1314
1415type Rect = Record < 'x' | 'y' | 'width' | 'height' , number > ;
1516
1617type Points = [ topBottom : AlignPointTopBottom , leftRight : AlignPointLeftRight ] ;
1718
19+ function getUnitOffset ( size : number , offset : OffsetType = 0 ) {
20+ const offsetStr = `${ offset } ` ;
21+ const cells = offsetStr . match ( / ^ ( .* ) \% $ / ) ;
22+ if ( cells ) {
23+ return size * ( parseFloat ( cells [ 1 ] ) / 100 ) ;
24+ }
25+ return parseFloat ( offsetStr ) ;
26+ }
27+
28+ function getNumberOffset (
29+ rect : { width : number ; height : number } ,
30+ offset ?: OffsetType [ ] ,
31+ ) {
32+ const [ offsetX , offsetY ] = offset || [ ] ;
33+
34+ return [
35+ getUnitOffset ( rect . width , offsetX ) ,
36+ getUnitOffset ( rect . height , offsetY ) ,
37+ ] ;
38+ }
39+
1840function splitPoints ( points : string = '' ) : Points {
1941 return [ points [ 0 ] as any , points [ 1 ] as any ] ;
2042}
@@ -229,11 +251,14 @@ export default function useAlign(
229251
230252 // Offset
231253 const { offset, targetOffset } = placementInfo ;
232- const [ popupOffsetX = 0 , popupOffsetY = 0 ] = offset || [ ] ;
233- const [ targetOffsetX = 0 , targetOffsetY = 0 ] = targetOffset || [ ] ;
254+ const [ popupOffsetX , popupOffsetY ] = getNumberOffset ( popupRect , offset ) ;
255+ const [ targetOffsetX , targetOffsetY ] = getNumberOffset (
256+ targetRect ,
257+ targetOffset ,
258+ ) ;
234259
235- targetRect . x + = targetOffsetX ;
236- targetRect . y + = targetOffsetY ;
260+ targetRect . x - = targetOffsetX ;
261+ targetRect . y - = targetOffsetY ;
237262
238263 // Points
239264 const [ popupPoint , targetPoint ] = placementInfo . points || [ ] ;
0 commit comments