Skip to content

fix: Patch scrollToIndex to scroll backwards#43

Merged
chiefcll merged 1 commit into
solid-tv:mainfrom
luccalaz:fix/scrollToIndex-not-scrolling-backwards
Jul 11, 2026
Merged

fix: Patch scrollToIndex to scroll backwards#43
chiefcll merged 1 commit into
solid-tv:mainfrom
luccalaz:fix/scrollToIndex-not-scrolling-backwards

Conversation

@luccalaz

Copy link
Copy Markdown
Contributor

Problem

Row.scrollToIndex / Column.scrollToIndex always scroll forward, even when the target index is behind the current one — selected updates but the container never scrolls back, leaving the selected child off-screen.

Repro: a Row with scroll="auto" and overflowing children, scrolled forward to some index n; call row.scrollToIndex(n - 1)selected becomes n - 1 but the row's x doesn't move.

Root cause

scrollToIndex overwrites this.selected and then calls the scroller without a lastSelected:

function scrollToIndex(index, options) {
    this.selected = index;
    scrollRow(index, this);
    ...
}

In withScrolling, an undefined lastSelected makes the move register as incrementing (isIncrementing = lastSelected === undefined || ...), so the next position is always computed in the forward direction and then clamped — moving backward, the clamp leaves the container where it was. Key navigation is unaffected because selectChild passes lastSelected through onSelectedChanged; only programmatic scrollToIndex calls lose the direction.

Fix

Capture the previous selected before overwriting it and pass it as lastSelected, so withScrolling resolves direction the same way it does for key navigation:

function scrollToIndex(index, options) {
    const lastSelected = this.selected;
    this.selected = index;
    scrollRow(index, this, undefined, lastSelected === index ? undefined : lastSelected);
    ...
}

When the index is unchanged, undefined is still passed on purpose: withScrolling early-returns on selected === lastSelected, and some callers use scrollToIndex with the current index to force positioning (e.g. after layout) — this keeps that behaviour intact.

Same fix applied to Column.scrollToIndex with scrollColumn.

@chiefcll chiefcll merged commit 4a1687d into solid-tv:main Jul 11, 2026
1 check passed
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.

2 participants