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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jduthon
Copy link

@jduthon jduthon commented Jun 20, 2024

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

before.mov

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:

after.mov

Type of Change

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

How Has This Been Tested?

  • Covered with unit tests
  • Tested on the storybook examples

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
  • N/A 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 Jun 20, 2024

🦋 Changeset detected

Latest commit: 1b88357

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

Copy link

vercel bot commented Jun 20, 2024

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

@@ -121,7 +121,7 @@ describe('useMeasurement', () => {
const { totalPages, scrollOffset } = result.current;

expect(totalPages).toBe(2);
expect(scrollOffset).toEqual([0, 500]);
expect(scrollOffset).toEqual([0, 400]);
Copy link
Author

Choose a reason for hiding this comment

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

This is now just the remainder scroll (so the exact amount that needs to be scrolled to fully have the next page in view)

expect(totalPages).toBe(3);
expect(scrollOffset).toEqual([0, 573, 1146]);
expect(totalPages).toBe(4);
// 573 * 0, 573 * 1, 573 * 2, 573 * 3 + (1720 - 573 * 3)
Copy link
Author

Choose a reason for hiding this comment

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

Not sure if this is useful, but I thought a code comment explaining where the values are coming from for the toEqual might be helpful?
Happy to edit or delete if it adds more confusion for now

// this test covers that even when the leftover width is less than
// half of the screen width, it should still be scrollable so that user can see
// the small overflow
scrollWidth: 600,
Copy link
Author

Choose a reason for hiding this comment

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

New test that verifies the fix works correctly

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.

None yet

1 participant