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

[Feature] Initial page index #1090

merged 8 commits into from
Jan 16, 2025

Conversation

melvin-chen
Copy link
Member

@melvin-chen melvin-chen commented Jan 13, 2025

Description

Adds a new prop for the carousel to start on any page index. This is related to #1073 and helps with the v7 to v8+ prop migration.

In v7, there was a slideIndex prop so users could dictate what slide should be showing at a given time. This was half remedied by the goToSlide() method where users could manually update the indicies, but users still couldn't specify an index to start with on load. I've added initialPage that's plugged into the default value for the use-paging useState with a fallback to 0. This is specified in the updated prop pages.

Fixes #1073

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Tested in browser in Storybook (added a story) and with unit tests.

Checklist

  • My code follows the style guidelines of this project (I have run pnpm run lint)
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes (I have run pnpm run test:ci-with-server/pnpm run test)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have recorded any user-facing fixes or changes with pnpm changeset.
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

Copy link

changeset-bot bot commented Jan 13, 2025

⚠️ No Changeset found

Latest commit: f91542a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Jan 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nuka-carousel-website ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 16, 2025 5:39pm

docs/api/autoplay.mdx Outdated Show resolved Hide resolved
packages/nuka/src/hooks/use-paging.tsx Outdated Show resolved Hide resolved
docs/api/autoplay.mdx Outdated Show resolved Hide resolved
if (initialPage) {
setCurrentPage(Math.max(0, Math.min(initialPage, totalPages)));
}
}, [initialPage, totalPages]);
Copy link
Contributor

Choose a reason for hiding this comment

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

This new version makes it render at index 0 and slide to index 1. Seemed to be working better with the previous code.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, yeah that was what I was commenting about #1090 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

This is fixed in fcb0049

@@ -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

@melvin-chen melvin-chen merged commit 27fa32e into main Jan 16, 2025
4 checks passed
@melvin-chen melvin-chen deleted the feat/initial-index branch January 16, 2025 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

There is no option to set a default slide
2 participants