Skip to content

fix(carousel): keep slides on the active panel during autoplay and navigation - #3270

Merged
interactivellama merged 10 commits into
masterfrom
fix/carousel-autoplay-stale-closure
Oct 30, 2026
Merged

interactivellama merged 10 commits into
masterfrom
fix/carousel-autoplay-stale-closure

Conversation

@interactivellama

@interactivellama interactivellama commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3271

Additional description

Carousel autoplay 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 with itemsPerPanel 1 and 3.

Root cause. Both bugs come from the hooks rewrite (#3195, 520f86bb7). The class component read this.getCurrentPanel() live; the rewrite moved the logic into closures that go stale.

  1. Autoplay stall. The setInterval callback in startAutoplay closed over currentPanel, nrOfPanels and isInfinite from the render where autoplay started. Every tick computed currentPanel + 1 from that first panel, so it re-selected panel 1 forever. The interval now reads them from a latestRef kept in sync each render.
  2. Stale translation. setCurrentPanel ran changeTranslationAutomatically in a setTimeout(…, 0). That callback comes from the render that handled the click or tick, so it holds the previous panel and wrote the previous translateX over the one the currentPanel effect had just set. setCurrentPanel no longer takes a callback; the effect already derives the translation from the new panel.

Why tests missed it. The old carousel.browser-test.jsx and the jsdom port never let an interval fire, and jsdom reports offsetWidth as 0, so every translation is 0 there. Fake timers inside act() 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): translateX per autoplay tick, one loop and two loops, for 1 and 3 items per panel (12 items so there are 4 panels); and translateX after next/previous button, indicator and right-arrow navigation.
  • Checked against the code before each fix: the autoplay tests fail on the original, the two-loop tests fail with only the first fix, and 6 of the 8 navigation tests fail without the second.

Stories. WithAutoplay now sets isInfinite. ThreeItemsPerPanel has 12 items with unique ids (it reused two sample items, so two items shared an id), matching the browser test. Snapshot updated for ThreeItemsPerPanel only.

Not changed here: the non-infinite stop-at-end path still has no test, and stopAutoplay does not update state when only onRequestAutoplayToggle is passed.


CONTRIBUTOR checklist (do not remove)

Green gate:

  • npm run lint passes with 0 errors (4 existing warnings in carousel files)
  • npm run typecheck is clean
  • npm test -- --run passes: 59 files, 1273 tests (unit, browser and snapshots)
  • npm run build succeeds. The library build and .d.ts succeed, but check-package-exports in my local run reports utilities/hooks missing 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:

  • Added Vitest tests, with regression tests for both bugs. Real-browser tests are in carousel.browser.test.jsx.
  • Reviewed the stories in Storybook by eye. I checked behavior through the browser tests, not visually.
  • Story code uses the component's real prop API.
  • Storybook a11y panel checked. Not done.
  • No type or .d.ts changes.

REVIEWER checklist (do not remove)

  • CI is green
  • Change has adequate test coverage; bug fixes include a regression test.
  • Reviewed the affected Storybook stories and their a11y panels.
  • Rendered markup still conforms to SLDS (snapshot diff: ThreeItemsPerPanel only).
  • Public API / package surface: unchanged.

zigzagdev and others added 9 commits October 30, 2026 12:56
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]>
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]>
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]>
// 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 });

@interactivellama interactivellama Oct 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@interactivellama
interactivellama merged commit 02c4ebf into master Oct 30, 2026
10 checks passed
@interactivellama
interactivellama deleted the fix/carousel-autoplay-stale-closure branch October 30, 2026 23:10
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Carousel: autoplay stalls after one panel and slides lag behind the active panel

3 participants