angular / angular/components

bug(virtual-scroll): scrollToIndex on viewport while inside cdkVirtualScrollingElement is wrong

Aperta
#33,063 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
area: cdk/scrolling gemini-triaged P3
Lingua principale
TypeScript
Stelle
25k
Fork
6.8k
Merge medio
1g 8h
PR unite (30g)
91

Descrizione

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

When scrolling by index using `scrollToIndex` on the viewport it doesn't take into account any content around the viewport when using `cdkVirtualScrollingElement`.
The logic calculates the offset only based on the `itemSize` without taking into account the viewport offset.

### Reproduction

StackBlitz link: https://stackblitz.com/edit/qmx1v9bc
Steps to reproduce:
1. In the `Regular` section clicking on the button `Scroll to item 50` correctly scrolls to item 50. This is a regular viewport without `cdkVirtualScrollingElement`.
2. In the `With cdkVirtualScrollingElement` section clicking on the button `Scroll to item 50 (not working)` it scrolls to item 48 instead of 50 as there is a 100px header above the viewport while it should scroll to item 50.
3. If the button `Scroll to item 50 (fixed)` is clicked the scrolling works as expected as I calculate the offset myself using `viewport.measureViewportOffset('top')` which is missing in `scrollToIndex`.

### Expected Behavior

It should scroll to the correct item when using `cdkVirtualScrollingElement`

### Actual Behavior

It scrolls without taking into account the viewport offset.

### Environment

- Angular: 21.2.6
- CDK/Material: 21.1.2
- Browser(s): Chrome 146
- Operating System (e.g. Windows, macOS, Ubuntu): Windows 11

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia dall’implementazione del viewport per lo scorrimento virtuale di CDK e dall’entry point scrollToIndex, quindi confronta il suo comportamento con measureViewportOffset('top') nella riproduzione StackBlitz fornita. Verifica la modifica con i casi regular viewport e cdkVirtualScrollingElement: scorrendo fino all’elemento 50, l’elemento 50 dovrebbe essere selezionato in entrambe le sezioni, anche con l’offset dell’intestazione di 100px.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
angular, typescript
Ambito
frontend
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
64/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.