Repository navigation
fix(carousel): keep slides on the active panel during autoplay and navigation - #3270
Merged
Merged
Conversation
startAutoplay's setInterval callback closed over currentPanel, nrOfPanels, and isInfinite at the moment autoplay started. Since the interval keeps running with that same closure, every subsequent tick recomputed "next" from the same stale currentPanel instead of the updated one, so autoplay advanced once and then got stuck forever. The pre-rewrite class component read this.state.currentPanel fresh on every tick, so it never hit this. Keep a ref in sync with the latest currentPanel/nrOfPanels/isInfinite on every render and read from it inside the interval callback (and in setCurrentPanel's reported currentPanel) instead of the closed-over values.
Verifies that autoplay advances through every panel and loops back to the start, instead of stalling after the first tick.
The autoplay interval passed changeTranslationAutomatically to setCurrentPanel, which runs it in a setTimeout. The interval outlives the render that created it, so that callback held a stale panel and could overwrite the correct translateX set by the currentPanel effect. The indicators kept advancing while the slides stuck on the first panel. The currentPanel effect already keeps the translation in sync, so the interval no longer passes a callback. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…owser Uses real timers and a real stage width, since jsdom reports offsetWidth 0 and fake timers inside act() hide the interval/effect ordering race. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Uses the ThreeItemsPerPanel story's props with twelve items, since the story's two panels are too few to show the strip drifting from the active panel. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The story reused the first two sample items to reach six, so two items shared an id. It now has twelve items with unique ids, four panels of three, matching the autoplay browser test. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
setCurrentPanel scheduled changeTranslationAutomatically in a setTimeout. That callback comes from the render that handled the click, so it held the previous panel and wrote the previous translateX after the new one, leaving the slides one click behind while the indicators moved. The currentPanel effect already derives the translation from the new panel, so setCurrentPanel no longer takes a callback. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ation Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The real-timer tests update state outside act on purpose, since wrapping the waits in act queues the tick and the translation effect into one flush and hides the ordering bug. Set IS_REACT_ACT_ENVIRONMENT to false for this file only so React stops logging not-wrapped-in-act warnings. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
6 of 15 tasks
| // is running, so it can't rely on values closed over when it was created. | ||
| // Keep a ref in sync on every render and read from it inside the interval | ||
| // so each tick sees the latest panel/count instead of a stale snapshot. | ||
| const latestRef = useRef({ currentPanel, nrOfPanels, isInfinite }); |
Contributor
Author
There was a problem hiding this comment.
This is the primary bugfix. The 0.10.x version was a class component that read this.getCurrentPanel() at call time, so it always saw the latest increment across callbacks.
Niznikr
approved these changes
Oct 30, 2026
Niznikr
added a commit
that referenced
this pull request
Nov 4, 2026
Conflicts came from #3268 (forwardRef, className on the root), #3266 (Input counter), #3270 (Carousel autoplay) and #3265 (MenuDropdown open registration). - Slider, VerticalNavigation, Popover, Tree: keep master's forwardRef, root className and classNameList/styleList, with the controlled value and selection from this branch. - Input, Carousel, MenuDropdown: keep this branch's implementation, which already fixes the same stale closures through refs. Master's regression tests are kept; the uncontrolled Input maxValue test is rewritten for a controlled counter. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3271
Additional description
Carouselautoplay stalled after one panel, and the slides stayed put or lagged one click behind while the indicators advanced, for both autoplay and the previous/next buttons, indicators and arrow keys. Reproduced withitemsPerPanel1 and 3.Root cause. Both bugs come from the hooks rewrite (#3195,
520f86bb7). The class component readthis.getCurrentPanel()live; the rewrite moved the logic into closures that go stale.setIntervalcallback instartAutoplayclosed overcurrentPanel,nrOfPanelsandisInfinitefrom the render where autoplay started. Every tick computedcurrentPanel + 1from that first panel, so it re-selected panel 1 forever. The interval now reads them from alatestRefkept in sync each render.setCurrentPanelranchangeTranslationAutomaticallyin asetTimeout(…, 0). That callback comes from the render that handled the click or tick, so it holds the previous panel and wrote the previoustranslateXover the one thecurrentPaneleffect had just set.setCurrentPanelno longer takes a callback; the effect already derives the translation from the new panel.Why tests missed it. The old
carousel.browser-test.jsxand the jsdom port never let an interval fire, and jsdom reportsoffsetWidthas 0, so every translation is 0 there. Fake timers insideact()also hide the second bug: they flush effects in a fixed order, and the new browser test passed under fake timers while the real behavior was broken. The browser tests use real timers.Tests
carousel.test.jsx(jsdom): advances through every panel across ticks, then loops.carousel.browser.test.jsx(new, real Chromium, real timers):translateXper autoplay tick, one loop and two loops, for 1 and 3 items per panel (12 items so there are 4 panels); andtranslateXafter next/previous button, indicator and right-arrow navigation.Stories.
WithAutoplaynow setsisInfinite.ThreeItemsPerPanelhas 12 items with unique ids (it reused two sample items, so two items shared an id), matching the browser test. Snapshot updated forThreeItemsPerPanelonly.Not changed here: the non-infinite stop-at-end path still has no test, and
stopAutoplaydoes not update state when onlyonRequestAutoplayToggleis passed.CONTRIBUTOR checklist (do not remove)
Green gate:
npm run lintpasses with 0 errors (4 existing warnings in carousel files)npm run typecheckis cleannpm test -- --runpasses: 59 files, 1273 tests (unit, browser and snapshots)npm run buildsucceeds. The library build and.d.tssucceed, butcheck-package-exportsin my local run reportsutilities/hooksmissing a default export. This branch doesn't touch it and master CI is green, so I believe it is a local artifact, but I haven't confirmed.Behavior & docs:
carousel.browser.test.jsx..d.tschanges.REVIEWER checklist (do not remove)
ThreeItemsPerPanelonly).