@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/dist/index.mjs CHANGED
@@ -2,8 +2,10 @@ import React8, { createContext, useMemo, useRef, useEffect, useState, useCallbac
2
2
  import { StyleSheet, Platform, View, Text, Pressable, TouchableOpacity, Modal, FlatList, ScrollView, Animated, Easing, useWindowDimensions, LayoutAnimation, UIManager } from 'react-native';
3
3
  import { MODAL_OVERLAY_COLOR, useUi } from '@dloizides/ui-feedback';
4
4
  import { jsx, jsxs } from 'react/jsx-runtime';
5
+ import { useA11y } from '@dloizides/a11y';
5
6
  import { createPortal } from 'react-dom';
6
7
  import { SvgIcon } from '@dloizides/ui-icons';
8
+ import { useReducedMotion, prefersReducedMotion } from '@dloizides/rn-web-hooks';
7
9
 
8
10
  // src/Section/Section.tsx
9
11
  var styles = StyleSheet.create({
@@ -664,7 +666,6 @@ function useResolvedDropdownVariant(explicit) {
664
666
  const { width } = useWindowDimensions();
665
667
  return resolveDropdownVariant(explicit, width, Platform.OS);
666
668
  }
667
- var IS_WEB3 = Platform.OS === "web";
668
669
  var BORDER_RADIUS3 = 8;
669
670
  var BORDER_WIDTH4 = 1;
670
671
  var BODY_FONT_SIZE2 = 14;
@@ -673,21 +674,8 @@ var MODAL_MIN_WIDTH = 200;
673
674
  var MODAL_MAX_HEIGHT = 300;
674
675
  var CONTAINER_PADDING_H = 12;
675
676
  var CONTAINER_PADDING_V = 10;
676
- var SR_ONLY_SIZE = 1;
677
- var SR_ONLY_OFFSET = -1;
678
- function buildHintProps(hint, hintId) {
679
- return IS_WEB3 ? { "aria-describedby": hintId } : { accessibilityHint: hint };
680
- }
681
677
  var styles8 = StyleSheet.create({
682
678
  anchor: { position: "relative" },
683
- srOnly: {
684
- position: "absolute",
685
- width: SR_ONLY_SIZE,
686
- height: SR_ONLY_SIZE,
687
- margin: SR_ONLY_OFFSET,
688
- padding: 0,
689
- overflow: "hidden"
690
- },
691
679
  container: {
692
680
  borderWidth: BORDER_WIDTH4,
693
681
  borderRadius: BORDER_RADIUS3,
@@ -725,7 +713,13 @@ var ModalDropdown = ({
725
713
  const resolvedVariant = useResolvedDropdownVariant(variant);
726
714
  const isMenu = resolvedVariant === "menu" /* Menu */;
727
715
  const [isOpen, setIsOpen] = useState(false);
728
- const hintId = `${testID}-hint`;
716
+ const { a11yProps, hintNode } = useA11y({
717
+ label: accessibilityLabel,
718
+ hint: accessibilityHint,
719
+ role: "button",
720
+ state: { expanded: isOpen },
721
+ testID
722
+ });
729
723
  const anchorRef = useRef(null);
730
724
  const dialogRef = useRef(null);
731
725
  useFocusTrap(dialogRef, isOpen && !isMenu);
@@ -776,22 +770,10 @@ var ModalDropdown = ({
776
770
  );
777
771
  const keyExtractor = useCallback((item) => String(item.value), []);
778
772
  return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: anchorStyle, children: [
779
- /* @__PURE__ */ jsx(
780
- TouchableOpacity,
781
- {
782
- accessible: true,
783
- ...buildHintProps(accessibilityHint, hintId),
784
- accessibilityLabel,
785
- accessibilityRole: "button",
786
- accessibilityState: { expanded: isOpen },
787
- "aria-expanded": isOpen,
788
- style: containerStyle,
789
- testID,
790
- onPress: handleToggle,
791
- children: renderTrigger !== void 0 ? renderTrigger({ label: selectedLabel, isOpen }) : /* @__PURE__ */ jsx(Text, { style: [styles8.selectedText, { color: colors.text }], children: selectedLabel })
792
- }
793
- ),
794
- IS_WEB3 ? /* @__PURE__ */ jsx(Text, { nativeID: hintId, style: styles8.srOnly, testID: `${testID}-hint`, children: accessibilityHint }) : null,
773
+ /* @__PURE__ */ jsxs(TouchableOpacity, { ...a11yProps, style: containerStyle, onPress: handleToggle, children: [
774
+ renderTrigger !== void 0 ? renderTrigger({ label: selectedLabel, isOpen }) : /* @__PURE__ */ jsx(Text, { style: [styles8.selectedText, { color: colors.text }], children: selectedLabel }),
775
+ hintNode
776
+ ] }),
795
777
  isMenu && isOpen ? /* @__PURE__ */ jsx(
796
778
  InlineMenu,
797
779
  {
@@ -920,10 +902,6 @@ function enableAndroidLayoutAnimationOnce() {
920
902
  setter(true);
921
903
  }
922
904
  }
923
- function prefersReducedMotion() {
924
- if (Platform.OS !== "web" || typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
925
- return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
926
- }
927
905
  function animateNextLayout() {
928
906
  if (prefersReducedMotion()) return;
929
907
  enableAndroidLayoutAnimationOnce();
@@ -1049,10 +1027,6 @@ var styles11 = StyleSheet.create({
1049
1027
  });
1050
1028
  var ACTIVATION_KEYS = ["Enter", " ", "Spacebar"];
1051
1029
  var INSTANT_DURATION_MS = 0;
1052
- function prefersReducedMotion2() {
1053
- if (Platform.OS !== "web" || typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
1054
- return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
1055
- }
1056
1030
  var AccordionItem = ({
1057
1031
  id,
1058
1032
  title,
@@ -1074,14 +1048,15 @@ var AccordionItem = ({
1074
1048
  const bodyTestID = `${headerTestID}-body`;
1075
1049
  const chevronTestID = `${headerTestID}-chevron`;
1076
1050
  const [rotation] = useState(() => new Animated.Value(expanded ? ROTATE_EXPANDED : ROTATE_COLLAPSED));
1051
+ const reducedMotion = useReducedMotion();
1077
1052
  useEffect(() => {
1078
1053
  Animated.timing(rotation, {
1079
1054
  toValue: expanded ? ROTATE_EXPANDED : ROTATE_COLLAPSED,
1080
- duration: prefersReducedMotion2() ? INSTANT_DURATION_MS : ROTATE_DURATION_MS,
1055
+ duration: reducedMotion ? INSTANT_DURATION_MS : ROTATE_DURATION_MS,
1081
1056
  easing: Easing.out(Easing.ease),
1082
1057
  useNativeDriver: Platform.OS !== "web"
1083
1058
  }).start();
1084
- }, [expanded, rotation]);
1059
+ }, [expanded, rotation, reducedMotion]);
1085
1060
  const rotate = rotation.interpolate({
1086
1061
  inputRange: [ROTATE_COLLAPSED, ROTATE_EXPANDED],
1087
1062
  outputRange: [COLLAPSED_DEG, boxed ? BOXED_EXPANDED_DEG : PLAIN_EXPANDED_DEG]
@@ -1198,16 +1173,48 @@ function formatUrlForDisplay(value) {
1198
1173
  const trimmed = withoutWww.replace(TRAILING_SLASH, "");
1199
1174
  return trimmed.length > 0 ? trimmed : value;
1200
1175
  }
1176
+ var UNMEASURED = 0;
1177
+ var IS_WEB3 = Platform.OS === "web";
1178
+ function measureParentInnerWidth(node) {
1179
+ const parent = node.parentElement;
1180
+ if (parent === null) return UNMEASURED;
1181
+ const cs = window.getComputedStyle(parent);
1182
+ const padding = (parseFloat(cs.paddingLeft) || 0) + (parseFloat(cs.paddingRight) || 0);
1183
+ return Math.max(UNMEASURED, parent.clientWidth - padding);
1184
+ }
1185
+ function useAvailableWidth() {
1186
+ const [width, setWidth] = useState(UNMEASURED);
1187
+ const nodeRef = useRef(null);
1188
+ const measureRef = useCallback((node) => {
1189
+ if (!IS_WEB3) return;
1190
+ nodeRef.current = node;
1191
+ if (nodeRef.current !== null) setWidth(measureParentInnerWidth(nodeRef.current));
1192
+ }, []);
1193
+ useEffect(() => {
1194
+ if (!IS_WEB3) return void 0;
1195
+ const remeasure = () => {
1196
+ const node = nodeRef.current;
1197
+ if (node !== null) setWidth(measureParentInnerWidth(node));
1198
+ };
1199
+ window.addEventListener("resize", remeasure);
1200
+ return () => window.removeEventListener("resize", remeasure);
1201
+ }, []);
1202
+ const onLayout = useCallback((event) => {
1203
+ if (IS_WEB3) return;
1204
+ setWidth(event.nativeEvent.layout.width);
1205
+ }, []);
1206
+ return { width, measureRef, onLayout };
1207
+ }
1201
1208
  var AVG_CHAR_WIDTH_RATIO = 0.58;
1202
1209
  var DEFAULT_FONT_SIZE2 = 13;
1203
- var UNMEASURED = 0;
1210
+ var UNMEASURED2 = 0;
1204
1211
  var styles12 = StyleSheet.create({
1205
1212
  base: { fontSize: DEFAULT_FONT_SIZE2 },
1206
1213
  /** `minWidth: 0` lets the text shrink inside a flex row instead of forcing the row wider. */
1207
1214
  fill: { flexShrink: 1, minWidth: 0 }
1208
1215
  });
1209
1216
  function charBudget(width, fontSize) {
1210
- if (width <= UNMEASURED) return UNMEASURED;
1217
+ if (width <= UNMEASURED2) return UNMEASURED2;
1211
1218
  return Math.floor(width / (fontSize * AVG_CHAR_WIDTH_RATIO));
1212
1219
  }
1213
1220
  var TruncatedText = ({
@@ -1220,32 +1227,34 @@ var TruncatedText = ({
1220
1227
  accessibilityLabel,
1221
1228
  accessibilityHint
1222
1229
  }) => {
1223
- const [width, setWidth] = useState(UNMEASURED);
1230
+ const { width, measureRef, onLayout } = useAvailableWidth();
1224
1231
  const hostRef = useRef(null);
1225
- const onLayout = useCallback((e) => {
1226
- setWidth(e.nativeEvent.layout.width);
1227
- }, []);
1228
- const attachTooltip = useCallback(
1232
+ const attachRef = useCallback(
1229
1233
  (node) => {
1230
1234
  hostRef.current = node;
1235
+ measureRef(node);
1231
1236
  if (Platform.OS !== "web" || node === null) return;
1232
1237
  node.setAttribute?.("title", value);
1233
1238
  },
1234
- [value]
1239
+ [value, measureRef]
1240
+ );
1241
+ const flatStyle = useMemo(
1242
+ () => StyleSheet.flatten([styles12.base, style]),
1243
+ [style]
1235
1244
  );
1236
- const flatStyle = useMemo(() => StyleSheet.flatten([styles12.base, style]), [style]);
1237
1245
  const fontSize = flatStyle.fontSize ?? DEFAULT_FONT_SIZE2;
1238
1246
  const display = useMemo(() => {
1239
1247
  const source = isUrl ? formatUrlForDisplay(value) : value;
1240
1248
  const budget = charBudget(width, fontSize);
1241
- return budget === UNMEASURED ? source : truncateMiddle(source, budget);
1249
+ return budget === UNMEASURED2 ? source : truncateMiddle(source, budget);
1242
1250
  }, [value, isUrl, width, fontSize]);
1243
- return /* @__PURE__ */ jsx(View, { style: styles12.fill, onLayout, children: /* @__PURE__ */ jsx(
1251
+ return /* @__PURE__ */ jsx(
1244
1252
  Text,
1245
1253
  {
1246
- ref: attachTooltip,
1254
+ ref: attachRef,
1255
+ onLayout,
1247
1256
  numberOfLines: 1,
1248
- style: [styles12.base, style],
1257
+ style: [styles12.base, styles12.fill, style],
1249
1258
  testID,
1250
1259
  accessibilityRole: onPress !== void 0 || href !== void 0 ? "link" : "text",
1251
1260
  accessibilityLabel,
@@ -1254,7 +1263,7 @@ var TruncatedText = ({
1254
1263
  ...Platform.OS === "web" && href !== void 0 ? { href } : {},
1255
1264
  children: display
1256
1265
  }
1257
- ) });
1266
+ );
1258
1267
  };
1259
1268
 
1260
1269
  export { Accordion, AccordionItem, DEFAULT_BILLING_ROUTE, DropdownVariant, ELLIPSIS, Heading, LAYOUT_I18N, LAYOUT_TEST_IDS, ModalDropdown, ModalShell, Section, SegmentedControl, StatusBadge, TruncatedText, UpgradePrompt, formatUrlForDisplay, truncateMiddle, useFocusTrap };