Skip to content

fix(Carousel): pages calculation - #2345

Merged
benjamincanac merged 2 commits into
nuxt:devfrom
SwoleRonson:fix/carousel-pages
Oct 9, 2024
Merged

benjamincanac merged 2 commits into
nuxt:devfrom
SwoleRonson:fix/carousel-pages

Conversation

@SwoleRonson

Copy link
Copy Markdown

🔗 Linked issue

Resolves #2045

❓ Type of change

  • 📖 Documentation (updates to the documentation or readme)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Fixed the carousel pages calculation so that it no longer errors when the number of prop items is less than the number of calculated divisions.

The pages calculation in the carousel component returns a negative value when the number of items is less than the number of carousel divisions (calculated from the width of the carousel divided by the width of an item).

This causes the error RangeError: Invalid array length in the indicators template, as it is trying to run v-for="page in pages", where pages is negative.

The use case which first produced this issue was a carousel of recently viewed products. Initially, there is only 1 item in the carousel, however the items widths had been setup to display multiple items, as and when the user views more products.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

…e number of items is less than the number of divisions
@benjamincanac benjamincanac changed the title fix: carousel pages calculation fix(Carousel): pages calculation Oct 9, 2024
@benjamincanac
benjamincanac merged commit 5cf24fa into nuxt:dev Oct 9, 2024
@benjamincanac

Copy link
Copy Markdown
Member

Thanks! 😊

const itemDivisions = Math.round(carouselWidth.value / itemWidth.value);

if (props.items.length <= itemDivisions) {
return 0;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@EdmundChaplin why 0 instead of 1?

In this case, isLast seems to be broken.
When there is only one page, pages is 0 and currentPage is 1, so isLast is false and the right arrow remains enabled.

This branch was successfully deployed

1 active deployment
Preview — 0c6a79e1 Deployed Oct 9, 2024 by vercel[bot]
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.

Indicators causing bug in Carousel: "invalid array length"

3 participants