Skip to content

Commit 2661aa6

Browse files
committed
Horizontal layout centers items within the row height
1 parent 06078af commit 2661aa6

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

packages/plugin-scroll/src/lib/strategies/base-strategy.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export abstract class BaseScrollStrategy {
3333
abstract getTotalContentSize(virtualItems: VirtualItem[]): Size;
3434
protected abstract getScrollOffset(viewport: ViewportMetrics): number;
3535
protected abstract getClientSize(viewport: ViewportMetrics): number;
36-
36+
3737
/**
3838
* Returns the item's extent along the scroll axis. Vertical layout uses height;
3939
* horizontal layout uses width. Used for visible range and end spacing calculations.
@@ -50,6 +50,15 @@ export abstract class BaseScrollStrategy {
5050
return (totalContentSize.width - _item.width) / 2;
5151
}
5252

53+
/**
54+
* Vertical centering offset for items shorter than the content max height.
55+
* Horizontal layout centers items within the row height; vertical layout keeps
56+
* items top-aligned and uses the default of 0.
57+
*/
58+
protected getCenteringOffsetY(_item: VirtualItem, _totalContentSize: Size | undefined): number {
59+
return 0;
60+
}
61+
5362
protected getVisibleRange(
5463
viewport: ViewportMetrics,
5564
virtualItems: VirtualItem[],
@@ -133,10 +142,11 @@ export abstract class BaseScrollStrategy {
133142

134143
virtualItems.forEach((item) => {
135144
const centeringOffsetX = this.getCenteringOffsetX(item, totalContentSize);
145+
const centeringOffsetY = this.getCenteringOffsetY(item, totalContentSize);
136146

137147
item.pageLayouts.forEach((page) => {
138148
const itemX = (item.x + centeringOffsetX) * scale;
139-
const itemY = item.y * scale;
149+
const itemY = (item.y + centeringOffsetY) * scale;
140150
const pageX = itemX + page.x * scale;
141151
const pageY = itemY + page.y * scale;
142152
const pageWidth = page.rotatedWidth * scale;
@@ -212,11 +222,12 @@ export abstract class BaseScrollStrategy {
212222
if (!pageLayout) return null;
213223

214224
const centeringOffsetX = this.getCenteringOffsetX(item, totalContentSize);
225+
const centeringOffsetY = this.getCenteringOffsetY(item, totalContentSize);
215226

216227
return {
217228
origin: {
218229
x: item.x + pageLayout.x + centeringOffsetX,
219-
y: item.y + pageLayout.y,
230+
y: item.y + pageLayout.y + centeringOffsetY,
220231
},
221232
size: {
222233
width: pageLayout.width,

packages/plugin-scroll/src/lib/strategies/horizontal-strategy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,13 @@ export class HorizontalScrollStrategy extends BaseScrollStrategy {
8282
protected getCenteringOffsetX(_item: VirtualItem, _totalContentSize: Size | undefined): number {
8383
return 0;
8484
}
85+
86+
/**
87+
* Horizontal rows visually center shorter items within the tallest row height.
88+
* Match that DOM layout so page-coordinate targeting lands on the rendered page.
89+
*/
90+
protected getCenteringOffsetY(item: VirtualItem, totalContentSize: Size | undefined): number {
91+
if (!totalContentSize || item.height >= totalContentSize.height) return 0;
92+
return (totalContentSize.height - item.height) / 2;
93+
}
8594
}

0 commit comments

Comments
 (0)