@dloizides/ui-layout 1.11.0 → 1.12.1

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.d.mts CHANGED
@@ -1,5 +1,5 @@
1
1
  import React, { RefObject } from 'react';
2
- import { ViewStyle, StyleProp, View } from 'react-native';
2
+ import { ViewStyle, StyleProp, TextStyle, View } from 'react-native';
3
3
 
4
4
  /**
5
5
  * Section — a bordered card container that themes its border + surface.
@@ -266,6 +266,91 @@ interface AccordionItemProps {
266
266
  }
267
267
  declare const AccordionItem: ({ id, title, right, children, defaultOpen: _defaultOpen, disabled, testID, accessibilityLabel, accessibilityHint, }: AccordionItemProps) => React.ReactElement;
268
268
 
269
+ /**
270
+ * TruncatedText — a single-line text/link that middle-truncates to fit its container and
271
+ * never overflows, at any width.
272
+ *
273
+ * THREE THINGS THAT LOOK LIKE THEY WOULD WORK AND DO NOT (all verified against
274
+ * react-native-web 0.21, not assumed):
275
+ *
276
+ * 1. `numberOfLines={1}` alone. RN-web compiles it to `text-overflow: ellipsis`, which only
277
+ * truncates at the TAIL. Every URL from one publisher then collapses to the same
278
+ * `timesofindia.indiatimes.com/city/de…` prefix — the reader cannot tell the articles
279
+ * apart. There is no CSS for middle truncation, so it is computed in JS here.
280
+ * 2. `title={full}` for the hover tooltip. RN-web's `<Text>` forwards exactly
281
+ * `href`, `lang` and `pointerEvents` beyond the shared whitelist — `title` is dropped
282
+ * silently. It is set on the host node through a ref instead (the same escape hatch
283
+ * `ui-buttons/useKeyboardActivation` already uses).
284
+ * 3. JS truncation alone. The character budget is estimated from the measured width, and an
285
+ * estimate can be wrong for an unusual glyph mix. So `numberOfLines={1}` is ALSO applied,
286
+ * as a hard backstop: if the estimate ever runs long, CSS clamps it. Overflow is then
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
289
+ * stayed 0, the character budget stayed 0, and the component silently degraded to exactly
290
+ * 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.
294
+ *
295
+ * The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
296
+ * and, on web, as the native tooltip.
297
+ */
298
+
299
+ interface TruncatedTextProps {
300
+ /** The full value. Always the accessible name and the web tooltip, however it is displayed. */
301
+ value: string;
302
+ /**
303
+ * Treat `value` as a URL: drop `scheme://`, a leading `www.` and a bare trailing slash so the
304
+ * character budget is spent on the host and the slug rather than on boilerplate.
305
+ */
306
+ isUrl?: boolean;
307
+ /** Web only: render a real `<a href>`, so middle-click and "copy link address" work. */
308
+ href?: string;
309
+ onPress?: () => void;
310
+ style?: TextStyle | TextStyle[];
311
+ testID: string;
312
+ accessibilityLabel: string;
313
+ accessibilityHint: string;
314
+ }
315
+ declare const TruncatedText: ({ value, isUrl, href, onPress, style, testID, accessibilityLabel, accessibilityHint, }: TruncatedTextProps) => React.ReactElement;
316
+
317
+ /**
318
+ * Pure truncation helpers for `TruncatedText`.
319
+ *
320
+ * WHY MIDDLE AND NOT TAIL: the component exists because full URLs (adverse-media article
321
+ * links in the AML console) blew out their row. The obvious fix — RN's `numberOfLines={1}`,
322
+ * which RN-web compiles to `text-overflow: ellipsis` — truncates at the TAIL, and a
323
+ * tail-truncated URL is useless: every URL from one publisher collapses to the same
324
+ * `https://timesofindia.indiatimes.com/city/de…` prefix, so the reader learns nothing about
325
+ * WHICH article it is. The informative parts sit at both ends — the HOST (who published it,
326
+ * the trust signal) and the SLUG (what it is about). The uninformative part is the middle
327
+ * path. So we drop the middle:
328
+ *
329
+ * timesofindia.indiatimes.com/…/delhi-court-orders-probe
330
+ *
331
+ * CSS cannot express that, so it is computed here in JS. The component still applies
332
+ * `numberOfLines={1}` on top as a hard backstop, so even if the width estimate is off the
333
+ * text can never overflow its container.
334
+ */
335
+ /** The single-character ellipsis (U+2026) — one glyph, so it costs one character of budget. */
336
+ declare const ELLIPSIS = "\u2026";
337
+ /**
338
+ * Middle-truncate `value` to at most `maxChars` characters, joining the kept head and tail
339
+ * with an ellipsis. Returns `value` unchanged when it already fits (or when `maxChars` is too
340
+ * small to leave anything readable, in which case the caller's `numberOfLines` backstop clamps
341
+ * it instead of us emitting a meaningless "a…z").
342
+ *
343
+ * The head keeps the extra character on an odd budget, because for both URLs and identifiers
344
+ * the left side is the more identifying one.
345
+ */
346
+ declare function truncateMiddle(value: string, maxChars: number): string;
347
+ /**
348
+ * Strip the parts of a URL a human never needs to read — the scheme, a leading `www.`, and a
349
+ * bare trailing slash — so the truncation budget is spent on the host and the slug instead.
350
+ * Non-URL values (opaque list identifiers) pass through untouched.
351
+ */
352
+ declare function formatUrlForDisplay(value: string): string;
353
+
269
354
  declare function useFocusTrap(containerRef: RefObject<View | null>, enabled: boolean): void;
