Skip to content

Commit

Permalink
style: fix arrow position in rtl direction (#831)
Browse files Browse the repository at this point in the history
* style: fix arrow position in rtl direction

* fix test snapshot
  • Loading branch information
afc163 authored Jun 18, 2024
1 parent ce8620d commit 8856255
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 35 deletions.
22 changes: 6 additions & 16 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,9 @@
&-clear {
position: absolute;
top: 0;
right: 4px;
inset-inline-end: 4px;
cursor: pointer;

.@{prefix-cls}-rtl & {
right: auto;
left: 4px;
}

&-btn::after {
content: '×';
}
Expand All @@ -431,6 +426,10 @@
display: none;
}

&-rtl {
direction: rtl;
}

// Panel
@arrow-size: 10px;

Expand All @@ -456,25 +455,16 @@
height: @arrow-size;
transition: all 0.3s;

.@{prefix-cls}-dropdown-rtl& {
right: @arrow-size;
left: auto;
margin-right: 10px;
margin-left: 0;
}

&::before,
&::after {
position: absolute;
top: 50%;
left: 50%;
inset-inline-start: 50%;
box-sizing: border-box;
transform: translate(-50%, -50%);
content: '';

.@{prefix-cls}-dropdown-rtl& {
right: 50%;
left: auto;
transform: translate(50%, -50%);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/PickerInput/Popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
ValueDate,
} from '../../interface';
import { toArray } from '../../utils/miscUtil';
import { getRealPlacement, getoffsetUnit } from '../../utils/uiUtil';
import PickerContext from '../context';
import Footer, { type FooterProps } from './Footer';
import PopupPanel, { type PopupPanelProps } from './PopupPanel';
Expand Down Expand Up @@ -208,8 +209,8 @@ export default function Popup<DateType extends object = any>(props: PopupProps<D
);

if (range) {
const placementRight = placement?.toLowerCase().endsWith('right');
const offsetUnit = placementRight ? 'insetInlineEnd' : 'insetInlineStart';
const realPlacement = getRealPlacement(placement, rtl);
const offsetUnit = getoffsetUnit(realPlacement, rtl);
renderNode = (
<div
ref={wrapperRef}
Expand Down
13 changes: 7 additions & 6 deletions src/PickerInput/Selector/RangeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useInputProps from './hooks/useInputProps';
import useRootProps from './hooks/useRootProps';
import Icon, { ClearIcon } from './Icon';
import Input, { type InputRef } from './Input';
import { getoffsetUnit, getRealPlacement } from '../../utils/uiUtil';

export type SelectorIdType =
| string
Expand Down Expand Up @@ -170,26 +171,26 @@ function RangeSelector<DateType extends object = any>(
});

// ====================== ActiveBar =======================
const placementRight = placement?.toLowerCase().endsWith('right');
const offsetUnit = rtl ? 'insetInlineEnd' : 'insetInlineStart';

const realPlacement = getRealPlacement(placement, rtl);
const offsetUnit = getoffsetUnit(realPlacement, rtl);
const placementRight = realPlacement?.toLowerCase().endsWith('right');
const [activeBarStyle, setActiveBarStyle] = React.useState<React.CSSProperties>({
position: 'absolute',
width: 0,
[offsetUnit]: 0,
});

const syncActiveOffset = useEvent(() => {
const input = getInput(activeIndex);
if (input) {
const { offsetWidth, offsetLeft, offsetParent } = input.nativeElement;
const parentWidth = (offsetParent as HTMLElement)?.offsetWidth || 0;
const activeOffset = placementRight ? (parentWidth - offsetWidth - offsetLeft) : offsetLeft;
setActiveBarStyle((ori) => ({
...ori,
width: offsetWidth,
[offsetUnit]: offsetLeft,
[offsetUnit]: activeOffset,
}));
onActiveOffset(placementRight ? (parentWidth - offsetWidth - offsetLeft) : offsetLeft);
onActiveOffset(activeOffset);
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/PickerInput/Selector/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export function getMaskRange(key: string): [startVal: number, endVal: number, de
};

return PresetRange[key];
}
}
10 changes: 3 additions & 7 deletions src/PickerTrigger/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Trigger from '@rc-component/trigger';
import type { AlignType, BuildInPlacements } from '@rc-component/trigger/lib/interface';
import classNames from 'classnames';
import * as React from 'react';
import { getRealPlacement } from '../utils/uiUtil';
import PickerContext from '../PickerInput/context';

const BUILT_IN_PLACEMENTS = {
Expand Down Expand Up @@ -79,18 +80,13 @@ function PickerTrigger({
const { prefixCls } = React.useContext(PickerContext);
const dropdownPrefixCls = `${prefixCls}-dropdown`;

const mergedPlacement = React.useMemo(() => {
if (placement !== undefined) {
return placement;
}
return direction === 'rtl' ? 'bottomRight' : 'bottomLeft';
}, [placement, direction]);
const realPlacement = getRealPlacement(placement, direction === 'rtl');

return (
<Trigger
showAction={[]}
hideAction={['click']}
popupPlacement={mergedPlacement}
popupPlacement={realPlacement}
builtinPlacements={builtinPlacements}
prefixCls={dropdownPrefixCls}
popupTransitionName={transitionName}
Expand Down
17 changes: 17 additions & 0 deletions src/utils/uiUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,20 @@ export function elementsContains(
) {
return elements.some((ele) => ele && ele.contains(target));
}

export function getRealPlacement(placement: string, rtl: boolean) {
if (placement !== undefined) {
return placement;
}
return rtl ? 'bottomRight' : 'bottomLeft';
}

export function getoffsetUnit(placement: string, rtl: boolean) {
const realPlacement = getRealPlacement(placement, rtl);
const placementRight = realPlacement?.toLowerCase().endsWith('right');
let offsetUnit = placementRight ? 'insetInlineEnd' : 'insetInlineStart';
if (rtl) {
offsetUnit = ['insetInlineStart', 'insetInlineEnd'].find(unit => unit !== offsetUnit);
}
return offsetUnit;
}
6 changes: 3 additions & 3 deletions tests/__snapshots__/range.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports[`Picker.Range icon 1`] = `
</div>
<div
class="rc-picker-active-bar"
style="position: absolute; width: 0px; inset-inline-start: 0;"
style="position: absolute; width: 0px;"
/>
<span
class="rc-picker-suffix"
Expand Down Expand Up @@ -89,7 +89,7 @@ exports[`Picker.Range onPanelChange is array args should render correctly in pla
</div>
<div
class="rc-picker-active-bar"
style="position: absolute; width: 0px; inset-inline-start: 0;"
style="position: absolute; width: 0px; inset-inline-end: 0;"
/>
</div>
</div>
Expand Down Expand Up @@ -129,7 +129,7 @@ exports[`Picker.Range onPanelChange is array args should render correctly in rtl
</div>
<div
class="rc-picker-active-bar"
style="position: absolute; width: 0px; inset-inline-end: 0;"
style="position: absolute; width: 0px;"
/>
</div>
</div>
Expand Down

0 comments on commit 8856255

Please sign in to comment.