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

[Feature] Initial page index #1090

Merged
merged 8 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion packages/nuka/src/Carousel/Carousel.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
}
.nuka-overflow {
overflow: scroll;
scroll-behavior: smooth;
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
.nuka-overflow.scroll-smooth {
scroll-behavior: smooth;
}
.nuka-overflow.scroll-auto {
scroll-behavior: auto;
}
.nuka-overflow::-webkit-scrollbar {
display: none;
}
Expand Down
12 changes: 11 additions & 1 deletion packages/nuka/src/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ export const Carousel = forwardRef<SlideHandle, CarouselProps>(
initialPage,
});

// -- remove initial smooth scroll when given initialPage prop
const [currentReachedInitialPage, setCurrentReachedInitialPage] =
useState(!initialPage);

useEffect(() => {
if (!currentReachedInitialPage && initialPage !== undefined) {
setCurrentReachedInitialPage(initialPage === currentPage);
}
}, [initialPage, currentPage, currentReachedInitialPage]);

// -- handle touch scroll events
const [touchStart, setTouchStart] = useState<null | number>(null);
const [touchEnd, setTouchEnd] = useState<null | number>(null);
Expand Down Expand Up @@ -176,7 +186,7 @@ export const Carousel = forwardRef<SlideHandle, CarouselProps>(
)}
<div className="nuka-slide-container">
<div
className="nuka-overflow"
className={`nuka-overflow ${currentReachedInitialPage ? 'scroll-smooth' : 'scroll-auto'}`}
Copy link
Contributor

@carbonrobot carbonrobot Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thinking, I think you can simplify and prevent an extra re-render by the following

Add the following code to the existing useEffect after line 154

if (initialPage !== undefined && currentPage === initialPage) {
   containerRef.current.classList.remove('scroll-auto');
   containerRef.current.classList.add('scroll-smooth');
}

This eliminates the extra useEffect and state tracking.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah that seems a lot better, added in 33f52d6! I also fixed up the logic, I think it should be

if (initialPage === undefined || currentPage === initialPage) {...

That way if the initialPage isn't specified, it'll just revert back to smooth scrolling

ref={containerRef}
onTouchEnd={onTouchEnd}
onTouchMove={onTouchMove}
Expand Down
Loading