270
355
 
271
356
  /** Default testIDs — match the apps' existing values so Playwright selectors keep working. */
@@ -324,4 +409,4 @@ declare const LAYOUT_I18N: {
324
409
  readonly upgradePromptDismissHint: "settings.billing.upgradePrompt.dismissHint";
325
410
  };
326
411
 
327
- export { Accordion, AccordionItem, type AccordionItemProps, type AccordionProps, type AccordionVariant, DEFAULT_BILLING_ROUTE, type DropdownOption, type DropdownTriggerState, DropdownVariant, Heading, type HeadingProps, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, type ModalDropdownProps, ModalShell, type ModalShellProps, Section, type SectionProps, SegmentedControl, type SegmentedControlProps, type SegmentedOption, StatusBadge, type StatusBadgeProps, UpgradePrompt, type UpgradePromptProps, useFocusTrap };
412
+ export { Accordion, AccordionItem, type AccordionItemProps, type AccordionProps, type AccordionVariant, DEFAULT_BILLING_ROUTE, type DropdownOption, type DropdownTriggerState, DropdownVariant, ELLIPSIS, Heading, type HeadingProps, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, type ModalDropdownProps, ModalShell, type ModalShellProps, Section, type SectionProps, SegmentedControl, type SegmentedControlProps, type SegmentedOption, StatusBadge, type StatusBadgeProps, TruncatedText, type TruncatedTextProps, UpgradePrompt, type UpgradePromptProps, formatUrlForDisplay, truncateMiddle, useFocusTrap };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import React, { RefObject } from 'react';
2
- import { ViewStyle, StyleProp, View } from 'react-native';
2
+ import { ViewStyle, StyleProp, TextStyle, View } from 'react-native';
3
3
 
