@dloizides/ui-layout 1.12.1 → 1.12.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.12.2
4
+
5
+ - **Fix (`TruncatedText` STILL did not middle-truncate; 1.12.1's fix was the wrong mechanism).**
6
+ 1.12.1 moved the `onLayout` measurement from the `<Text>` to a wrapping `<View>`. Verified in
7
+ Chrome: that did not fire either, and neither did a `ResizeObserver` on the same node — zero
8
+ callbacks from all three mechanisms under react-native-web 0.21.
9
+
10
+ Width now comes from `useAvailableWidth`, which on web reads the **parent's** inner width
11
+ straight off the DOM in a ref callback, plus a `resize` listener. The parent is measured
12
+ rather than the element because the element shrink-wraps its own text, so measuring it would
13
+ feed the output back into the input and oscillate. Native keeps `onLayout`, where it works.
14
+
15
+ Confirmed in Chrome at four container widths — the same URL renders as
16
+ `timesofindia.indiatimes.com/cit…ties/articleshow/114237788.cms` (520px) down to
17
+ `timeso…88.cms` (120px), with no element exceeding its container at either a 1400px or a
18
+ 420px viewport.
19
+
20
+ Note for anyone extending this: unit tests cannot catch this class of bug here. The
21
+ truncation helpers are pure and passed throughout all three versions; what was broken was
22
+ whether the measurement ever ran in a real browser.
23
+
3
24
  ## 1.12.1
4
25
 
5
26
  - **Fix (`TruncatedText` did not actually middle-truncate).** 1.12.0 measured its available
package/dist/index.d.mts CHANGED
@@ -285,12 +285,13 @@ declare const AccordionItem: ({ id, title, right, children, defaultOpen: _defaul
285
285
  * estimate can be wrong for an unusual glyph mix. So `numberOfLines={1}` is ALSO applied,
286
286
  * as a hard backstop: if the estimate ever runs long, CSS clamps it. Overflow is then
287
287
  * impossible by construction rather than by tuning.
288
- * 4. `onLayout` on the `<Text>` itself. It does not fire under RN-web, so the measured width
288
+ * 4. `onLayout` (on the `<Text>` OR on a wrapping `<View>`), and `ResizeObserver`. None of the
289
+ * three delivered a single callback for this element under RN-web 0.21. The measured width
289
290
  * stayed 0, the character budget stayed 0, and the component silently degraded to exactly
290
291
  * the tail-only CSS truncation of (1) — compiling, typechecking and passing its unit tests
291
- * the whole time, because the helpers are pure and were never the broken part. The width is
292
- * measured on a wrapping `<View>`, which does fire, and the result was confirmed in a
293
- * browser rather than inferred.
292
+ * the whole time, because the helpers are pure and were never the broken part. Only a
293
+ * browser could catch it. Width now comes from `useAvailableWidth`, which reads the parent's
294
+ * inner width straight off the DOM on web; see that module for why the PARENT.
294
295
  *
295
296
  * The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
296
297
  * and, on web, as the native tooltip.
package/dist/index.d.ts CHANGED
@@ -285,12 +285,13 @@ declare const AccordionItem: ({ id, title, right, children, defaultOpen: _defaul
285
285
  * estimate can be wrong for an unusual glyph mix. So `numberOfLines={1}` is ALSO applied,
286
286
  * as a hard backstop: if the estimate ever runs long, CSS clamps it. Overflow is then
287
287
  * impossible by construction rather than by tuning.
288
- * 4. `onLayout` on the `<Text>` itself. It does not fire under RN-web, so the measured width
288
+ * 4. `onLayout` (on the `<Text>` OR on a wrapping `<View>`), and `ResizeObserver`. None of the
289
+ * three delivered a single callback for this element under RN-web 0.21. The measured width
289
290
  * stayed 0, the character budget stayed 0, and the component silently degraded to exactly
290
291
  * the tail-only CSS truncation of (1) — compiling, typechecking and passing its unit tests
291
- * the whole time, because the helpers are pure and were never the broken part. The width is
292
- * measured on a wrapping `<View>`, which does fire, and the result was confirmed in a
293
- * browser rather than inferred.
292
+ * the whole time, because the helpers are pure and were never the broken part. Only a
293
+ * browser could catch it. Width now comes from `useAvailableWidth`, which reads the parent's
294
+ * inner width straight off the DOM on web; see that module for why the PARENT.
294
295
  *
295
296
  * The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
296
297
  * and, on web, as the native tooltip.
package/dist/index.js CHANGED
@@ -1204,16 +1204,48 @@ function formatUrlForDisplay(value) {
1204
1204
  const trimmed = withoutWww.replace(TRAILING_SLASH, "");
1205
1205
  return trimmed.length > 0 ? trimmed : value;
1206
1206
  }
1207
+ var UNMEASURED = 0;
1208
+ var IS_WEB4 = reactNative.Platform.OS === "web";
1209
+ function measureParentInnerWidth(node) {
1210
+ const parent = node.parentElement;
1211
+ if (parent === null) return UNMEASURED;
1212
+ const cs = window.getComputedStyle(parent);
1213
+ const padding = (parseFloat(cs.paddingLeft) || 0) + (parseFloat(cs.paddingRight) || 0);
1214
+ return Math.max(UNMEASURED, parent.clientWidth - padding);
1215
+ }
1216
+ function useAvailableWidth() {
1217
+ const [width, setWidth] = React8.useState(UNMEASURED);
1218
+ const nodeRef = React8.useRef(null);
1219
+ const measureRef = React8.useCallback((node) => {
1220
+ if (!IS_WEB4) return;
1221
+ nodeRef.current = node;
1222
+ if (nodeRef.current !== null) setWidth(measureParentInnerWidth(nodeRef.current));
1223
+ }, []);
1224
+ React8.useEffect(() => {
1225
+ if (!IS_WEB4) return void 0;
1226
+ const remeasure = () => {
1227
+ const node = nodeRef.current;
1228
+ if (node !== null) setWidth(measureParentInnerWidth(node));
1229
+ };
1230
+ window.addEventListener("resize", remeasure);
1231
+ return () => window.removeEventListener("resize", remeasure);
1232
+ }, []);
1233
+ const onLayout = React8.useCallback((event) => {
1234
+ if (IS_WEB4) return;
1235
+ setWidth(event.nativeEvent.layout.width);
1236
+ }, []);
1237
+ return { width, measureRef, onLayout };
1238
+ }
1207
1239
  var AVG_CHAR_WIDTH_RATIO = 0.58;
1208
1240
  var DEFAULT_FONT_SIZE2 = 13;
1209
- var UNMEASURED = 0;
1241
+ var UNMEASURED2 = 0;
1210
1242
  var styles12 = reactNative.StyleSheet.create({
1211
1243
  base: { fontSize: DEFAULT_FONT_SIZE2 },
1212
1244
  /** `minWidth: 0` lets the text shrink inside a flex row instead of forcing the row wider. */
1213
1245
  fill: { flexShrink: 1, minWidth: 0 }
1214
1246
  });
1215
1247
  function charBudget(width, fontSize) {
1216
- if (width <= UNMEASURED) return UNMEASURED;
1248
+ if (width <= UNMEASURED2) return UNMEASURED2;
1217
1249
  return Math.floor(width / (fontSize * AVG_CHAR_WIDTH_RATIO));
1218
1250
  }
1219
1251
  var TruncatedText = ({
@@ -1226,32 +1258,34 @@ var TruncatedText = ({
1226
1258
  accessibilityLabel,
1227
1259
  accessibilityHint
1228
1260
  }) => {
1229
- const [width, setWidth] = React8.useState(UNMEASURED);
1261
+ const { width, measureRef, onLayout } = useAvailableWidth();
1230
1262
  const hostRef = React8.useRef(null);
1231
- const onLayout = React8.useCallback((e) => {
1232
- setWidth(e.nativeEvent.layout.width);
1233
- }, []);
1234
- const attachTooltip = React8.useCallback(
1263
+ const attachRef = React8.useCallback(
1235
1264
  (node) => {
1236
1265
  hostRef.current = node;
1266
+ measureRef(node);
1237
1267
  if (reactNative.Platform.OS !== "web" || node === null) return;
1238
1268
  node.setAttribute?.("title", value);
1239
1269
  },
1240
- [value]
1270
+ [value, measureRef]
1271
+ );
1272
+ const flatStyle = React8.useMemo(
1273
+ () => reactNative.StyleSheet.flatten([styles12.base, style]),
1274
+ [style]
1241
1275
  );
1242
- const flatStyle = React8.useMemo(() => reactNative.StyleSheet.flatten([styles12.base, style]), [style]);
1243
1276
  const fontSize = flatStyle.fontSize ?? DEFAULT_FONT_SIZE2;
1244
1277
  const display = React8.useMemo(() => {
1245
1278
  const source = isUrl ? formatUrlForDisplay(value) : value;
1246
1279
  const budget = charBudget(width, fontSize);
1247
- return budget === UNMEASURED ? source : truncateMiddle(source, budget);
1280
+ return budget === UNMEASURED2 ? source : truncateMiddle(source, budget);
1248
1281
  }, [value, isUrl, width, fontSize]);
1249
- return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles12.fill, onLayout, children: /* @__PURE__ */ jsxRuntime.jsx(
1282
+ return /* @__PURE__ */ jsxRuntime.jsx(
1250
1283
  reactNative.Text,
1251
1284
  {
1252
- ref: attachTooltip,
1285
+ ref: attachRef,
1286
+ onLayout,
1253
1287
  numberOfLines: 1,
1254
- style: [styles12.base, style],
1288
+ style: [styles12.base, styles12.fill, style],
1255
1289
  testID,
1256
1290
  accessibilityRole: onPress !== void 0 || href !== void 0 ? "link" : "text",
1257
1291
  accessibilityLabel,
@@ -1260,7 +1294,7 @@ var TruncatedText = ({
1260
1294
  ...reactNative.Platform.OS === "web" && href !== void 0 ? { href } : {},
1261
1295
  children: display
1262
1296
  }
1263
- ) });
1297
+ );
1264
1298
  };
1265
1299
 
1266
1300
  exports.Accordion = Accordion;