Skip to content

Commit 8856255

Browse files
authored
style: fix arrow position in rtl direction (#831)
* style: fix arrow position in rtl direction * fix test snapshot
1 parent ce8620d commit 8856255

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed

assets/index.less

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,9 @@
404404
&-clear {
405405
position: absolute;
406406
top: 0;
407-
right: 4px;
407+
inset-inline-end: 4px;
408408
cursor: pointer;
409409

410-
.@{prefix-cls}-rtl & {
411-
right: auto;
412-
left: 4px;
413-
}
414-
415410
&-btn::after {
416411
content: '×';
417412
}
@@ -431,6 +426,10 @@
431426
display: none;
432427
}
433428

429+
&-rtl {
430+
direction: rtl;
431+
}
432+
434433
// Panel
435434
@arrow-size: 10px;
436435

@@ -456,25 +455,16 @@
456455
height: @arrow-size;
457456
transition: all 0.3s;
458457

459-
.@{prefix-cls}-dropdown-rtl& {
460-
right: @arrow-size;
461-
left: auto;
462-
margin-right: 10px;
463-
margin-left: 0;
464-
}
465-
466458
&::before,
467459
&::after {
468460
position: absolute;
469461
top: 50%;
470-
left: 50%;
462+
inset-inline-start: 50%;
471463
box-sizing: border-box;
472464
transform: translate(-50%, -50%);
473465
content: '';
474466

475467
.@{prefix-cls}-dropdown-rtl& {
476-
right: 50%;
477-
left: auto;
478468
transform: translate(50%, -50%);
479469
}
480470
}

src/PickerInput/Popup/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type {
88
ValueDate,
99
} from '../../interface';
1010
import { toArray } from '../../utils/miscUtil';
11+
import { getRealPlacement, getoffsetUnit } from '../../utils/uiUtil';
1112
import PickerContext from '../context';
1213
import Footer, { type FooterProps } from './Footer';
1314
import PopupPanel, { type PopupPanelProps } from './PopupPanel';
@@ -208,8 +209,8 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
208209
);
209210

210211
if (range) {
211-
const placementRight = placement?.toLowerCase().endsWith('right');
212-
const offsetUnit = placementRight ? 'insetInlineEnd' : 'insetInlineStart';
212+
const realPlacement = getRealPlacement(placement, rtl);
213+
const offsetUnit = getoffsetUnit(realPlacement, rtl);
213214
renderNode = (
214215
<div
215216
ref={wrapperRef}

src/PickerInput/Selector/RangeSelector.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import useInputProps from './hooks/useInputProps';
88
import useRootProps from './hooks/useRootProps';
99
import Icon, { ClearIcon } from './Icon';
1010
import Input, { type InputRef } from './Input';
11+
import { getoffsetUnit, getRealPlacement } from '../../utils/uiUtil';
1112

1213
export type SelectorIdType =
1314
| string
@@ -170,26 +171,26 @@ function RangeSelector<DateType extends object = any>(
170171
});
171172

172173
// ====================== ActiveBar =======================
173-
const placementRight = placement?.toLowerCase().endsWith('right');
174-
const offsetUnit = rtl ? 'insetInlineEnd' : 'insetInlineStart';
175-
174+
const realPlacement = getRealPlacement(placement, rtl);
175+
const offsetUnit = getoffsetUnit(realPlacement, rtl);
176+
const placementRight = realPlacement?.toLowerCase().endsWith('right');
176177
const [activeBarStyle, setActiveBarStyle] = React.useState<React.CSSProperties>({
177178
position: 'absolute',
178179
width: 0,
179-
[offsetUnit]: 0,
180180
});
181181

182182
const syncActiveOffset = useEvent(() => {
183183
const input = getInput(activeIndex);
184184
if (input) {
185185
const { offsetWidth, offsetLeft, offsetParent } = input.nativeElement;
186186
const parentWidth = (offsetParent as HTMLElement)?.offsetWidth || 0;
187+
const activeOffset = placementRight ? (parentWidth - offsetWidth - offsetLeft) : offsetLeft;
187188
setActiveBarStyle((ori) => ({
188189
...ori,
189190
width: offsetWidth,
190-
[offsetUnit]: offsetLeft,
191+
[offsetUnit]: activeOffset,
191192
}));
192-
onActiveOffset(placementRight ? (parentWidth - offsetWidth - offsetLeft) : offsetLeft);
193+
onActiveOffset(activeOffset);
193194
}
194195
});
195196

src/PickerInput/Selector/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ export function getMaskRange(key: string): [startVal: number, endVal: number, de
1212
};
1313

1414
return PresetRange[key];
15-
}
15+
}

