FormidableLabs / nuka-carousel

Small, fast, and accessibility-first React carousel library with an easily customizable UI and behavior to fit your brand and site.
https://commerce.nearform.com/open-source/nuka-carousel
Other
2.99k stars 594 forks source link

Fix for leftover width less than half of container/screen size #1064

Open jduthon opened 1 week ago

jduthon commented 1 week ago

Description

During implementation of the carousel, we noticed sometimes a weird behavior where the amount that can be scrolled left does not go all the way to the end of the content.

See example from this before recording

https://github.com/FormidableLabs/nuka-carousel/assets/4168055/b7fc3495-ec21-4c69-a9d4-f9c5213f176b

You can notice there the scroll stops at the Slide 8, but technically there are 10 slides (so the last slide is numbered 9)

This is due to the number of pages being calculated as a rounded value, hence if the leftover width is less than half of the container size, the number of pages will be rounded down to the lower value and it'll be impossible to scroll to the end with the buttons/scroll indicators.

I propose the following fix:

  1. Use a ceiled value instead of the rounded one, so it'll always include the last missing leftover part
  2. I calculated the last scroll value as a modulo of the width so that the scroll value is precise. It's not technically mandatory (at least for Chrome) as the browser only scrolls to the available content, but I thought if this value is ever used for some other computations it might make sense to have it be precise

Here is the after result from the same story at the same screen size:

https://github.com/FormidableLabs/nuka-carousel/assets/4168055/85933140-837b-44f3-9e25-d7424c318fd1

Type of Change

How Has This Been Tested?

Checklist

changeset-bot[bot] commented 1 week ago

🦋 Changeset detected

Latest commit: 1b88357722e80b6b1285ede035f3de6321ebb682

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ------------- | ----- | | nuka-carousel | Patch |

Not sure what this means? Click here to learn what changesets are.

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

vercel[bot] commented 1 week ago

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 Jun 20, 2024 4:20pm