@dloizides/ui-layout 1.12.1 → 1.13.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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
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
+
3
24
  ## 1.12.1
4
25
 
5
26
  - **Fix (`TruncatedText` did not actually middle-truncate).** 1.12.0 measured its available
package/dist/index.d.mts CHANGED
@@ -285,12 +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>` itself. It does not fire under RN-web, so the measured width
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
289
290
  * stayed 0, the character budget stayed 0, and the component silently degraded to exactly
290
291
  * 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.
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.
294
295
  *
295
296
  * The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
296
297
  * and, on web, as the native tooltip.
package/dist/index.d.ts CHANGED
@@ -285,12 +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>` itself. It does not fire under RN-web, so the measured width
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
289
290
  * stayed 0, the character budget stayed 0, and the component silently degraded to exactly
290
291
  * 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.
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.
294
295
  *
295
296
  * The full, untruncated value always stays reachable: as the accessible name (`aria-label`)
296
297
  * and, on web, as the native tooltip.
package/dist/index.js CHANGED
@@ -4,8 +4,10 @@ var React8 = require('react');
4
4
  var reactNative = require('react-native');
5
5
  var uiFeedback = require('@dloizides/ui-feedback');
6
6
  var jsxRuntime = require('react/jsx-runtime');
7
+ var a11y = require('@dloizides/a11y');
7
8
  var reactDom = require('react-dom');
8
9
  var uiIcons = require('@dloizides/ui-icons');
10
+ var rnWebHooks = require('@dloizides/rn-web-hooks');
9
11
 
10
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
11
13
 
@@ -670,7 +672,6 @@ function useResolvedDropdownVariant(explicit) {
670
672
  const { width } = reactNative.useWindowDimensions();
671
673
  return resolveDropdownVariant(explicit, width, reactNative.Platform.OS);
672
674
  }
673
- var IS_WEB3 = reactNative.Platform.OS === "web";
674
675
  var BORDER_RADIUS3 = 8;
675
676
  var BORDER_WIDTH4 = 1;
676
677
  var BODY_FONT_SIZE2 = 14;
@@ -679,21 +680,8 @@ var MODAL_MIN_WIDTH = 200;
679
680
  var MODAL_MAX_HEIGHT = 300;
680
681
  var CONTAINER_PADDING_H = 12;
681
682
  var CONTAINER_PADDING_V = 10;
