Skip to content

Commit 8b16cf0

Browse files
committed
fix(autoHeightBridge): normalize measured CSS height and post stable pixel value
- add normalizeHeight() to convert CSS-measured height into device/viewport-adjusted pixels (uses window.devicePixelRatio and visualViewport.scale, clamps to safe int) - store raw CSS height in lastCssHeight separately from posted lastHeight - only post when the normalized height changes to reduce redundant messages - ensure non-finite/zero values are ignored
1 parent 7f9bafe commit 8b16cf0

1 file changed

Lines changed: 46 additions & 3 deletions

File tree

src/constants/autoHeightBridge.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
4141
microtask: false,
4242
pendingLoads: 0,
4343
lastHeight: 0,
44+
lastCssHeight: 0,
4445
fallbackTimer: null,
4546
fallbackDelay: INITIAL_FALLBACK_MS,
4647
cleanup: [],
@@ -223,21 +224,63 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
223224
return Math.max(0, Math.ceil(readMaxValue(values)));
224225
};
225226
227+
var normalizeHeight = function (height) {
228+
if (!height || !isFinite(height) || height <= 0) {
229+
return 0;
230+
}
231+
232+
var pixelRatio =
233+
typeof window.devicePixelRatio === 'number' &&
234+
window.devicePixelRatio > 0
235+
? window.devicePixelRatio
236+
: 1;
237+
238+
var viewport = window.visualViewport;
239+
var viewportScale =
240+
viewport && typeof viewport.scale === 'number' && viewport.scale > 0
241+
? viewport.scale
242+
: 1;
243+
244+
var density = pixelRatio / viewportScale;
245+
if (!density || density <= 0) {
246+
density = 1;
247+
}
248+
249+
var normalized = Math.ceil(height / density + 0.01);
250+
251+
if (!isFinite(normalized) || normalized <= 0) {
252+
return 0;
253+
}
254+
255+
if (normalized > 2147483647) {
256+
normalized = 2147483647;
257+
}
258+
259+
return normalized;
260+
};
261+
226262
var postHeight = function (height) {
227263
if (!height || height <= 0) {
228264
return;
229265
}
230266
231-
if (state.lastHeight === height) {
267+
state.lastCssHeight = height;
268+
269+
var normalized = normalizeHeight(height);
270+
if (!normalized) {
271+
return;
272+
}
273+
274+
if (state.lastHeight === normalized) {
232275
return;
233276
}
234277
235-
state.lastHeight = height;
278+
state.lastHeight = normalized;
236279
237280
try {
238281
var channel = window.ReactNativeWebView;
239282
if (channel && typeof channel.postMessage === 'function') {
240-
channel.postMessage(String(height));
283+
channel.postMessage(String(normalized));
241284
}
242285
} catch (error) {
243286
// no-op

0 commit comments

Comments
 (0)