Skip to content

Commit

Permalink
feat(ui): slides support swipe gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Nov 18, 2022
1 parent c71e227 commit 2f2ce7d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/site/src/styles/routes/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@
width: 28px;
height: 2px;
background-color: var(--#{$rd-prefix}text-color);
border-radius: 50%;
/* stylelint-disable-next-line declaration-property-value-allowed-list */
border-radius: 1px;
/* stylelint-disable-next-line declaration-property-value-disallowed-list */
transition: all 300ms linear;
transform-origin: center;
Expand Down
23 changes: 20 additions & 3 deletions packages/ui/src/components/slides/Slides.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,11 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
//#endregion

const dataRef = useRef<{
startDragTime: number;
clearTid?: () => void;
}>({});
}>({
startDragTime: 0,
});

const async = useAsync();

Expand Down Expand Up @@ -214,7 +217,13 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
break;
}
}
changeActiveId(dList[newIndex].id);
if (newIndex === activeIndex) {
if (performance.now() - dataRef.current.startDragTime < 300 && Math.abs(dragDistance) > 30) {
changeActiveId(dList[Math.max(newIndex - 1, 0)].id);
}
} else {
changeActiveId(dList[newIndex].id);
}
} else {
let newIndex = activeIndex;
let size = 0;
Expand All @@ -226,7 +235,13 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides
break;
}
}
changeActiveId(dList[newIndex].id);
if (newIndex === activeIndex) {
if (performance.now() - dataRef.current.startDragTime < 300 && Math.abs(dragDistance) > 30) {
changeActiveId(dList[Math.min(newIndex + 1, dList.length - 1)].id);
}
} else {
changeActiveId(dList[newIndex].id);
}
}
}
setDragStartPosition(undefined);
Expand Down Expand Up @@ -302,10 +317,12 @@ export function DSlides<ID extends DId, T extends DSlideItem<ID>>(props: DSlides

if (e.button === 0) {
setDragStartPosition(e[dVertical ? 'clientY' : 'clientX']);
dataRef.current.startDragTime = performance.now();
}
}}
onTouchStart={(e) => {
setDragStartPosition(e.touches[0][dVertical ? 'clientY' : 'clientX']);
dataRef.current.startDragTime = performance.now();
}}
onTouchEnd={() => {
handleDragEnd();
Expand Down

0 comments on commit 2f2ce7d

Please sign in to comment.