src/PickerTrigger/index.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Trigger from '@rc-component/trigger';
22
import type { AlignType, BuildInPlacements } from '@rc-component/trigger/lib/interface';
33
import classNames from 'classnames';
44
import * as React from 'react';
5+
import { getRealPlacement } from '../utils/uiUtil';
56
import PickerContext from '../PickerInput/context';
67

78
const BUILT_IN_PLACEMENTS = {
@@ -79,18 +80,13 @@ function PickerTrigger({
7980
const { prefixCls } = React.useContext(PickerContext);
8081
const dropdownPrefixCls = `${prefixCls}-dropdown`;
8182

82-
const mergedPlacement = React.useMemo(() => {
83-
if (placement !== undefined) {
84-
return placement;
85-
}
86-
return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
87-
}, [placement, direction]);
83+
const realPlacement = getRealPlacement(placement, direction === 'rtl');
8884

8985
return (
9086
<Trigger
9187
showAction={[]}
9288
hideAction={['click']}
93-
popupPlacement={mergedPlacement}
89+
popupPlacement={realPlacement}
9490
builtinPlacements={builtinPlacements}
9591
prefixCls={dropdownPrefixCls}
9692
popupTransitionName={transitionName}

src/utils/uiUtil.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,20 @@ export function elementsContains(
187187
) {
188188
return elements.some((ele) => ele && ele.contains(target));
189189
}
190+
191+
export function getRealPlacement(placement: string, rtl: boolean) {
192+
if (placement !== undefined) {
193+
return placement;
194+
}
195+
return rtl ? 'bottomRight' : 'bottomLeft';
196+
}
197+
198+
export function getoffsetUnit(placement: string, rtl: boolean) {
199+
const realPlacement = getRealPlacement(placement, rtl);
200+
const placementRight = realPlacement?.toLowerCase().endsWith('right');
201+
let offsetUnit = placementRight ? 'insetInlineEnd' : 'insetInlineStart';
202+
if (rtl) {
203+
offsetUnit = ['insetInlineStart', 'insetInlineEnd'].find(unit => unit !== offsetUnit);
204+
}
205+
return offsetUnit;
206+
}

tests/__snapshots__/range.spec.tsx.snap

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ exports[`Picker.Range icon 1`] = `
3434
</div>
3535
<div
3636
class="rc-picker-active-bar"
37-
style="position: absolute; width: 0px; inset-inline-start: 0;"
37+
style="position: absolute; width: 0px;"
3838
/>
3939
<span
4040
class="rc-picker-suffix"
@@ -89,7 +89,7 @@ exports[`Picker.Range onPanelChange is array args should render correctly in pla
8989
</div>
9090
<div
9191
class="rc-picker-active-bar"
92-
style="position: absolute; width: 0px; inset-inline-start: 0;"
92+
style="position: absolute; width: 0px; inset-inline-end: 0;"
9393
/>
9494
</div>
9595
</div>
@@ -129,7 +129,7 @@ exports[`Picker.Range onPanelChange is array args should render correctly in rtl
129129
</div>
130130
<div
131131
class="rc-picker-active-bar"
132-
style="position: absolute; width: 0px; inset-inline-end: 0;"
132+
style="position: absolute; width: 0px;"
133133
/>
134134
</div>
135135
</div>

0 commit comments

Comments
 (0)