4
4
  /**
5
5
  * Section — a bordered card container that themes its border + surface.
@@ -266,6 +266,91 @@ interface AccordionItemProps {
266
266
  }
267
267
  declare const AccordionItem: ({ id, title, right, children, defaultOpen: _defaultOpen, disabled, testID, accessibilityLabel, accessibilityHint, }: AccordionItemProps) => React.ReactElement;
268
268
 
269
+ /**
270
+ * TruncatedText — a single-line text/link that middle-truncates to fit its container and
271
+ * never overflows, at any width.
272
+ *
273
+ * THREE THINGS THAT LOOK LIKE THEY WOULD WORK AND DO NOT (all verified against
274
+ * react-native-web 0.21, not assumed):
275
+ *
276
+ * 1. `numberOfLines={1}` alone. RN-web compiles it to `text-overflow: ellipsis`, which only
277
+ * truncates at the TAIL. Every URL from one publisher then collapses to the same
278
+ * `timesofindia.indiatimes.com/city/de…` prefix — the reader cannot tell the articles
279
+ * apart. There is no CSS for middle truncation, so it is computed in JS here.
280
+ * 2. `title={full}` for the hover tooltip. RN-web's `<Text>` forwards exactly
281
+ * `href`, `lang` and `pointerEvents` beyond the shared whitelist — `title` is dropped
282
+ * silently. It is set on the host node through a ref instead (the same escape hatch
283
+ * `ui-buttons/useKeyboardActivation` already uses).
284
+ * 3. JS truncation alone. The character budget is estimated from the measured width, and an
285
+ * estimate can be wrong for an unusual glyph mix. So `numberOfLines={1}` is ALSO applied,
286
+ * as a hard backstop: if the estimate ever runs long, CSS clamps it. Overflow is then
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
289
+ * stayed 0, the character budget stayed 0, and the component silently degraded to exactly
290
+ * 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.
294
+ *
295
+ * The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
296
+ * and, on web, as the native tooltip.
297
+ */
298
+
299
+ interface TruncatedTextProps {
300
+ /** The full value. Always the accessible name and the web tooltip, however it is displayed. */
301
+ value: string;
302
+ /**
303
+ * Treat `value` as a URL: drop `scheme://`, a leading `www.` and a bare trailing slash so the
304
+ * character budget is spent on the host and the slug rather than on boilerplate.
305
+ */
306
+ isUrl?: boolean;
307
+ /** Web only: render a real `<a href>`, so middle-click and "copy link address" work. */
308
+ href?: string;
309
+ onPress?: () => void;
310
+ style?: TextStyle | TextStyle[];
311
+ testID: string;
312
+ accessibilityLabel: string;
313
+ accessibilityHint: string;
314
+ }
315
+ declare const TruncatedText: ({ value, isUrl, href, onPress, style, testID, accessibilityLabel, accessibilityHint, }: TruncatedTextProps) => React.ReactElement;
316
+
317
+ /**
318
+ * Pure truncation helpers for `TruncatedText`.
319
+ *
320
+ * WHY MIDDLE AND NOT TAIL: the component exists because full URLs (adverse-media article
321
+ * links in the AML console) blew out their row. The obvious fix — RN's `numberOfLines={1}`,
322
+ * which RN-web compiles to `text-overflow: ellipsis` — truncates at the TAIL, and a
323
+ * tail-truncated URL is useless: every URL from one publisher collapses to the same
324
+ * `https://timesofindia.indiatimes.com/city/de…` prefix, so the reader learns nothing about
325
+ * WHICH article it is. The informative parts sit at both ends — the HOST (who published it,
326
+ * the trust signal) and the SLUG (what it is about). The uninformative part is the middle
327
+ * path. So we drop the middle:
328
+ *
329
+ * timesofindia.indiatimes.com/…/delhi-court-orders-probe
330
+ *
331
+ * CSS cannot express that, so it is computed here in JS. The component still applies
332
+ * `numberOfLines={1}` on top as a hard backstop, so even if the width estimate is off the
333
+ * text can never overflow its container.
334
+ */
335
+ /** The single-character ellipsis (U+2026) — one glyph, so it costs one character of budget. */
336
+ declare const ELLIPSIS = "\u2026";
337
+ /**
338
+ * Middle-truncate `value` to at most `maxChars` characters, joining the kept head and tail
339
+ * with an ellipsis. Returns `value` unchanged when it already fits (or when `maxChars` is too
340
+ * small to leave anything readable, in which case the caller's `numberOfLines` backstop clamps
341
+ * it instead of us emitting a meaningless "a…z").
342
+ *
343
+ * The head keeps the extra character on an odd budget, because for both URLs and identifiers
344
+ * the left side is the more identifying one.
345
+ */
346
+ declare function truncateMiddle(value: string, maxChars: number): string;
347
+ /**
348
+ * Strip the parts of a URL a human never needs to read — the scheme, a leading `www.`, and a
349
+ * bare trailing slash — so the truncation budget is spent on the host and the slug instead.
350
+ * Non-URL values (opaque list identifiers) pass through untouched.
351
+ */
352
+ declare function formatUrlForDisplay(value: string): string;
353
+
269
354
  declare function useFocusTrap(containerRef: RefObject<View | null>, enabled: boolean): void;
