Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

util: Extract helper for off-screen width measurement.

Aman Agrawal6b1299ad285a
diff --git a/web/src/util.ts b/web/src/util.ts
index 49d85f8c20b..aef83b32b2a 100644
--- a/web/src/util.ts
+++ b/web/src/util.ts
@@ -599,13 +599,12 @@ export function parse_youtube_start_time(url: string): number | undefined {
return undefined;
}
-// Measure the maximum rendered width of a set of candidate text
-// strings. This is used to set CSS variables for column widths
-// that need to fit their content tightly. All candidates are
-// inserted as block-level children of a single hidden container
-// sized to max-content, so only one reflow is needed.
+// Make a hidden, max-content-sized container for measuring the
+// rendered width of content without affecting the page layout.
+// Append it where the content's CSS rules apply, measure, and
+// remove it.
/* istanbul ignore next */
-export let max_text_content_width = (candidates: string[], css_class?: string): number => {
+export function make_offscreen_measurement_container(): HTMLDivElement {
const container = document.createElement("div");
Object.assign(container.style, {
position: "absolute",
@@ -615,6 +614,17 @@ export let max_text_content_width = (candidates: string[], css_class?: string):
left: "-9999px",
top: "0",
});
+ return container;
+}
+
+// Measure the maximum rendered width of a set of candidate text
+// strings. This is used to set CSS variables for column widths
+// that need to fit their content tightly. All candidates are
+// inserted as block-level children of a single hidden container
+// sized to max-content, so only one reflow is needed.
+/* istanbul ignore next */
+export let max_text_content_width = (candidates: string[], css_class?: string): number => {
+ const container = make_offscreen_measurement_container();
for (const text of candidates) {
const child = document.createElement("div");