1- import * as React from 'react' ;
2- import CSSMotion from 'rc-motion' ;
31import classNames from 'classnames' ;
2+ import CSSMotion from 'rc-motion' ;
43import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect' ;
54import { composeRef } from 'rc-util/lib/ref' ;
5+ import * as React from 'react' ;
66import type { SegmentedValue } from '.' ;
77
88type ThumbReact = {
99 left : number ;
10+ right : number ;
1011 width : number ;
1112} | null ;
1213
@@ -18,6 +19,7 @@ export interface MotionThumbInterface {
1819 motionName : string ;
1920 onMotionStart : VoidFunction ;
2021 onMotionEnd : VoidFunction ;
22+ direction ?: 'ltr' | 'rtl' ;
2123}
2224
2325const calcThumbStyle = (
@@ -26,6 +28,10 @@ const calcThumbStyle = (
2628 targetElement
2729 ? {
2830 left : targetElement . offsetLeft ,
31+ right :
32+ ( targetElement . parentElement ! . clientWidth as number ) -
33+ targetElement . clientWidth -
34+ targetElement . offsetLeft ,
2935 width : targetElement . clientWidth ,
3036 }
3137 : null ;
@@ -42,6 +48,7 @@ export default function MotionThumb(props: MotionThumbInterface) {
4248 motionName,
4349 onMotionStart,
4450 onMotionEnd,
51+ direction,
4552 } = props ;
4653
4754 const thumbRef = React . useRef < HTMLDivElement > ( null ) ;
@@ -81,6 +88,21 @@ export default function MotionThumb(props: MotionThumbInterface) {
8188 }
8289 } , [ value ] ) ;
8390
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+ ) ;
105+
84106 // =========================== Motion ===========================
85107 const onAppearStart = ( ) => {
86108 return {
@@ -118,9 +140,9 @@ export default function MotionThumb(props: MotionThumbInterface) {
118140 { ( { className : motionClassName , style : motionStyle } , ref ) => {
119141 const mergedStyle = {
120142 ...motionStyle ,
121- '--thumb-start-left' : toPX ( prevStyle ?. left ) ,
143+ '--thumb-start-left' : thumbStart ,
122144 '--thumb-start-width' : toPX ( prevStyle ?. width ) ,
123- '--thumb-active-left' : toPX ( nextStyle ?. left ) ,
145+ '--thumb-active-left' : thumbActive ,
124146 '--thumb-active-width' : toPX ( nextStyle ?. width ) ,
125147 } as React . CSSProperties ;
126148
0 commit comments