Skip to content

carousel: subscribe before calling setApi - #214

Merged
deebov merged 1 commit into
mainfrom
fix/carousel-setapi-order
Sep 26, 2026
Merged

deebov merged 1 commit into
mainfrom
fix/carousel-setapi-order

Conversation

@deebov

@deebov deebov commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Problem

This is a regression from #211, found by the new browser behaviour tests (a-peak-works/untitledui-react#346). The effect that calls setApi was declared before the one that subscribes to Embla's select and reInit events, and effects run in declaration order. So a consumer that moves the carousel as soon as it gets the api did it before anyone was listening:

<Carousel.Root setApi={(api) => api?.scrollTo(2, true)}>

Prev/Next and the indicators then stayed on slide 1. Before #211, the subscribing effect also re-read the position, which hid the problem, but that re-read is the setState in an effect that the lint rules forbid.

Fix

Declare the subscribing effect first. setApi then runs with the listeners already attached. There's no extra render and no setState in an effect.

Verified

  • The behaviour test ScrollFromSetApi fails without this change and passes with it; each run rebuilt Storybook.
  • All 29 behaviour tests pass with the fix.
  • bun run test (type-check, lint and Prettier) passes.

🤖 Generated with Claude Code

The lint fix in #211 left the listeners in an effect declared after the one
that calls setApi. Effects run in order, so a consumer that scrolls as soon as
it gets the api (e.g. `setApi={(api) => api?.scrollTo(2, true)}`) moved the
carousel before anyone was listening. The triggers and indicators then stayed
on slide 1. Subscribing first fixes it, with no setState in an effect.
@deebov
deebov merged commit ddc0834 into main Sep 26, 2026
1 check passed
@deebov
deebov deleted the fix/carousel-setapi-order branch September 26, 2026 14:58
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.

1 participant