@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/dist/index.mjs CHANGED
@@ -1198,16 +1198,48 @@ function formatUrlForDisplay(value) {
1198
1198
  const trimmed = withoutWww.replace(TRAILING_SLASH, "");
1199
1199
  return trimmed.length > 0 ? trimmed : value;
1200
1200
  }
1201
+ var UNMEASURED = 0;
1202
+ var IS_WEB4 = Platform.OS === "web";
1203
+ function measureParentInnerWidth(node) {
1204
+ const parent = node.parentElement;
1205
+ if (parent === null) return UNMEASURED;
1206
+ const cs = window.getComputedStyle(parent);
1207
+ const padding = (parseFloat(cs.paddingLeft) || 0) + (parseFloat(cs.paddingRight) || 0);
1208
+ return Math.max(UNMEASURED, parent.clientWidth - padding);
1209
+ }
1210
+ function useAvailableWidth() {
1211
+ const [width, setWidth] = useState(UNMEASURED);
1212
+ const nodeRef = useRef(null);
1213
+ const measureRef = useCallback((node) => {
1214
+ if (!IS_WEB4) return;
1215
+ nodeRef.current = node;
1216
+ if (nodeRef.current !== null) setWidth(measureParentInnerWidth(nodeRef.current));
1217
+ }, []);
1218
+ useEffect(() => {
1219
+ if (!IS_WEB4) return void 0;
1220
+ const remeasure = () => {
1221
+ const node = nodeRef.current;
1222
+ if (node !== null) setWidth(measureParentInnerWidth(node));
1223
+ };
1224
+ window.addEventListener("resize", remeasure);
1225
+ return () => window.removeEventListener("resize", remeasure);
1226
+ }, []);
1227
+ const onLayout = useCallback((event) => {
1228
+ if (IS_WEB4) return;
1229
+ setWidth(event.nativeEvent.layout.width);
1230
+ }, []);
1231
+ return { width, measureRef, onLayout };
1232
+ }
1201
1233
  var AVG_CHAR_WIDTH_RATIO = 0.58;
1202
1234
  var DEFAULT_FONT_SIZE2 = 13;
1203
- var UNMEASURED = 0;
1235
+ var UNMEASURED2 = 0;
1204
1236
  var styles12 = StyleSheet.create({
1205
1237
  base: { fontSize: DEFAULT_FONT_SIZE2 },
1206
1238
  /** `minWidth: 0` lets the text shrink inside a flex row instead of forcing the row wider. */
1207
1239
  fill: { flexShrink: 1, minWidth: 0 }
1208
1240
  });
1209
1241
  function charBudget(width, fontSize) {
1210
- if (width <= UNMEASURED) return UNMEASURED;
1242
+ if (width <= UNMEASURED2) return UNMEASURED2;
1211
1243
  return Math.floor(width / (fontSize * AVG_CHAR_WIDTH_RATIO));
1212
1244
  }
1213
1245
  var TruncatedText = ({
@@ -1220,32 +1252,34 @@ var TruncatedText = ({
1220
1252
  accessibilityLabel,
1221
1253
  accessibilityHint
1222
1254
  }) => {
1223
- const [width, setWidth] = useState(UNMEASURED);
1255
+ const { width, measureRef, onLayout } = useAvailableWidth();
1224
1256
  const hostRef = useRef(null);
1225
- const onLayout = useCallback((e) => {
1226
- setWidth(e.nativeEvent.layout.width);
1227
- }, []);
1228
- const attachTooltip = useCallback(
1257
+ const attachRef = useCallback(
1229
1258
  (node) => {
1230
1259
  hostRef.current = node;
1260
+ measureRef(node);
1231
1261
  if (Platform.OS !== "web" || node === null) return;
1232
1262
  node.setAttribute?.("title", value);
1233
1263
  },
1234
- [value]
1264
+ [value, measureRef]
1265
+ );
1266
+ const flatStyle = useMemo(
1267
+ () => StyleSheet.flatten([styles12.base, style]),
1268
+ [style]
1235
1269
  );
1236
- const flatStyle = useMemo(() => StyleSheet.flatten([styles12.base, style]), [style]);
1237
1270
  const fontSize = flatStyle.fontSize ?? DEFAULT_FONT_SIZE2;
1238
1271
  const display = useMemo(() => {
1239
1272
  const source = isUrl ? formatUrlForDisplay(value) : value;
1240
1273
  const budget = charBudget(width, fontSize);
1241
- return budget === UNMEASURED ? source : truncateMiddle(source, budget);
1274
+ return budget === UNMEASURED2 ? source : truncateMiddle(source, budget);
1242
1275
  }, [value, isUrl, width, fontSize]);
1243
- return /* @__PURE__ */ jsx(View, { style: styles12.fill, onLayout, children: /* @__PURE__ */ jsx(
1276
+ return /* @__PURE__ */ jsx(
1244
1277
  Text,
1245
1278
  {
1246
- ref: attachTooltip,
1279
+ ref: attachRef,
1280
+ onLayout,
1247
1281
  numberOfLines: 1,
1248
- style: [styles12.base, style],
1282
+ style: [styles12.base, styles12.fill, style],
1249
1283
  testID,
1250
1284
  accessibilityRole: onPress !== void 0 || href !== void 0 ? "link" : "text",
1251
1285
  accessibilityLabel,
@@ -1254,7 +1288,7 @@ var TruncatedText = ({
1254
1288
  ...Platform.OS === "web" && href !== void 0 ? { href } : {},
1255
1289
  children: display
1256
1290
  }
1257
- ) });
1291
+ );
1258
1292
  };
1259
1293
 
1260
1294
  export { Accordion, AccordionItem, DEFAULT_BILLING_ROUTE, DropdownVariant, ELLIPSIS, Heading, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, ModalShell, Section, SegmentedControl, StatusBadge, TruncatedText, UpgradePrompt, formatUrlForDisplay, truncateMiddle, useFocusTrap };