@dloizides/ui-layout 1.11.0 → 1.12.0

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,85 @@ 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
+ *
289
+ * The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
290
+ * and, on web, as the native tooltip.
291
+ */
292
+
293
+ interface TruncatedTextProps {
294
+ /** The full value. Always the accessible name and the web tooltip, however it is displayed. */
295
+ value: string;
296
+ /**
297
+ * Treat `value` as a URL: drop `scheme://`, a leading `www.` and a bare trailing slash so the
298
+ * character budget is spent on the host and the slug rather than on boilerplate.
299
+ */
300
+ isUrl?: boolean;
301
+ /** Web only: render a real `<a href>`, so middle-click and "copy link address" work. */
302
+ href?: string;
303
+ onPress?: () => void;
304
+ style?: TextStyle | TextStyle[];
305
+ testID: string;
306
+ accessibilityLabel: string;
307
+ accessibilityHint: string;
308
+ }
309
+ declare const TruncatedText: ({ value, isUrl, href, onPress, style, testID, accessibilityLabel, accessibilityHint, }: TruncatedTextProps) => React.ReactElement;
310
+
311
+ /**
312
+ * Pure truncation helpers for `TruncatedText`.
313
+ *
314
+ * WHY MIDDLE AND NOT TAIL: the component exists because full URLs (adverse-media article
315
+ * links in the AML console) blew out their row. The obvious fix — RN's `numberOfLines={1}`,
316
+ * which RN-web compiles to `text-overflow: ellipsis` — truncates at the TAIL, and a
317
+ * tail-truncated URL is useless: every URL from one publisher collapses to the same
318
+ * `https://timesofindia.indiatimes.com/city/de…` prefix, so the reader learns nothing about
319
+ * WHICH article it is. The informative parts sit at both ends — the HOST (who published it,
320
+ * the trust signal) and the SLUG (what it is about). The uninformative part is the middle
321
+ * path. So we drop the middle:
322
+ *
323
+ * timesofindia.indiatimes.com/…/delhi-court-orders-probe
324
+ *
325
+ * CSS cannot express that, so it is computed here in JS. The component still applies
326
+ * `numberOfLines={1}` on top as a hard backstop, so even if the width estimate is off the
327
+ * text can never overflow its container.
328
+ */
329
+ /** The single-character ellipsis (U+2026) — one glyph, so it costs one character of budget. */
330
+ declare const ELLIPSIS = "\u2026";
331
+ /**
332
+ * Middle-truncate `value` to at most `maxChars` characters, joining the kept head and tail
333
+ * with an ellipsis. Returns `value` unchanged when it already fits (or when `maxChars` is too
334
+ * small to leave anything readable, in which case the caller's `numberOfLines` backstop clamps
335
+ * it instead of us emitting a meaningless "a…z").
336
+ *
337
+ * The head keeps the extra character on an odd budget, because for both URLs and identifiers
338
+ * the left side is the more identifying one.
339
+ */
340
+ declare function truncateMiddle(value: string, maxChars: number): string;
341
+ /**
342
+ * Strip the parts of a URL a human never needs to read — the scheme, a leading `www.`, and a
343
+ * bare trailing slash — so the truncation budget is spent on the host and the slug instead.
344
+ * Non-URL values (opaque list identifiers) pass through untouched.
345
+ */
346
+ declare function formatUrlForDisplay(value: string): string;
347
+
269
348
  declare function useFocusTrap(containerRef: RefObject<View | null>, enabled: boolean): void;
270
349
 
271
350
  /** Default testIDs — match the apps' existing values so Playwright selectors keep working. */
@@ -324,4 +403,4 @@ declare const LAYOUT_I18N: {
324
403
  readonly upgradePromptDismissHint: "settings.billing.upgradePrompt.dismissHint";
325
404
  };