270
355
 
271
356
  /** Default testIDs — match the apps' existing values so Playwright selectors keep working. */
@@ -324,4 +409,4 @@ declare const LAYOUT_I18N: {
324
409
  readonly upgradePromptDismissHint: "settings.billing.upgradePrompt.dismissHint";
325
410
  };
326
411
 
327
- export { Accordion, AccordionItem, type AccordionItemProps, type AccordionProps, type AccordionVariant, DEFAULT_BILLING_ROUTE, type DropdownOption, type DropdownTriggerState, DropdownVariant, Heading, type HeadingProps, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, type ModalDropdownProps, ModalShell, type ModalShellProps, Section, type SectionProps, SegmentedControl, type SegmentedControlProps, type SegmentedOption, StatusBadge, type StatusBadgeProps, UpgradePrompt, type UpgradePromptProps, useFocusTrap };
412
+ export { Accordion, AccordionItem, type AccordionItemProps, type AccordionProps, type AccordionVariant, DEFAULT_BILLING_ROUTE, type DropdownOption, type DropdownTriggerState, DropdownVariant, ELLIPSIS, Heading, type HeadingProps, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, type ModalDropdownProps, ModalShell, type ModalShellProps, Section, type SectionProps, SegmentedControl, type SegmentedControlProps, type SegmentedOption, StatusBadge, type StatusBadgeProps, TruncatedText, type TruncatedTextProps, UpgradePrompt, type UpgradePromptProps, formatUrlForDisplay, truncateMiddle, useFocusTrap };
package/dist/index.js CHANGED
@@ -1184,10 +1184,90 @@ var AccordionItem = ({
1184
1184
  ] });
1185
1185
  };
1186
1186
 
