Skip to content

fix: scroll editor carousel to the snap containing the selected slide - #208

Open
imrraaj wants to merge 2 commits into
developfrom
fix/editor-selection-slides-to-scroll
Open

imrraaj wants to merge 2 commits into
developfrom
fix/editor-selection-slides-to-scroll

Conversation

@imrraaj

@imrraaj imrraaj commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Selecting a slide (or any block inside one, including from List View) in the editor scrolled the carousel to a different slide whenever Slides to scroll was greater than 1, pushing the selected block out of view. The selection-sync effect passed a slide index to Embla's scrollTo() / selectedScrollSnap(), which work in scroll snap indexes. This maps the slide to the snap that contains it before scrolling.

Type of change

  • Bug fix
  • New feature
  • Enhancement/refactor
  • Documentation update
  • Test update
  • Build/CI/tooling

Related issue(s)

Closes #201

What changed

  • viewport/edit.tsx: map the selected slide index to its scroll snap through Embla's internalEngine().slideRegistry (respects slidesToScroll, containScroll and loop) before calling selectedScrollSnap() / scrollTo(). This is the same lookup Embla uses internally for slide focus.
  • Editor-only: save() output, block attributes and the frontend view.js bundle are unchanged (verified by comparing build output with and without the change), so no deprecation is needed.

Breaking changes

Does this introduce a breaking change? If yes, describe the impact and migration path below.

  • Yes — migration path:
  • No

Testing

Describe how this was tested.

  • Unit tests
  • Manual testing
  • Cross-browser testing (if UI changes)

Test details:

  • Manual, in the editor: 6 image slides, 2 Columns (50%) style, Slides to scroll: 2. Selecting slide 2 keeps slides 1–2 in view (previously jumped to 3–4); selecting slides 3/4 shows 3–4 and 5/6 shows 5–6, including selection from List View.
  • Ran the old and new code against real Embla 8.6.0 across 3–7 slides, 1–3 slides per view, slidesToScroll 1–3, loop on/off, trimSnaps/keepSnaps and start/center alignment. With slidesToScroll: 1 the scroll target is identical in every case. Every case where it differs (slidesToScroll > 1) was one where the old target didn't contain the selected slide.

Screenshots / recordings

Screen.Recording.2026-10-06.at.5.09.09.PM.mov

Checklist

  • I have self-reviewed this PR
  • I have added/updated tests where needed
  • I have updated docs where needed
  • I have checked for breaking changes

@imrraaj imrraaj self-assigned this Oct 6, 2026
Copilot AI balanced review requested due to automatic review settings October 6, 2026 11:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The core regression path lacks automated test coverage.

Review effort: Balanced
Findings: 1 Low severity

Open (1)
What changed in this PR

Fixes editor carousel selection by mapping slide indexes to their containing Embla scroll snaps.

Changes:

  • Uses Embla’s slide registry to resolve the correct snap.
  • Prevents grouped-slide selections from scrolling out of view.
File Description
src/​blocks/​carousel/​viewport/​edit.tsx Maps selected slides to snap indexes before scrolling.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +196 to +198
const snapIndex = api
.internalEngine()
.slideRegistry.findIndex( ( slides ) => slides.includes( selectedSlideIndex ) );

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

the useSelect mock always returns no selected block, so the effect exits before reaching the snap mapping.
The real library runs in jsdom, where every size is 0, so it can't produce a grouped registry like [[0, 1], [2, 3]]. Covering this needs a new jest.mock( 'embla-carousel' ) with a stubbed internalEngine().slideRegistry, plus a selection mock that can be set per test.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The core grouped-slide mapping remains uncovered by automated tests, as noted in the unresolved review thread.

Review effort: Balanced
Findings: 1 Low severity

Open (1)

Comment thread src/blocks/carousel/viewport/edit.tsx
Copilot AI balanced review requested due to automatic review settings October 14, 2026 08:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The implementation matches Embla’s slide-to-snap mapping and includes focused regression coverage.

1 open finding

🧠 Review effort: Balanced

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.

[Bug]: Selecting a slide in the editor scrolls the carousel to the wrong slide when "Slides to scroll" > 1

3 participants