326
405
 
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 };
406
+ 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,85 @@ 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
+ *
289
+ * The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
290
+ * and, on web, as the native tooltip.
291
+ */
292
+
293
+ interface TruncatedTextProps {
294
+ /** The full value. Always the accessible name and the web tooltip, however it is displayed. */
295
+ value: string;
296
+ /**
297
+ * Treat `value` as a URL: drop `scheme://`, a leading `www.` and a bare trailing slash so the
298
+ * character budget is spent on the host and the slug rather than on boilerplate.
299
+ */
300
+ isUrl?: boolean;
301
+ /** Web only: render a real `<a href>`, so middle-click and "copy link address" work. */
302
+ href?: string;
303
+ onPress?: () => void;
304
+ style?: TextStyle | TextStyle[];
305
+ testID: string;
306
+ accessibilityLabel: string;
307
+ accessibilityHint: string;
308
+ }
309
+ declare const TruncatedText: ({ value, isUrl, href, onPress, style, testID, accessibilityLabel, accessibilityHint, }: TruncatedTextProps) => React.ReactElement;
310
+
311
+ /**
312
+ * Pure truncation helpers for `TruncatedText`.
313
+ *
314
+ * WHY MIDDLE AND NOT TAIL: the component exists because full URLs (adverse-media article
315
+ * links in the AML console) blew out their row. The obvious fix — RN's `numberOfLines={1}`,
316
+ * which RN-web compiles to `text-overflow: ellipsis` — truncates at the TAIL, and a
317
+ * tail-truncated URL is useless: every URL from one publisher collapses to the same
318
+ * `https://timesofindia.indiatimes.com/city/de…` prefix, so the reader learns nothing about
319
+ * WHICH article it is. The informative parts sit at both ends — the HOST (who published it,
320
+ * the trust signal) and the SLUG (what it is about). The uninformative part is the middle
321
+ * path. So we drop the middle:
322
+ *
323
+ * timesofindia.indiatimes.com/…/delhi-court-orders-probe
324
+ *
325
+ * CSS cannot express that, so it is computed here in JS. The component still applies
326
+ * `numberOfLines={1}` on top as a hard backstop, so even if the width estimate is off the
327
+ * text can never overflow its container.
328
+ */
329
+ /** The single-character ellipsis (U+2026) — one glyph, so it costs one character of budget. */
330
+ declare const ELLIPSIS = "\u2026";
331
+ /**
332
+ * Middle-truncate `value` to at most `maxChars` characters, joining the kept head and tail
333
+ * with an ellipsis. Returns `value` unchanged when it already fits (or when `maxChars` is too
334
+ * small to leave anything readable, in which case the caller's `numberOfLines` backstop clamps
335
+ * it instead of us emitting a meaningless "a…z").
336
+ *
337
+ * The head keeps the extra character on an odd budget, because for both URLs and identifiers
338
+ * the left side is the more identifying one.
339
+ */
340
+ declare function truncateMiddle(value: string, maxChars: number): string;
341
+ /**
342
+ * Strip the parts of a URL a human never needs to read — the scheme, a leading `www.`, and a
343
+ * bare trailing slash — so the truncation budget is spent on the host and the slug instead.
344
+ * Non-URL values (opaque list identifiers) pass through untouched.
345
+ */
346
+ declare function formatUrlForDisplay(value: string): string;
347
+
269
348
  declare function useFocusTrap(containerRef: RefObject<View | null>, enabled: boolean): void;
270
349
 
271
350
  /** Default testIDs — match the apps' existing values so Playwright selectors keep working. */
@@ -324,4 +403,4 @@ declare const LAYOUT_I18N: {
324
403
  readonly upgradePromptDismissHint: "settings.billing.upgradePrompt.dismissHint";
325
404
  };
326
405
 
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 };
406
+ 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,91 @@ 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(
1250
+ reactNative.Text,
1251
+ {
1252
+ ref: attachTooltip,
1253
+ onLayout,
1254
+ numberOfLines: 1,
1255
+ style: [styles12.base, styles12.fill, style],
1256
+ testID,
1257
+ accessibilityRole: onPress !== void 0 || href !== void 0 ? "link" : "text",
1258
+ accessibilityLabel,
1259
+ accessibilityHint,
1260
+ onPress,
1261
+ ...reactNative.Platform.OS === "web" && href !== void 0 ? { href } : {},
1262
+ children: display
1263
+ }
1264
+ );
1265
+ };
1266
+
1187
1267
  exports.Accordion = Accordion;
1188
1268
  exports.AccordionItem = AccordionItem;
1189
1269
  exports.DEFAULT_BILLING_ROUTE = DEFAULT_BILLING_ROUTE;
1190
1270
  exports.DropdownVariant = DropdownVariant;
1271
+ exports.ELLIPSIS = ELLIPSIS;
1191
1272
  exports.Heading = Heading;
1192
1273
  exports.LAYOUT_I18N = LAYOUT_I18N;
1193
1274
  exports.LAYOUT_TEST_IDS = LAYOUT_TEST_IDS;
@@ -1196,7 +1277,10 @@ exports.ModalShell = ModalShell;
1196
1277
  exports.Section = Section;
1197
1278
  exports.SegmentedControl = SegmentedControl;
1198
1279
  exports.StatusBadge = StatusBadge;
1280
+ exports.TruncatedText = TruncatedText;
1199
1281
  exports.UpgradePrompt = UpgradePrompt;
1282
+ exports.formatUrlForDisplay = formatUrlForDisplay;
1283
+ exports.truncateMiddle = truncateMiddle;
1200
1284
  exports.useFocusTrap = useFocusTrap;
1201
1285
  //# sourceMappingURL=index.js.map
1202
1286
  //# sourceMappingURL=index.js.map