1187
+ // src/TruncatedText/truncate.ts
1188
+ var ELLIPSIS = "\u2026";
1189
+ var MIN_TRUNCATABLE_CHARS = 4;
1190
+ var SCHEME_PREFIX = /^[a-z][a-z0-9+.-]*:\/\//i;
1191
+ var WWW_PREFIX = /^www\./i;
1192
+ var TRAILING_SLASH = /\/$/;
1193
+ function truncateMiddle(value, maxChars) {
1194
+ if (maxChars < MIN_TRUNCATABLE_CHARS) return value;
1195
+ if (value.length <= maxChars) return value;
1196
+ const budget = maxChars - 1;
1197
+ const head = Math.ceil(budget / 2);
1198
+ const tail = budget - head;
1199
+ return `${value.slice(0, head)}${ELLIPSIS}${value.slice(value.length - tail)}`;
1200
+ }
1201
+ function formatUrlForDisplay(value) {
1202
+ const withoutScheme = value.replace(SCHEME_PREFIX, "");
1203
+ const withoutWww = withoutScheme.replace(WWW_PREFIX, "");
1204
+ const trimmed = withoutWww.replace(TRAILING_SLASH, "");
1205
+ return trimmed.length > 0 ? trimmed : value;
1206
+ }
1207
+ var AVG_CHAR_WIDTH_RATIO = 0.58;
1208
+ var DEFAULT_FONT_SIZE2 = 13;
1209
+ var UNMEASURED = 0;
1210
+ var styles12 = reactNative.StyleSheet.create({
1211
+ base: { fontSize: DEFAULT_FONT_SIZE2 },
1212
+ /** `minWidth: 0` lets the text shrink inside a flex row instead of forcing the row wider. */
1213
+ fill: { flexShrink: 1, minWidth: 0 }
1214
+ });
1215
+ function charBudget(width, fontSize) {
1216
+ if (width <= UNMEASURED) return UNMEASURED;
1217
+ return Math.floor(width / (fontSize * AVG_CHAR_WIDTH_RATIO));
1218
+ }
1219
+ var TruncatedText = ({
1220
+ value,
1221
+ isUrl = false,
1222
+ href,
1223
+ onPress,
1224
+ style,
1225
+ testID,
1226
+ accessibilityLabel,
1227
+ accessibilityHint
1228
+ }) => {
1229
+ const [width, setWidth] = React8.useState(UNMEASURED);
1230
+ const hostRef = React8.useRef(null);
1231
+ const onLayout = React8.useCallback((e) => {
1232
+ setWidth(e.nativeEvent.layout.width);
1233
+ }, []);
1234
+ const attachTooltip = React8.useCallback(
1235
+ (node) => {
1236
+ hostRef.current = node;
1237
+ if (reactNative.Platform.OS !== "web" || node === null) return;
1238
+ node.setAttribute?.("title", value);
1239
+ },
1240
+ [value]
1241
+ );
1242
+ const flatStyle = React8.useMemo(() => reactNative.StyleSheet.flatten([styles12.base, style]), [style]);
1243
+ const fontSize = flatStyle.fontSize ?? DEFAULT_FONT_SIZE2;
1244
+ const display = React8.useMemo(() => {
1245
+ const source = isUrl ? formatUrlForDisplay(value) : value;
1246
+ const budget = charBudget(width, fontSize);
1247
+ return budget === UNMEASURED ? source : truncateMiddle(source, budget);
1248
+ }, [value, isUrl, width, fontSize]);
1249
+ return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles12.fill, onLayout, children: /* @__PURE__ */ jsxRuntime.jsx(
1250
+ reactNative.Text,
1251
+ {
1252
+ ref: attachTooltip,
1253
+ numberOfLines: 1,
1254
+ style: [styles12.base, style],
1255
+ testID,
1256
+ accessibilityRole: onPress !== void 0 || href !== void 0 ? "link" : "text",
1257
+ accessibilityLabel,
1258
+ accessibilityHint,
1259
+ onPress,
1260
+ ...reactNative.Platform.OS === "web" && href !== void 0 ? { href } : {},
1261
+ children: display
1262
+ }
1263
+ ) });
1264
+ };
1265
+
1187
1266
  exports.Accordion = Accordion;
1188
1267
  exports.AccordionItem = AccordionItem;
1189
1268
  exports.DEFAULT_BILLING_ROUTE = DEFAULT_BILLING_ROUTE;
1190
1269
  exports.DropdownVariant = DropdownVariant;
1270
+ exports.ELLIPSIS = ELLIPSIS;
1191
1271
  exports.Heading = Heading;
1192
1272
  exports.LAYOUT_I18N = LAYOUT_I18N;
1193
1273
  exports.LAYOUT_TEST_IDS = LAYOUT_TEST_IDS;
@@ -1196,7 +1276,10 @@ exports.ModalShell = ModalShell;
1196
1276
  exports.Section = Section;
1197
1277
  exports.SegmentedControl = SegmentedControl;
1198
1278
  exports.StatusBadge = StatusBadge;
1279
+ exports.TruncatedText = TruncatedText;
1199
1280
  exports.UpgradePrompt = UpgradePrompt;
1281
+ exports.formatUrlForDisplay = formatUrlForDisplay;
1282
+ exports.truncateMiddle = truncateMiddle;
1200
1283
  exports.useFocusTrap = useFocusTrap;
1201
1284
  //# sourceMappingURL=index.js.map
1202
1285
  //# sourceMappingURL=index.js.map