-
Notifications
You must be signed in to change notification settings - Fork 26
fix: rtl switch issue https://github.com/ant-design/ant-design/issues/40128 #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import '../../assets/style.less'; | ||
import React, { useState } from 'react'; | ||
import Segmented from 'rc-segmented'; | ||
|
||
export default function App() { | ||
const [direction, setDirection] = useState<'rtl' | 'ltr'>('rtl'); | ||
return ( | ||
<div className="wrapper"> | ||
<button | ||
onClick={() => { | ||
setDirection('rtl'); | ||
}} | ||
style={{ | ||
padding: '0 8px', | ||
marginRight: 8, | ||
}} | ||
> | ||
rtl | ||
</button> | ||
<button | ||
onClick={() => { | ||
setDirection('ltr'); | ||
}} | ||
style={{ | ||
padding: '0 8px', | ||
}} | ||
> | ||
ltr | ||
</button> | ||
<p | ||
style={{ | ||
marginBottom: 8, | ||
}} | ||
/> | ||
<Segmented | ||
options={['iOS', 'Android', 'Web']} | ||
onChange={(value) => console.log(value, typeof value)} | ||
direction={direction} | ||
/> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,7 @@ nav: | |
## refs | ||
|
||
<code src="./demo/refs.tsx"></code> | ||
|
||
## rtl | ||
|
||
<code src="./demo/rtl.tsx"></code> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import * as React from 'react'; | ||
import CSSMotion from 'rc-motion'; | ||
import classNames from 'classnames'; | ||
import CSSMotion from 'rc-motion'; | ||
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect'; | ||
import { composeRef } from 'rc-util/lib/ref'; | ||
import * as React from 'react'; | ||
import type { SegmentedValue } from '.'; | ||
|
||
type ThumbReact = { | ||
left: number; | ||
right: number; | ||
width: number; | ||
} | null; | ||
|
||
|
@@ -18,6 +19,7 @@ export interface MotionThumbInterface { | |
motionName: string; | ||
onMotionStart: VoidFunction; | ||
onMotionEnd: VoidFunction; | ||
direction?: 'ltr' | 'rtl'; | ||
} | ||
|
||
const calcThumbStyle = ( | ||
|
@@ -26,6 +28,10 @@ const calcThumbStyle = ( | |
targetElement | ||
? { | ||
left: targetElement.offsetLeft, | ||
right: | ||
(targetElement.parentElement!.clientWidth as number) - | ||
targetElement.clientWidth - | ||
targetElement.offsetLeft, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 感觉改多了,是不是把 left 换成 insetInlineStart 就够了? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好像不行,试了下 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里的 thumb 滑块使用 translateX 做的偏移,不是用 left/insetInlineStart 实现的 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 再跑下ci,之前的 test case 有问题 修复了一下。 |
||
width: targetElement.clientWidth, | ||
} | ||
: null; | ||
|
@@ -42,6 +48,7 @@ export default function MotionThumb(props: MotionThumbInterface) { | |
motionName, | ||
onMotionStart, | ||
onMotionEnd, | ||
direction, | ||
} = props; | ||
|
||
const thumbRef = React.useRef<HTMLDivElement>(null); | ||
|
@@ -81,6 +88,21 @@ export default function MotionThumb(props: MotionThumbInterface) { | |
} | ||
}, [value]); | ||
|
||
const thumbStart = React.useMemo( | ||
() => | ||
direction === 'rtl' | ||
? toPX(-(prevStyle?.right as number)) | ||
: toPX(prevStyle?.left as number), | ||
[direction, prevStyle], | ||
); | ||
const thumbActive = React.useMemo( | ||
() => | ||
direction === 'rtl' | ||
? toPX(-(nextStyle?.right as number)) | ||
: toPX(nextStyle?.left as number), | ||
[direction, nextStyle], | ||
); | ||
|
||
// =========================== Motion =========================== | ||
const onAppearStart = () => { | ||
return { | ||
|
@@ -118,9 +140,9 @@ export default function MotionThumb(props: MotionThumbInterface) { | |
{({ className: motionClassName, style: motionStyle }, ref) => { | ||
const mergedStyle = { | ||
...motionStyle, | ||
'--thumb-start-left': toPX(prevStyle?.left), | ||
'--thumb-start-left': thumbStart, | ||
'--thumb-start-width': toPX(prevStyle?.width), | ||
'--thumb-active-left': toPX(nextStyle?.left), | ||
'--thumb-active-left': thumbActive, | ||
'--thumb-active-width': toPX(nextStyle?.width), | ||
} as React.CSSProperties; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
写成 toggleButtong 是不是更简洁一些
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
主要为了显示清楚一点,所以分成了两个
button
,demo
上看上去更直观一点,个人感觉。