Skip to content
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

feat(swipe): add RTL mode #13123

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/vant/src/swipe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ export default {
</style>
```

### RTL Layout

```html
<van-swipe indicator-color="white" style="direction: rtl;">
<van-swipe-item>1</van-swipe-item>
<van-swipe-item>2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>
```

## API

### Swipe Props
Expand Down
13 changes: 13 additions & 0 deletions packages/vant/src/swipe/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ export default {
</style>
```

### RTL 布局

可以通过对上下文或自身样式的direction属性判断调整。

```html
<van-swipe indicator-color="white" style="direction: rtl;">
<van-swipe-item>1</van-swipe-item>
<van-swipe-item>2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>
```

## API

### Swipe Props
Expand Down
90 changes: 63 additions & 27 deletions packages/vant/src/swipe/Swipe.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
preventDefault,
createNamespace,
makeNumericProp,
isRtl,
} from '../utils';

// Composables
Expand Down Expand Up @@ -83,6 +84,7 @@ export default defineComponent({

// Whether the user is dragging the swipe
let dragging = false;
let rtl = false;

const touch = useTouch();
const { children, linkChildren } = useChildren(SWIPE_KEY);
Expand All @@ -98,7 +100,9 @@ export default defineComponent({
const minOffset = computed(() => {
if (state.rect) {
const base = props.vertical ? state.rect.height : state.rect.width;
return base - size.value * count.value;
return rtl && !props.vertical
? size.value * count.value - base
: base - size.value * count.value;
}
return 0;
});
Expand All @@ -111,9 +115,9 @@ export default defineComponent({

const trackSize = computed(() => count.value * size.value);

const activeIndicator = computed(
() => (state.active + count.value) % count.value,
);
const activeIndicator = computed(() => {
return (state.active + count.value) % count.value;
});

const isCorrectDirection = computed(() => {
const expect = props.vertical ? 'vertical' : 'horizontal';
Expand Down Expand Up @@ -153,14 +157,21 @@ export default defineComponent({
const getTargetOffset = (targetActive: number, offset = 0) => {
let currentPosition = targetActive * size.value;
if (!props.loop) {
currentPosition = Math.min(currentPosition, -minOffset.value);
currentPosition =
rtl && !props.vertical
? Math.min(currentPosition, minOffset.value)
: Math.min(currentPosition, -minOffset.value);
}

let targetOffset = offset - currentPosition;
let targetOffset =
rtl && !props.vertical
? offset + currentPosition
: offset - currentPosition;
if (!props.loop) {
targetOffset = clamp(targetOffset, minOffset.value, 0);
targetOffset =
rtl && !props.vertical
? clamp(targetOffset, 0, minOffset.value)
: clamp(targetOffset, minOffset.value, 0);
}

return targetOffset;
};

Expand All @@ -180,22 +191,34 @@ export default defineComponent({
const { active } = state;
const targetActive = getTargetActive(pace);
const targetOffset = getTargetOffset(targetActive, offset);

// auto move first and last swipe in loop mode
if (props.loop) {
if (children[0] && targetOffset !== minOffset.value) {
const outRightBound = targetOffset < minOffset.value;
children[0].setOffset(outRightBound ? trackSize.value : 0);
}
if (rtl && !props.vertical) {
if (children[count.value - 1]) {
const outRightBound = targetOffset < size.value;
children[count.value - 1].setOffset(
outRightBound ? trackSize.value : 0,
);
}

if (children[count.value - 1] && targetOffset !== 0) {
const outLeftBound = targetOffset > 0;
children[count.value - 1].setOffset(
outLeftBound ? -trackSize.value : 0,
);
if (children[0]) {
const outLeftBound = targetOffset >= minOffset.value;
children[0].setOffset(outLeftBound ? -trackSize.value : 0);
}
} else {
if (children[0] && targetOffset !== minOffset.value) {
const outRightBound = targetOffset < minOffset.value;
children[0].setOffset(outRightBound ? trackSize.value : 0);
}

if (children[count.value - 1] && targetOffset !== 0) {
const outLeftBound = targetOffset > 0;
children[count.value - 1].setOffset(
outLeftBound ? -trackSize.value : 0,
);
}
}
}

state.active = targetActive;
state.offset = targetOffset;

Expand Down Expand Up @@ -281,6 +304,7 @@ export default defineComponent({
}
}

rtl = isRtl(root);
state.active = active;
state.swiping = true;
state.offset = getTargetOffset(active);
Expand Down Expand Up @@ -327,9 +351,13 @@ export default defineComponent({
if (isCorrectDirection.value) {
const isEdgeTouch =
!props.loop &&
((state.active === 0 && delta.value > 0) ||
(state.active === count.value - 1 && delta.value < 0));

((!rtl &&
((state.active === 0 && delta.value > 0) ||
(state.active === count.value - 1 && delta.value < 0))) ||
(rtl &&
((state.active === count.value - 1 && delta.value > 0) ||
(state.active === 0 && delta.value < 0)) &&
!props.vertical));
if (!isEdgeTouch) {
preventDefault(event, props.stopPropagation);
move({ offset: delta.value });
Expand Down Expand Up @@ -357,16 +385,19 @@ export default defineComponent({
const offset = props.vertical
? touch.offsetY.value
: touch.offsetX.value;

let pace = 0;

if (props.loop) {
pace = offset > 0 ? (delta.value > 0 ? -1 : 1) : 0;
if (offset > 0) {
pace = delta.value > 0 ? -1 : 1;
} else {
pace = 0;
}
} else {
pace = -Math[delta.value > 0 ? 'ceil' : 'floor'](
delta.value / size.value,
);
}
pace = rtl && !props.vertical ? -pace : pace;

move({
pace,
Expand Down Expand Up @@ -430,7 +461,12 @@ export default defineComponent({
}
if (props.showIndicators && count.value > 1) {
return (
<div class={bem('indicators', { vertical: props.vertical })}>
<div
class={bem('indicators', {
vertical: props.vertical,
rtl: rtl,
})}
>
{Array(count.value).fill('').map(renderDot)}
</div>
);
Expand Down
11 changes: 11 additions & 0 deletions packages/vant/src/swipe/demo/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const t = useTranslate({
title4: '纵向滚动',
title5: '自定义滑块大小',
title6: '自定义指示器',
title7: 'RTL 布局',
message: '当前 Swipe 索引:',
},
'en-US': {
Expand All @@ -19,6 +20,7 @@ const t = useTranslate({
title4: 'Vertical Scrolling',
title5: 'Set SwipeItem Size',
title6: 'Custom indicator',
title7: ' RTL Layout',
message: 'Current Swipe index:',
},
});
Expand Down Expand Up @@ -95,6 +97,15 @@ const onChange = (index: number) => showToast(t('message') + index);
</template>
</van-swipe>
</demo-block>

<demo-block :title="t('title7')">
<van-swipe indicator-color="white" style="direction: rtl">
<van-swipe-item>1</van-swipe-item>
<van-swipe-item>2</van-swipe-item>
<van-swipe-item>3</van-swipe-item>
<van-swipe-item>4</van-swipe-item>
</van-swipe>
</demo-block>
</template>

<style lang="less">
Expand Down
14 changes: 14 additions & 0 deletions packages/vant/src/swipe/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@
margin-bottom: var(--van-swipe-indicator-size);
}
}

&--rtl {
.van-swipe__indicator:not(:last-child) {
margin-left: var(--van-swipe-indicator-size);
margin-right: 0;
}
&.van-swipe__indicators--vertical {
left: auto;
right: var(--van-swipe-indicator-margin);
.van-swipe__indicator {
margin-right: var(--van-swipe-indicator-size);
}
}
}
}

&__indicator {
Expand Down
42 changes: 42 additions & 0 deletions packages/vant/src/swipe/test/__snapshots__/demo-ssr.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,46 @@ exports[`should render demo and match snapshot 1`] = `
</div>
</div>
</div>
<div>
<!--[-->
<div
class="van-swipe"
style="direction:rtl;"
>
<div
style="transition-duration:500ms;transform:translateX(0px);"
class="van-swipe__track"
>
<!--[-->
<div
class="van-swipe-item"
style
>
<!--[-->
1
</div>
<div
class="van-swipe-item"
style
>
<!--[-->
2
</div>
<div
class="van-swipe-item"
style
>
<!--[-->
3
</div>
<div
class="van-swipe-item"
style
>
<!--[-->
4
</div>
</div>
</div>
</div>
`;
49 changes: 49 additions & 0 deletions packages/vant/src/swipe/test/__snapshots__/demo.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,53 @@ exports[`should render demo and match snapshot 1`] = `
</div>
</div>
</div>
<div>
<div
class="van-swipe"
style="direction: rtl;"
>
<div
style="transition-duration: 0ms; transform: translateX(0px); width: 400px;"
class="van-swipe__track"
>
<div
class="van-swipe-item"
style="width: 100px;"
>
1
</div>
<div
class="van-swipe-item"
style="width: 100px;"
>
2
</div>
<div
class="van-swipe-item"
style="width: 100px;"
>
3
</div>
<div
class="van-swipe-item"
style="width: 100px;"
>
4
</div>
</div>
<div class="van-swipe__indicators van-swipe__indicators--rtl">
<i
style="background-color: white;"
class="van-swipe__indicator van-swipe__indicator--active"
>
</i>
<i class="van-swipe__indicator">
</i>
<i class="van-swipe__indicator">
</i>
<i class="van-swipe__indicator">
</i>
</div>
</div>
</div>
`;
Loading