Skip to content

Commit cadfb67

Browse files
committed
fix(autoHeightBridge): use getMaxHeight for measurement and account for devicePixelRatio
- Simplify measureDocumentHeight by using getMaxHeight across scrollingElement, body, and html - Adjust posted height by dividing rawHeight by window.devicePixelRatio and rounding up to reduce redundant updates
1 parent 9fae5a4 commit cadfb67

File tree

1 file changed

+21
-34
lines changed

1 file changed

+21
-34
lines changed

src/constants/autoHeightBridge.ts

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -47,58 +47,45 @@ export const AUTO_HEIGHT_BRIDGE = `(() => {
4747
return 0;
4848
};
4949
50+
const getMaxHeight = (element) => {
51+
if (!element) {
52+
return 0;
53+
}
54+
return Math.max(
55+
element.scrollHeight || 0,
56+
element.offsetHeight || 0,
57+
element.clientHeight || 0
58+
);
59+
};
60+
5061
const measureDocumentHeight = () => {
5162
const scrollingElement = document.scrollingElement;
5263
const body = document.body;
5364
const html = document.documentElement;
5465
55-
let height = 0;
56-
57-
if (scrollingElement) {
58-
height = Math.max(
59-
height,
60-
scrollingElement.scrollHeight,
61-
scrollingElement.offsetHeight,
62-
scrollingElement.clientHeight,
63-
);
64-
}
65-
66-
if (body) {
67-
height = Math.max(
68-
height,
69-
body.scrollHeight,
70-
body.offsetHeight,
71-
body.clientHeight,
72-
);
73-
}
74-
75-
if (html) {
76-
height = Math.max(
77-
height,
78-
html.scrollHeight,
79-
html.offsetHeight,
80-
html.clientHeight,
81-
);
82-
}
83-
84-
return Math.ceil(height);
66+
return Math.max(
67+
getMaxHeight(scrollingElement),
68+
getMaxHeight(body),
69+
getMaxHeight(html)
70+
);
8571
};
8672
8773
const postHeight = (rawHeight) => {
8874
if (!rawHeight || rawHeight <= 0) {
8975
return;
9076
}
9177
92-
const nextHeight = Math.ceil(rawHeight);
78+
const pixelRatio = window.devicePixelRatio || 1;
79+
const adjustedHeight = Math.ceil(rawHeight / pixelRatio);
9380
94-
if (window.__AUTO_HEIGHT_LAST__ === nextHeight) {
81+
if (window.__AUTO_HEIGHT_LAST__ === adjustedHeight) {
9582
return;
9683
}
9784
98-
window.__AUTO_HEIGHT_LAST__ = nextHeight;
85+
window.__AUTO_HEIGHT_LAST__ = adjustedHeight;
9986
10087
try {
101-
window.ReactNativeWebView?.postMessage(String(nextHeight));
88+
window.ReactNativeWebView?.postMessage(String(adjustedHeight));
10289
} catch (error) {
10390
// no-op
10491
}

0 commit comments

Comments
 (0)