@dloizides/ui-layout 1.12.0 → 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 +36 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +44 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +44 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,41 @@
|
|
|
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
|
+
|
|
24
|
+
## 1.12.1
|
|
25
|
+
|
|
26
|
+
- **Fix (`TruncatedText` did not actually middle-truncate).** 1.12.0 measured its available
|
|
27
|
+
width with `onLayout` **on the `<Text>` itself, which never fires under RN-web**. The width
|
|
28
|
+
stayed 0, the character budget stayed 0, and the component silently fell back to rendering
|
|
29
|
+
the full string — i.e. to exactly the tail-only CSS ellipsis it exists to replace. It
|
|
30
|
+
compiled, typechecked and passed all its unit tests throughout, because the truncation
|
|
31
|
+
helpers are pure and were never the broken part; only a browser could see it. Measured in
|
|
32
|
+
Chrome at a 120px container: text rendered `timesofindia.indiatimes.com/city/delhi/delhi-…`
|
|
33
|
+
with **no `…` character present** — CSS clipping, not middle truncation.
|
|
34
|
+
|
|
35
|
+
The width is now measured on a wrapping `<View>`, which does fire `onLayout`. The overflow
|
|
36
|
+
guarantee was never affected (the element stayed inside its parent in both versions); what
|
|
37
|
+
was lost was the informative half of the behaviour.
|
|
38
|
+
|
|
3
39
|
## 1.12.0
|
|
4
40
|
|
|
5
41
|
- **New: `TruncatedText`** — a single-line text/link that middle-truncates to fit its container
|
package/dist/index.d.mts
CHANGED
|
@@ -285,6 +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>` 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
|
|
290
|
+
* stayed 0, the character budget stayed 0, and the component silently degraded to exactly
|
|
291
|
+
* the tail-only CSS truncation of (1) — compiling, typechecking and passing its unit tests
|
|
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.
|
|
288
295
|
*
|
|
289
296
|
* The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
|
|
290
297
|
* and, on web, as the native tooltip.
|
package/dist/index.d.ts
CHANGED
|
@@ -285,6 +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>` 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
|
|
290
|
+
* stayed 0, the character budget stayed 0, and the component silently degraded to exactly
|
|
291
|
+
* the tail-only CSS truncation of (1) — compiling, typechecking and passing its unit tests
|
|
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.
|
|
288
295
|
*
|
|
289
296
|
* The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
|
|
290
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
|
|
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 <=
|
|
1248
|
+
if (width <= UNMEASURED2) return UNMEASURED2;
|
|
1217
1249
|
return Math.floor(width / (fontSize * AVG_CHAR_WIDTH_RATIO));
|
|
1218
1250
|
}
|
|
1219
1251
|
var TruncatedText = ({
|
|
@@ -1226,30 +1258,31 @@ var TruncatedText = ({
|
|
|
1226
1258
|
accessibilityLabel,
|
|
1227
1259
|
accessibilityHint
|
|
1228
1260
|
}) => {
|
|
1229
|
-
const
|
|
1261
|
+
const { width, measureRef, onLayout } = useAvailableWidth();
|
|
1230
1262
|
const hostRef = React8.useRef(null);
|
|
1231
|
-
const
|
|
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 ===
|
|
1280
|
+
return budget === UNMEASURED2 ? source : truncateMiddle(source, budget);
|
|
1248
1281
|
}, [value, isUrl, width, fontSize]);
|
|
1249
1282
|
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
1250
1283
|
reactNative.Text,
|
|
1251
1284
|
{
|
|
1252
|
-
ref:
|
|
1285
|
+
ref: attachRef,
|
|
1253
1286
|
onLayout,
|
|
1254
1287
|
numberOfLines: 1,
|
|
1255
1288
|
style: [styles12.base, styles12.fill, style],
|