@@ -9,6 +9,9 @@ type ThumbReact = {
99 left : number ;
1010 right : number ;
1111 width : number ;
12+ top : number ;
13+ bottom : number ;
14+ height : number ;
1215} | null ;
1316
1417export interface MotionThumbInterface {
@@ -20,23 +23,53 @@ export interface MotionThumbInterface {
2023 onMotionStart : VoidFunction ;
2124 onMotionEnd : VoidFunction ;
2225 direction ?: 'ltr' | 'rtl' ;
26+ vertical ?: boolean ;
2327}
2428
2529const calcThumbStyle = (
2630 targetElement : HTMLElement | null | undefined ,
27- ) : ThumbReact =>
28- targetElement
29- ? {
30- left : targetElement . offsetLeft ,
31- right :
32- ( targetElement . parentElement ! . clientWidth as number ) -
33- targetElement . clientWidth -
34- targetElement . offsetLeft ,
35- width : targetElement . clientWidth ,
36- }
37- : null ;
31+ vertical ?: boolean ,
32+ ) : ThumbReact => {
33+ if ( ! targetElement ) return null ;
34+
35+ const style : ThumbReact = {
36+ left : targetElement . offsetLeft ,
37+ right :
38+ ( targetElement . parentElement ! . clientWidth as number ) -
39+ targetElement . clientWidth -
40+ targetElement . offsetLeft ,
41+ width : targetElement . clientWidth ,
42+ top : targetElement . offsetTop ,
43+ bottom :
44+ ( targetElement . parentElement ! . clientHeight as number ) -
45+ targetElement . clientHeight -
46+ targetElement . offsetTop ,
47+ height : targetElement . clientHeight ,
48+ } ;
49+
50+ if ( vertical ) {
51+ // Adjusts positioning and size for vertical layout by setting horizontal properties to 0 and using vertical properties from the style object.
52+ return {
53+ left : 0 ,
54+ right : 0 ,
55+ width : 0 ,
56+ top : style . top ,
57+ bottom : style . bottom ,
58+ height : style . height ,
59+ } ;
60+ }
61+
62+ return {
63+ left : style . left ,
64+ right : style . right ,
65+ width : style . width ,
66+ top : 0 ,
67+ bottom : 0 ,
68+ height : 0 ,
69+ } ;
70+ } ;
3871
39- const toPX = ( value : number ) =>
72+ const toPX = ( value : number | undefined ) : string | undefined =>
4073 value !== undefined ? `${ value } px` : undefined ;
4174
4275export default function MotionThumb ( props : MotionThumbInterface ) {
@@ -49,6 +82,7 @@ export default function MotionThumb(props: MotionThumbInterface) {
4982 onMotionStart,
5083 onMotionEnd,
5184 direction,
85+ vertical = false ,
5286 } = props ;
5387
5488 const thumbRef = React . useRef < HTMLDivElement > ( null ) ;
@@ -57,11 +91,9 @@ export default function MotionThumb(props: MotionThumbInterface) {
5791 // =========================== Effect ===========================
5892 const findValueElement = ( val : SegmentedValue ) => {
5993 const index = getValueIndex ( val ) ;
60-
6194 const ele = containerRef . current ?. querySelectorAll < HTMLDivElement > (
6295 `.${ prefixCls } -item` ,
6396 ) [ index ] ;
64-
6597 return ele ?. offsetParent && ele ;
6698 } ;
6799
@@ -73,8 +105,8 @@ export default function MotionThumb(props: MotionThumbInterface) {
73105 const prev = findValueElement ( prevValue ) ;
74106 const next = findValueElement ( value ) ;
75107
76- const calcPrevStyle = calcThumbStyle ( prev ) ;
77- const calcNextStyle = calcThumbStyle ( next ) ;
108+ const calcPrevStyle = calcThumbStyle ( prev , vertical ) ;
109+ const calcNextStyle = calcThumbStyle ( next , vertical ) ;
78110
79111 setPrevValue ( value ) ;
80112 setPrevStyle ( calcPrevStyle ) ;
@@ -88,34 +120,59 @@ export default function MotionThumb(props: MotionThumbInterface) {
88120 }
89121 } , [ value ] ) ;
90122
91- const thumbStart = React . useMemo (
92- ( ) =>
93- direction === 'rtl'
94- ? toPX ( - ( prevStyle ?. right as number ) )
95- : toPX ( prevStyle ?. left as number ) ,
96- [ direction , prevStyle ] ,
97- ) ;
98- const thumbActive = React . useMemo (
99- ( ) =>
100- direction === 'rtl'
101- ? toPX ( - ( nextStyle ?. right as number ) )
102- : toPX ( nextStyle ?. left as number ) ,
103- [ direction , nextStyle ] ,
104- ) ;
123+ const thumbStart = React . useMemo ( ( ) => {
124+ if ( vertical ) {
125+ return toPX ( prevStyle ?. top ?? 0 ) ;
126+ }
127+
128+ if ( direction === 'rtl' ) {
129+ return toPX ( - ( prevStyle ?. right as number ) ) ;
130+ }
131+
132+ return toPX ( prevStyle ?. left as number ) ;
133+ } , [ vertical , direction , prevStyle ] ) ;
134+
135+ const thumbActive = React . useMemo ( ( ) => {
136+ if ( vertical ) {
137+ return toPX ( nextStyle ?. top ?? 0 ) ;
138+ }
139+
140+ if ( direction === 'rtl' ) {
141+ return toPX ( - ( nextStyle ?. right as number ) ) ;
142+ }
143+
144+ return toPX ( nextStyle ?. left as number ) ;
145+ } , [ vertical , direction , nextStyle ] ) ;
105146
106147 // =========================== Motion ===========================
107148 const onAppearStart = ( ) => {
149+ if ( vertical ) {
150+ return {
151+ transform : 'translateY(var(--thumb-start-top))' ,
152+ height : 'var(--thumb-start-height)' ,
153+ } ;
154+ }
155+
108156 return {
109- transform : ` translateX(var(--thumb-start-left))` ,
110- width : ` var(--thumb-start-width)` ,
157+ transform : ' translateX(var(--thumb-start-left))' ,
158+ width : ' var(--thumb-start-width)' ,
111159 } ;
112160 } ;
161+
113162 const onAppearActive = ( ) => {
163+ if ( vertical ) {
164+ return {
165+ transform : 'translateY(var(--thumb-active-top))' ,
166+ height : 'var(--thumb-active-height)' ,
167+ } ;
168+ }
169+
114170 return {
115- transform : ` translateX(var(--thumb-active-left))` ,
116- width : ` var(--thumb-active-width)` ,
171+ transform : ' translateX(var(--thumb-active-left))' ,
172+ width : ' var(--thumb-active-width)' ,
117173 } ;
118174 } ;
175+
119176 const onVisibleChanged = ( ) => {
120177 setPrevStyle ( null ) ;
121178 setNextStyle ( null ) ;
@@ -144,6 +201,10 @@ export default function MotionThumb(props: MotionThumbInterface) {
144201 '--thumb-start-width' : toPX ( prevStyle ?. width ) ,
145202 '--thumb-active-left' : thumbActive ,
146203 '--thumb-active-width' : toPX ( nextStyle ?. width ) ,
204+ '--thumb-start-top' : thumbStart ,
205+ '--thumb-start-height' : toPX ( prevStyle ?. height ) ,
206+ '--thumb-active-top' : thumbActive ,
207+ '--thumb-active-height' : toPX ( nextStyle ?. height ) ,
147208 } as React . CSSProperties ;
148209
149210 // It's little ugly which should be refactor when @umi/test update to latest jsdom
0 commit comments