Skip to content
Merged
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
20 changes: 8 additions & 12 deletions src/components/Slider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const Slider = ({
const [scrollPos, setScrollPos] = useState({ isStart: true, isEnd: false });

const handleScroll = useCallback(() => {
const margin = 5;
const scrollWidth = containerRef.current?.scrollWidth ?? 0;
const clientWidth =
containerRef.current?.getBoundingClientRect().width ?? 0;
Expand All @@ -44,10 +45,10 @@ const Slider = ({
setScrollPos({ isStart: true, isEnd: true });
} else if (
scrollPosition >=
(containerRef.current?.scrollWidth ?? 0) - clientWidth
(containerRef.current?.scrollWidth ?? 0) - clientWidth - margin
) {
setScrollPos({ isStart: false, isEnd: true });
} else if (scrollPosition > 0) {
} else if (scrollPosition > margin) {
setScrollPos({ isStart: false, isEnd: false });
} else {
setScrollPos({ isStart: true, isEnd: false });
Expand Down Expand Up @@ -83,14 +84,9 @@ const Slider = ({
const [, setX] = useSpring(() => ({
from: { x: 0 },
to: { x: 0 },
onChange: (results) => {
if (containerRef.current) {
containerRef.current.scrollLeft = results.value.x;
}
},
}));

const slide = (direction: Direction) => {
const slide = async (direction: Direction) => {
const clientWidth =
containerRef.current?.getBoundingClientRect().width ?? 0;
const cardWidth =
Expand All @@ -105,7 +101,7 @@ const Slider = ({
scrollPosition - scrollOffset - visibleItems * cardWidth,
0
);
setX.start({
await setX.start({
from: { x: scrollPosition },
to: { x: newX },
onChange: (results) => {
Expand All @@ -115,7 +111,7 @@ const Slider = ({
},
reset: true,
config: { friction: 60, tension: 500, velocity: 20 },
});
})[0];

if (newX === 0) {
setScrollPos({ isStart: true, isEnd: false });
Expand All @@ -127,7 +123,7 @@ const Slider = ({
scrollPosition - scrollOffset + visibleItems * cardWidth,
containerRef.current?.scrollWidth ?? 0 - clientWidth
);
setX.start({
await setX.start({
from: { x: scrollPosition },
to: { x: newX },
onChange: (results) => {
Expand All @@ -137,7 +133,7 @@ const Slider = ({
},
reset: true,
config: { friction: 60, tension: 500, velocity: 20 },
});
})[0];

if (newX >= (containerRef.current?.scrollWidth ?? 0) - clientWidth) {
setScrollPos({ isStart: false, isEnd: true });
Expand Down
Loading