@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.mjs CHANGED
@@ -1178,6 +1178,86 @@ var AccordionItem = ({
1178
1178
  ] });
1179
1179
  };
1180
1180
 
1181
- export { Accordion, AccordionItem, DEFAULT_BILLING_ROUTE, DropdownVariant, Heading, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, ModalShell, Section, SegmentedControl, StatusBadge, UpgradePrompt, useFocusTrap };
1181
+ // src/TruncatedText/truncate.ts
1182
+ var ELLIPSIS = "\u2026";
1183
+ var MIN_TRUNCATABLE_CHARS = 4;
1184
+ var SCHEME_PREFIX = /^[a-z][a-z0-9+.-]*:\/\//i;
1185
+ var WWW_PREFIX = /^www\./i;
1186
+ var TRAILING_SLASH = /\/$/;
1187
+ function truncateMiddle(value, maxChars) {
1188
+ if (maxChars < MIN_TRUNCATABLE_CHARS) return value;
1189
+ if (value.length <= maxChars) return value;
1190
+ const budget = maxChars - 1;
1191
+ const head = Math.ceil(budget / 2);
1192
+ const tail = budget - head;
1193
+ return `${value.slice(0, head)}${ELLIPSIS}${value.slice(value.length - tail)}`;
1194
+ }
1195
+ function formatUrlForDisplay(value) {
1196
+ const withoutScheme = value.replace(SCHEME_PREFIX, "");
1197
+ const withoutWww = withoutScheme.replace(WWW_PREFIX, "");
1198
+ const trimmed = withoutWww.replace(TRAILING_SLASH, "");
1199
+ return trimmed.length > 0 ? trimmed : value;
1200
+ }
1201
+ var AVG_CHAR_WIDTH_RATIO = 0.58;
1202
+ var DEFAULT_FONT_SIZE2 = 13;
1203
+ var UNMEASURED = 0;
1204
+ var styles12 = StyleSheet.create({
1205
+ base: { fontSize: DEFAULT_FONT_SIZE2 },
1206
+ /** `minWidth: 0` lets the text shrink inside a flex row instead of forcing the row wider. */
1207
+ fill: { flexShrink: 1, minWidth: 0 }
1208
+ });
1209
+ function charBudget(width, fontSize) {
1210
+ if (width <= UNMEASURED) return UNMEASURED;
1211
+ return Math.floor(width / (fontSize * AVG_CHAR_WIDTH_RATIO));
1212
+ }
1213
+ var TruncatedText = ({
1214
+ value,
1215
+ isUrl = false,
1216
+ href,
1217
+ onPress,
1218
+ style,
1219
+ testID,
1220
+ accessibilityLabel,
1221
+ accessibilityHint
1222
+ }) => {
1223
+ const [width, setWidth] = useState(UNMEASURED);
1224
+ const hostRef = useRef(null);
1225
+ const onLayout = useCallback((e) => {
1226
+ setWidth(e.nativeEvent.layout.width);
1227
+ }, []);
1228
+ const attachTooltip = useCallback(
1229
+ (node) => {
1230
+ hostRef.current = node;
1231
+ if (Platform.OS !== "web" || node === null) return;
1232
+ node.setAttribute?.("title", value);
1233
+ },
1234
+ [value]
1235
+ );
1236
+ const flatStyle = useMemo(() => StyleSheet.flatten([styles12.base, style]), [style]);
1237
+ const fontSize = flatStyle.fontSize ?? DEFAULT_FONT_SIZE2;
1238
+ const display = useMemo(() => {
1239
+ const source = isUrl ? formatUrlForDisplay(value) : value;
1240
+ const budget = charBudget(width, fontSize);
1241
+ return budget === UNMEASURED ? source : truncateMiddle(source, budget);
1242
+ }, [value, isUrl, width, fontSize]);
1243
+ return /* @__PURE__ */ jsx(
1244
+ Text,
1245
+ {
1246
+ ref: attachTooltip,
1247
+ onLayout,
1248
+ numberOfLines: 1,
1249
+ style: [styles12.base, styles12.fill, style],
1250
+ testID,
1251
+ accessibilityRole: onPress !== void 0 || href !== void 0 ? "link" : "text",
1252
+ accessibilityLabel,
1253
+ accessibilityHint,
1254
+ onPress,
1255
+ ...Platform.OS === "web" && href !== void 0 ? { href } : {},
1256
+ children: display
1257
+ }
1258
+ );
1259
+ };
1260
+
1261
+ export { Accordion, AccordionItem, DEFAULT_BILLING_ROUTE, DropdownVariant, ELLIPSIS, Heading, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, ModalShell, Section, SegmentedControl, StatusBadge, TruncatedText, UpgradePrompt, formatUrlForDisplay, truncateMiddle, useFocusTrap };
1182
1262
  //# sourceMappingURL=index.mjs.map
1183
1263
  //# sourceMappingURL=index.mjs.map