682
- var SR_ONLY_SIZE = 1;
683
- var SR_ONLY_OFFSET = -1;
684
- function buildHintProps(hint, hintId) {
685
- return IS_WEB3 ? { "aria-describedby": hintId } : { accessibilityHint: hint };
686
- }
687
683
  var styles8 = reactNative.StyleSheet.create({
688
684
  anchor: { position: "relative" },
689
- srOnly: {
690
- position: "absolute",
691
- width: SR_ONLY_SIZE,
692
- height: SR_ONLY_SIZE,
693
- margin: SR_ONLY_OFFSET,
694
- padding: 0,
695
- overflow: "hidden"
696
- },
697
685
  container: {
698
686
  borderWidth: BORDER_WIDTH4,
699
687
  borderRadius: BORDER_RADIUS3,
@@ -731,7 +719,13 @@ var ModalDropdown = ({
731
719
  const resolvedVariant = useResolvedDropdownVariant(variant);
732
720
  const isMenu = resolvedVariant === "menu" /* Menu */;
733
721
  const [isOpen, setIsOpen] = React8.useState(false);
734
- const hintId = `${testID}-hint`;
722
+ const { a11yProps, hintNode } = a11y.useA11y({
723
+ label: accessibilityLabel,
724
+ hint: accessibilityHint,
725
+ role: "button",
726
+ state: { expanded: isOpen },
727
+ testID
728
+ });
735
729
  const anchorRef = React8.useRef(null);
736
730
  const dialogRef = React8.useRef(null);
737
731
  useFocusTrap(dialogRef, isOpen && !isMenu);
@@ -782,22 +776,10 @@ var ModalDropdown = ({
782
776
  );
783
777
  const keyExtractor = React8.useCallback((item) => String(item.value), []);
784
778
  return /* @__PURE__ */ jsxRuntime.jsxs(reactNative.View, { ref: anchorRef, style: anchorStyle, children: [
785
- /* @__PURE__ */ jsxRuntime.jsx(
786
- reactNative.TouchableOpacity,
787
- {
788
- accessible: true,
789
- ...buildHintProps(accessibilityHint, hintId),
790
- accessibilityLabel,
791
- accessibilityRole: "button",
792
- accessibilityState: { expanded: isOpen },
793
- "aria-expanded": isOpen,
794
- style: containerStyle,
795
- testID,
796
- onPress: handleToggle,
797
- children: renderTrigger !== void 0 ? renderTrigger({ label: selectedLabel, isOpen }) : /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: [styles8.selectedText, { color: colors.text }], children: selectedLabel })
798
- }
799
- ),
800
- IS_WEB3 ? /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { nativeID: hintId, style: styles8.srOnly, testID: `${testID}-hint`, children: accessibilityHint }) : null,
779
+ /* @__PURE__ */ jsxRuntime.jsxs(reactNative.TouchableOpacity, { ...a11yProps, style: containerStyle, onPress: handleToggle, children: [
780
+ renderTrigger !== void 0 ? renderTrigger({ label: selectedLabel, isOpen }) : /* @__PURE__ */ jsxRuntime.jsx(reactNative.Text, { style: [styles8.selectedText, { color: colors.text }], children: selectedLabel }),
781
+ hintNode
782
+ ] }),
801
783
  isMenu && isOpen ? /* @__PURE__ */ jsxRuntime.jsx(
802
784
  InlineMenu,
803
785
  {
@@ -926,12 +908,8 @@ function enableAndroidLayoutAnimationOnce() {
926
908
  setter(true);
927
909
  }
928
910
  }
929
- function prefersReducedMotion() {
930
- if (reactNative.Platform.OS !== "web" || typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
931
- return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
932
- }
933
911
  function animateNextLayout() {
934
- if (prefersReducedMotion()) return;
912
+ if (rnWebHooks.prefersReducedMotion()) return;
935
913
  enableAndroidLayoutAnimationOnce();
936
914
  const configure = reactNative.LayoutAnimation.configureNext;
937
915
  if (typeof configure !== "function") return;
@@ -1055,10 +1033,6 @@ var styles11 = reactNative.StyleSheet.create({
1055
1033
  });
1056
1034
  var ACTIVATION_KEYS = ["Enter", " ", "Spacebar"];
1057
1035
  var INSTANT_DURATION_MS = 0;
1058
- function prefersReducedMotion2() {
1059
- if (reactNative.Platform.OS !== "web" || typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
1060
- return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
1061
- }
1062
1036
  var AccordionItem = ({
1063
1037
  id,
1064
1038
  title,
@@ -1080,14 +1054,15 @@ var AccordionItem = ({
1080
1054
  const bodyTestID = `${headerTestID}-body`;
1081
1055
  const chevronTestID = `${headerTestID}-chevron`;
1082
1056
  const [rotation] = React8.useState(() => new reactNative.Animated.Value(expanded ? ROTATE_EXPANDED : ROTATE_COLLAPSED));
1057
+ const reducedMotion = rnWebHooks.useReducedMotion();
1083
1058
  React8.useEffect(() => {
1084
1059
  reactNative.Animated.timing(rotation, {
1085
1060
  toValue: expanded ? ROTATE_EXPANDED : ROTATE_COLLAPSED,
1086
- duration: prefersReducedMotion2() ? INSTANT_DURATION_MS : ROTATE_DURATION_MS,
1061
+ duration: reducedMotion ? INSTANT_DURATION_MS : ROTATE_DURATION_MS,
1087
1062
  easing: reactNative.Easing.out(reactNative.Easing.ease),
1088
1063
  useNativeDriver: reactNative.Platform.OS !== "web"
1089
1064
  }).start();
1090
- }, [expanded, rotation]);
1065
+ }, [expanded, rotation, reducedMotion]);
1091
1066
  const rotate = rotation.interpolate({
1092
1067
  inputRange: [ROTATE_COLLAPSED, ROTATE_EXPANDED],
1093
1068
  outputRange: [COLLAPSED_DEG, boxed ? BOXED_EXPANDED_DEG : PLAIN_EXPANDED_DEG]
@@ -1204,16 +1179,48 @@ function formatUrlForDisplay(value) {
1204
1179
  const trimmed = withoutWww.replace(TRAILING_SLASH, "");
1205
1180
  return trimmed.length > 0 ? trimmed : value;
1206
1181
  }
1182
+ var UNMEASURED = 0;
1183
+ var IS_WEB3 = reactNative.Platform.OS === "web";
1184
+ function measureParentInnerWidth(node) {
1185
+ const parent = node.parentElement;
1186
+ if (parent === null) return UNMEASURED;
1187
+ const cs = window.getComputedStyle(parent);
1188
+ const padding = (parseFloat(cs.paddingLeft) || 0) + (parseFloat(cs.paddingRight) || 0);
1189
+ return Math.max(UNMEASURED, parent.clientWidth - padding);
1190
+ }
1191
+ function useAvailableWidth() {
1192
+ const [width, setWidth] = React8.useState(UNMEASURED);
1193
+ const nodeRef = React8.useRef(null);
1194
+ const measureRef = React8.useCallback((node) => {
1195
+ if (!IS_WEB3) return;
1196
+ nodeRef.current = node;
1197
+ if (nodeRef.current !== null) setWidth(measureParentInnerWidth(nodeRef.current));
1198
+ }, []);
1199
+ React8.useEffect(() => {
1200
+ if (!IS_WEB3) return void 0;
1201
+ const remeasure = () => {
1202
+ const node = nodeRef.current;
1203
+ if (node !== null) setWidth(measureParentInnerWidth(node));
1204
+ };
1205
+ window.addEventListener("resize", remeasure);
1206
+ return () => window.removeEventListener("resize", remeasure);
1207
+ }, []);
1208
+ const onLayout = React8.useCallback((event) => {
1209
+ if (IS_WEB3) return;
1210
+ setWidth(event.nativeEvent.layout.width);
1211
+ }, []);
1212
+ return { width, measureRef, onLayout };
1213
+ }
1207
1214
  var AVG_CHAR_WIDTH_RATIO = 0.58;
1208
1215
  var DEFAULT_FONT_SIZE2 = 13;
1209
- var UNMEASURED = 0;
1216
+ var UNMEASURED2 = 0;
1210
1217
  var styles12 = reactNative.StyleSheet.create({
1211
1218
  base: { fontSize: DEFAULT_FONT_SIZE2 },
1212
1219
  /** `minWidth: 0` lets the text shrink inside a flex row instead of forcing the row wider. */
1213
1220
  fill: { flexShrink: 1, minWidth: 0 }
1214
1221
  });
1215
1222
  function charBudget(width, fontSize) {
1216
- if (width <= UNMEASURED) return UNMEASURED;
1223
+ if (width <= UNMEASURED2) return UNMEASURED2;
1217
1224
  return Math.floor(width / (fontSize * AVG_CHAR_WIDTH_RATIO));
1218
1225
  }
1219
1226
  var TruncatedText = ({
@@ -1226,32 +1233,34 @@ var TruncatedText = ({
1226
1233
  accessibilityLabel,
1227
1234
  accessibilityHint
1228
1235
  }) => {
1229
- const [width, setWidth] = React8.useState(UNMEASURED);
1236
+ const { width, measureRef, onLayout } = useAvailableWidth();
1230
1237
  const hostRef = React8.useRef(null);
1231
- const onLayout = React8.useCallback((e) => {
1232
- setWidth(e.nativeEvent.layout.width);
1233
- }, []);
1234
- const attachTooltip = React8.useCallback(
1238
+ const attachRef = React8.useCallback(
1235
1239
  (node) => {
1236
1240
  hostRef.current = node;
1241
+ measureRef(node);
1237
1242
  if (reactNative.Platform.OS !== "web" || node === null) return;
1238
1243
  node.setAttribute?.("title", value);
1239
1244
  },
1240
- [value]
1245
+ [value, measureRef]
1246
+ );
1247
+ const flatStyle = React8.useMemo(
1248
+ () => reactNative.StyleSheet.flatten([styles12.base, style]),
1249
+ [style]
1241
1250
  );
1242
- const flatStyle = React8.useMemo(() => reactNative.StyleSheet.flatten([styles12.base, style]), [style]);
1243
1251
  const fontSize = flatStyle.fontSize ?? DEFAULT_FONT_SIZE2;
1244
1252
  const display = React8.useMemo(() => {
1245
1253
  const source = isUrl ? formatUrlForDisplay(value) : value;
1246
1254
  const budget = charBudget(width, fontSize);
1247
- return budget === UNMEASURED ? source : truncateMiddle(source, budget);
1255
+ return budget === UNMEASURED2 ? source : truncateMiddle(source, budget);
1248
1256
  }, [value, isUrl, width, fontSize]);
1249
- return /* @__PURE__ */ jsxRuntime.jsx(reactNative.View, { style: styles12.fill, onLayout, children: /* @__PURE__ */ jsxRuntime.jsx(
1257
+ return /* @__PURE__ */ jsxRuntime.jsx(
1250
1258
  reactNative.Text,
1251
1259
  {
1252
- ref: attachTooltip,
1260
+ ref: attachRef,
1261
+ onLayout,
1253
1262
  numberOfLines: 1,
1254
- style: [styles12.base, style],
1263
+ style: [styles12.base, styles12.fill, style],
1255
1264
  testID,
1256
1265
  accessibilityRole: onPress !== void 0 || href !== void 0 ? "link" : "text",
1257
1266
  accessibilityLabel,
@@ -1260,7 +1269,7 @@ var TruncatedText = ({
1260
1269
  ...reactNative.Platform.OS === "web" && href !== void 0 ? { href } : {},
1261
1270
  children: display
1262
1271
  }
1263
- ) });
1272
+ );
1264
1273
  };
1265
1274
 
1266
1275
  exports.Accordion = Accordion;