Skip to content

Commit 9da0b7e

Browse files
committed
feat(web): adds swipe support to panel carousel
1 parent 2a1b704 commit 9da0b7e

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/components/views/panel/PanelContent.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const onTouchEnd = (e: TouchEvent) => {
3030
left: clientWidth ? Math.min(clientWidth / 2, 400) : 400,
3131
right: clientWidth ? Math.min(clientWidth / 2, 400) : 400,
3232
});
33-
if (swipe === SwipeDirection.Right) emits('onBack', e);
33+
if (swipe === SwipeDirection.Right) emits('onClose', e);
3434
};
3535
</script>
3636

src/components/views/panel/PanelTrailers.vue

+31-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<script setup lang="ts">
2-
import { NCarousel } from 'naive-ui';
3-
import { type PropType } from 'vue';
2+
import { type CarouselInst, NCarousel } from 'naive-ui';
3+
import { type PropType, ref } from 'vue';
44
55
import type { YoutubePlayerProps } from '~/models/youtube-player.model';
66
77
import YoutubePlayer from '~/components/common/video/YoutubePlayer.vue';
8+
import { handleSwipeLeftRight, SwipeDirection } from '~/utils/touch.utils';
89
910
defineProps({
1011
trailers: {
@@ -17,18 +18,46 @@ defineProps({
1718
default: false,
1819
},
1920
});
21+
22+
const carouselRef = ref<CarouselInst & InstanceType<typeof NCarousel>>();
23+
const touchStart = ref<TouchEvent>();
24+
25+
const onTouchStart = (e: TouchEvent) => {
26+
touchStart.value = e;
27+
};
28+
29+
const onTouchEnd = (e: TouchEvent) => {
30+
const _touchStart = touchStart.value?.targetTouches?.[0];
31+
const _touchEnd = e.changedTouches?.[0];
32+
if (!_touchStart) return;
33+
touchStart.value = undefined;
34+
const _ref = carouselRef.value;
35+
if (!_ref) return;
36+
const { clientWidth, clientHeight } = _ref.$el || {};
37+
const swipe = handleSwipeLeftRight(_touchStart, _touchEnd, {
38+
vertical: clientHeight ? Math.min(clientHeight / 2, 200) : 200,
39+
left: clientWidth ? Math.min(clientWidth / 2, 100) : 100,
40+
right: clientWidth ? Math.min(clientWidth / 2, 100) : 100,
41+
});
42+
if (!swipe) return;
43+
if (swipe === SwipeDirection.Right) _ref.next();
44+
if (swipe === SwipeDirection.Left) _ref.prev();
45+
};
2046
</script>
2147

2248
<template>
2349
<NCarousel
2450
v-if="!disabled && trailers?.length"
51+
ref="carouselRef"
2552
class="carousel"
2653
dot-type="line"
2754
effect="slide"
2855
centered-slides
2956
keyboard
3057
trigger="hover"
3158
:space-between="32"
59+
@touchstart="onTouchStart"
60+
@touchend="onTouchEnd"
3261
>
3362
<slot />
3463
<YoutubePlayer

0 commit comments

Comments
 (0)