Skip to content

Commit 55872e4

Browse files
thomasttvoclaude
authored andcommitted
Remove x,y from measureParentSize return type
measureParentSize uses a self-relative measureLayout call (view.measureLayout(view, cb)) to get the view's width and height. The x,y values from this call are always (0,0) on Paper and web since a view's position relative to itself is the origin. The downstream firstItemOffset calculation subtracted outerViewLayout.x/y which was always a no-op. Remove x,y from the return type (Layout -> Size), drop the dead subtractions in RecyclerView, and rename outerViewLayout to outerViewSize to reflect what it actually contains. Co-Authored-By: Claude Sonnet 4.5 (1M context) <noreply@anthropic.com>
1 parent 5a15a80 commit 55872e4

4 files changed

Lines changed: 28 additions & 20 deletions

File tree

src/__tests__/RecyclerView.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ jest.mock("../recyclerview/utils/measureLayout", () => {
1414
return {
1515
...originalModule,
1616
measureParentSize: jest.fn().mockImplementation(() => ({
17-
x: 0,
18-
y: 0,
1917
width: 399,
2018
height: 899,
2119
})),

src/recyclerview/RecyclerView.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -157,29 +157,30 @@ const RecyclerViewComponent = <T,>(
157157
*/
158158
useLayoutEffect(() => {
159159
if (internalViewRef.current && firstChildViewRef.current) {
160-
// Measure the outer and inner container layouts
161-
const outerViewLayout = measureParentSize(internalViewRef.current);
160+
// Measure the outer container size and inner container layout
161+
const outerViewSize = measureParentSize(internalViewRef.current);
162162
const firstChildViewLayout = measureFirstChildLayout(
163163
firstChildViewRef.current,
164164
internalViewRef.current
165165
);
166166

167-
containerViewSizeRef.current = outerViewLayout;
167+
containerViewSizeRef.current = outerViewSize;
168168

169-
// Calculate offset of first item
169+
// firstChildViewLayout is already relative to the outer container,
170+
// so its x/y directly gives the first item offset.
170171
const firstItemOffset = horizontal
171-
? firstChildViewLayout.x - outerViewLayout.x
172-
: firstChildViewLayout.y - outerViewLayout.y;
172+
? firstChildViewLayout.x
173+
: firstChildViewLayout.y;
173174

174175
// Update the RecyclerView manager with window dimensions
175176
recyclerViewManager.updateLayoutParams(
176177
{
177178
width: horizontal
178-
? outerViewLayout.width
179+
? outerViewSize.width
179180
: firstChildViewLayout.width,
180181
height: horizontal
181182
? firstChildViewLayout.height
182-
: outerViewLayout.height,
183+
: outerViewSize.height,
183184
},
184185
isHorizontalRTL && recyclerViewManager.hasLayout()
185186
? firstItemOffset -

src/recyclerview/utils/measureLayout.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ interface Layout {
77
height: number;
88
}
99

10+
interface Size {
11+
width: number;
12+
height: number;
13+
}
14+
1015
/**
1116
* Measures the layout of a view relative to itselft.
1217
* Using measure wasn't returing accurate values but this workaround does.
@@ -89,14 +94,15 @@ export function roundOffPixel(value: number): number {
8994
}
9095

9196
/**
92-
* Specific method for easier mocking
93-
* Measures the layout of parent of RecyclerView
94-
* Returns the x, y coordinates and dimensions of the view.
97+
* Measures the size of the RecyclerView's outer container.
98+
* Uses a self-relative measureLayout call to get width/height synchronously.
99+
*
95100
* @param view - The React Native View component to measure
96-
* @returns An object containing x, y, width, and height measurements
101+
* @returns An object containing width and height
97102
*/
98-
export function measureParentSize(view: View): Layout {
99-
return measureLayout(view, undefined);
103+
export function measureParentSize(view: View): Size {
104+
const layout = measureLayout(view, undefined);
105+
return { width: layout.width, height: layout.height };
100106
}
101107

102108
/**

src/recyclerview/utils/measureLayout.web.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ interface Layout {
55
height: number;
66
}
77

8+
interface Size {
9+
width: number;
10+
height: number;
11+
}
12+
813
/**
914
* Gets scroll offsets from up to 3 parent elements
1015
*/
@@ -43,12 +48,10 @@ export function roundOffPixel(value: number): number {
4348
}
4449

4550
/**
46-
* Measures the layout of parent of RecyclerView
51+
* Measures the size of the RecyclerView's outer container.
4752
*/
48-
export function measureParentSize(view: Element): Layout {
53+
export function measureParentSize(view: Element): Size {
4954
return {
50-
x: 0,
51-
y: 0,
5255
width: view.clientWidth,
5356
height: view.clientHeight,
5457
};

0 commit comments

Comments
 (0)