@terreno/ui 57.11.0 → 57.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/src/SplitPage.tsx CHANGED
@@ -2,6 +2,8 @@ import {
2
2
  Children,
3
3
  type ComponentProps,
4
4
  type ElementRef,
5
+ type ReactElement,
6
+ type ReactNode,
5
7
  useCallback,
6
8
  useEffect,
7
9
  useRef,
@@ -13,11 +15,12 @@ import {
13
15
  ScrollView,
14
16
  useWindowDimensions,
15
17
  View,
18
+ type ViewStyle,
16
19
  } from "react-native";
17
20
  import {SwiperFlatList} from "react-native-swiper-flatlist";
18
21
 
19
22
  import {Box} from "./Box";
20
- import {getRounding, type SplitPageListItem, type SplitPageProps} from "./Common";
23
+ import {getRounding, getSpacing, type SplitPageListItem, type SplitPageProps} from "./Common";
21
24
  import {FlatList} from "./FlatList";
22
25
  import {IconButton} from "./IconButton";
23
26
  import {isNarrowViewport} from "./MediaQuery";
@@ -25,6 +28,54 @@ import {SegmentedControl} from "./SegmentedControl";
25
28
  import {Spinner} from "./Spinner";
26
29
  import {useTheme} from "./Theme";
27
30
 
31
+ // flex: 1 (basis 0, shrink 1) sizes the content container to the pane. A height: "100%"
32
+ // child then stays pane-bounded and can scroll internally. A taller child that does not
33
+ // shrink still extends this ScrollView. flexGrow: 1 leaves the basis at the content size,
34
+ // so that same height: "100%" child grows with its content instead of the pane.
35
+ const desktopChildContentContainerStyle = {flex: 1};
36
+
37
+ // Matches IconButton's default box. The labeled pager reserves this slot even when the
38
+ // arrow is hidden, so the other arrow does not reflow.
39
+ const labeledColumnNavButtonHeight = 32;
40
+ const labeledPagerEdgeSpacing = getSpacing(2);
41
+
42
+ const renderDesktopChildColumn = ({
43
+ borderRadius,
44
+ columnKey,
45
+ element,
46
+ paneStyle,
47
+ scrollTestID,
48
+ testID,
49
+ }: {
50
+ borderRadius: number;
51
+ columnKey: number;
52
+ element: ReactNode;
53
+ paneStyle: ViewStyle;
54
+ scrollTestID: string;
55
+ testID: string;
56
+ }): ReactElement => {
57
+ return (
58
+ <View
59
+ key={columnKey}
60
+ style={{
61
+ ...paneStyle,
62
+ borderRadius,
63
+ height: "100%",
64
+ overflow: "hidden",
65
+ }}
66
+ testID={testID}
67
+ >
68
+ <ScrollView
69
+ contentContainerStyle={desktopChildContentContainerStyle}
70
+ style={{flex: 1, height: "100%"}}
71
+ testID={scrollTestID}
72
+ >
73
+ {element}
74
+ </ScrollView>
75
+ </View>
76
+ );
77
+ };
78
+
28
79
  // A component for rendering a list on one side and a details view on the right for large screens,
29
80
  // and a scrollable list where clicking an item takes you the details view.
30
81
  // On web, opt in to minimum widths for the desktop side-by-side children, or a labeled pager
@@ -62,8 +113,10 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
62
113
  const activeChildIndexRef = useRef(activeChildIndex);
63
114
  activeChildIndexRef.current = activeChildIndex;
64
115
  const swiperRef = useRef<ElementRef<typeof SwiperFlatList> | null>(null);
116
+ const narrowPagerRef = useRef<ElementRef<typeof View> | null>(null);
65
117
  const [desktopScrollWidth, setDesktopScrollWidth] = useState(0);
66
118
  const [measuredPageWidth, setMeasuredPageWidth] = useState(0);
119
+ const [measuredPageHeight, setMeasuredPageHeight] = useState(0);
67
120
 
68
121
  const isNarrowLayout =
69
122
  narrowBelowWidth === undefined ? isNarrowViewport() : windowWidth <= narrowBelowWidth;
@@ -109,6 +162,50 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
109
162
  });
110
163
  }, [measuredPageWidth, windowWidth]);
111
164
 
165
+ const rememberNarrowPagerSize = useCallback((width: number, height: number): void => {
166
+ if (width > 0) {
167
+ setMeasuredPageWidth((current) => (current === width ? current : width));
168
+ }
169
+ if (height > 0) {
170
+ setMeasuredPageHeight((current) => (current === height ? current : height));
171
+ }
172
+ }, []);
173
+
174
+ const onNarrowPagerLayout = useCallback(
175
+ (event: LayoutChangeEvent): void => {
176
+ const {height, width} = event.nativeEvent.layout;
177
+ rememberNarrowPagerSize(width, height);
178
+ },
179
+ [rememberNarrowPagerSize]
180
+ );
181
+
182
+ // The pager frame is already the visible height. Reading it here covers the case where
183
+ // the view's onLayout callback does not run after the frame settles.
184
+ useEffect(() => {
185
+ if (!isNarrowLayout || !isDetailActive) {
186
+ return;
187
+ }
188
+ const node = narrowPagerRef.current as unknown as HTMLElement | null;
189
+ if (
190
+ node == null ||
191
+ typeof node.offsetHeight !== "number" ||
192
+ typeof ResizeObserver === "undefined"
193
+ ) {
194
+ return;
195
+ }
196
+ const measure = (): void => {
197
+ rememberNarrowPagerSize(node.offsetWidth, node.offsetHeight);
198
+ };
199
+ measure();
200
+ const observer = new ResizeObserver(() => {
201
+ measure();
202
+ });
203
+ observer.observe(node);
204
+ return () => {
205
+ observer.disconnect();
206
+ };
207
+ }, [rememberNarrowPagerSize, isNarrowLayout, isDetailActive]);
208
+
112
209
  if (!children && !renderContent) {
113
210
  console.warn("A child node is required");
114
211
  return null;
@@ -205,25 +302,19 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
205
302
  width={activeTabs.length > 1 ? "100%" : "60%"}
206
303
  >
207
304
  {activeTabs.map((tabIndex, i) => {
208
- return (
209
- <ScrollView
210
- contentContainerStyle={{
211
- flex: 1,
212
- }}
213
- key={tabIndex}
214
- style={{
215
- borderRadius: childColumnBorderRadius,
216
- flex: 1,
217
- height: "100%",
218
- overflow: "hidden",
219
- paddingLeft: i ? 16 : 0,
220
- paddingRight: i ? 0 : 16,
221
- width: "60%",
222
- }}
223
- >
224
- {elementArray[tabIndex]}
225
- </ScrollView>
226
- );
305
+ return renderDesktopChildColumn({
306
+ borderRadius: childColumnBorderRadius,
307
+ columnKey: tabIndex,
308
+ element: elementArray[tabIndex],
309
+ paneStyle: {
310
+ flex: 1,
311
+ paddingLeft: i ? 16 : 0,
312
+ paddingRight: i ? 0 : 16,
313
+ width: "60%",
314
+ },
315
+ scrollTestID: `split-page-desktop-child-scroll-${i}`,
316
+ testID: `split-page-desktop-segment-child-${i}`,
317
+ });
227
318
  })}
228
319
  </Box>
229
320
  </View>
@@ -258,24 +349,19 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
258
349
  testID="split-page-desktop-children-scroll"
259
350
  >
260
351
  {elementArray.map((element, index) => {
261
- return (
262
- <ScrollView
263
- contentContainerStyle={{flexGrow: 1}}
264
- key={index}
265
- style={{
266
- borderRadius: childColumnBorderRadius,
267
- flexGrow: 0,
268
- flexShrink: 0,
269
- height: "100%",
270
- maxWidth: childWidth,
271
- overflow: "hidden",
272
- width: childWidth,
273
- }}
274
- testID={`split-page-desktop-child-${index}`}
275
- >
276
- {element}
277
- </ScrollView>
278
- );
352
+ return renderDesktopChildColumn({
353
+ borderRadius: childColumnBorderRadius,
354
+ columnKey: index,
355
+ element,
356
+ paneStyle: {
357
+ flexGrow: 0,
358
+ flexShrink: 0,
359
+ maxWidth: childWidth,
360
+ width: childWidth,
361
+ },
362
+ scrollTestID: `split-page-desktop-child-scroll-${index}`,
363
+ testID: `split-page-desktop-child-${index}`,
364
+ });
279
365
  })}
280
366
  </ScrollView>
281
367
  );
@@ -284,23 +370,17 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
284
370
  return (
285
371
  <Box alignItems="center" direction="row" flex="grow" justifyContent="center" paddingX={2}>
286
372
  {elementArray.map((element, index) => {
287
- return (
288
- <ScrollView
289
- contentContainerStyle={{
290
- flex: 1,
291
- }}
292
- key={index}
293
- style={{
294
- borderRadius: childColumnBorderRadius,
295
- flex: 1,
296
- height: "100%",
297
- overflow: "hidden",
298
- width: "60%",
299
- }}
300
- >
301
- {element}
302
- </ScrollView>
303
- );
373
+ return renderDesktopChildColumn({
374
+ borderRadius: childColumnBorderRadius,
375
+ columnKey: index,
376
+ element,
377
+ paneStyle: {
378
+ flex: 1,
379
+ width: "60%",
380
+ },
381
+ scrollTestID: `split-page-desktop-child-scroll-${index}`,
382
+ testID: `split-page-desktop-row-child-${index}`,
383
+ });
304
384
  })}
305
385
  </Box>
306
386
  );
@@ -362,19 +442,47 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
362
442
  swiperRef.current?.scrollToIndex({animated: true, index: destination});
363
443
  };
364
444
 
445
+ // The horizontal pager's content container sizes to its children, so height: "100%" on a page
446
+ // grows with the child instead of the visible pane. overflowY: hidden on that pager then clips
447
+ // the extra height. A measured pixel height gives the page a definite box: rounded corners still
448
+ // clip, and a height: "100%" child can scroll inside that box.
449
+ const narrowPageHeight = (ratio: number): number | "100%" | "90%" => {
450
+ if (measuredPageHeight > 0) {
451
+ return measuredPageHeight * ratio;
452
+ }
453
+ return ratio === 1 ? "100%" : "90%";
454
+ };
455
+
365
456
  const renderLabeledMobilePager = (labels: string[]) => {
366
457
  const pageWidth = measuredPageWidth > 0 ? measuredPageWidth : windowWidth;
367
458
  const lastIndex = elementArray.length - 1;
368
- const controlBottom = (bottomNavBarHeight ?? 0) + 8;
459
+ const navInset = bottomNavBarHeight ?? 0;
460
+ const labeledChromeHeight =
461
+ labeledPagerEdgeSpacing +
462
+ labeledPagerEdgeSpacing +
463
+ labeledColumnNavButtonHeight +
464
+ labeledPagerEdgeSpacing +
465
+ navInset;
466
+ const columnHeight =
467
+ measuredPageHeight > 0 ? Math.max(measuredPageHeight - labeledChromeHeight, 0) : undefined;
468
+ const navSlotStyle: ViewStyle = {
469
+ alignItems: "center",
470
+ flexShrink: 0,
471
+ height: labeledColumnNavButtonHeight,
472
+ justifyContent: "center",
473
+ width: labeledColumnNavButtonHeight,
474
+ };
369
475
  return (
370
476
  <View
371
- onLayout={(event: LayoutChangeEvent) => {
372
- const nextWidth = event.nativeEvent.layout.width;
373
- setMeasuredPageWidth((current) => (current === nextWidth ? current : nextWidth));
374
- }}
375
- style={{flex: 1, width: "100%"}}
477
+ onLayout={onNarrowPagerLayout}
478
+ ref={narrowPagerRef}
479
+ style={{flex: 1, height: "100%", minHeight: 0, width: "100%"}}
376
480
  testID="split-page-mobile-children"
377
481
  >
482
+ <View
483
+ style={{flexShrink: 0, height: labeledPagerEdgeSpacing}}
484
+ testID="split-page-column-top-space"
485
+ />
378
486
  <SwiperFlatList
379
487
  getItemLayout={(_data, index) => ({
380
488
  index,
@@ -387,7 +495,13 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
387
495
  }}
388
496
  ref={swiperRef}
389
497
  renderAll
390
- style={{width: "100%"}}
498
+ style={{
499
+ flexGrow: columnHeight === undefined ? 1 : 0,
500
+ flexShrink: 1,
501
+ height: columnHeight,
502
+ minHeight: 0,
503
+ width: "100%",
504
+ }}
391
505
  >
392
506
  {elementArray.map((element, index) => {
393
507
  return (
@@ -395,10 +509,9 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
395
509
  key={index}
396
510
  style={{
397
511
  borderRadius: childColumnBorderRadius,
398
- height: "100%",
512
+ height: columnHeight ?? "100%",
399
513
  overflow: "hidden",
400
514
  padding: 0,
401
- paddingBottom: bottomNavBarHeight,
402
515
  width: pageWidth,
403
516
  }}
404
517
  testID={`split-page-mobile-child-${index}`}
@@ -408,55 +521,67 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
408
521
  );
409
522
  })}
410
523
  </SwiperFlatList>
411
- {narrowViewportListButtonLabel ? (
412
- <View
413
- style={{
414
- bottom: controlBottom,
415
- left: 16,
416
- position: "absolute",
417
- zIndex: 1,
418
- }}
419
- >
420
- <IconButton
421
- accessibilityHint={narrowViewportListButtonLabel}
422
- accessibilityLabel={narrowViewportListButtonLabel}
423
- iconName="arrow-left"
424
- onClick={() => onItemDeselect()}
425
- testID="split-page-back-to-list"
426
- />
427
- </View>
428
- ) : null}
429
524
  <View
430
525
  style={{
431
- bottom: controlBottom,
526
+ alignItems: "center",
432
527
  flexDirection: "row",
433
- gap: 8,
434
- position: "absolute",
435
- right: 16,
436
- zIndex: 1,
528
+ flexShrink: 0,
529
+ height: labeledColumnNavButtonHeight,
530
+ justifyContent: "space-between",
531
+ marginTop: labeledPagerEdgeSpacing,
532
+ paddingHorizontal: getSpacing(4),
437
533
  }}
534
+ testID="split-page-column-nav"
438
535
  >
439
- {activeChildIndex > 0 ? (
440
- <IconButton
441
- accessibilityLabel={`Show previous column: ${labels[activeChildIndex - 1]}`}
442
- iconName="chevron-left"
443
- onClick={() => {
444
- showMobileChild(activeChildIndex - 1);
445
- }}
446
- testID="split-page-column-previous"
447
- />
448
- ) : null}
449
- {activeChildIndex < lastIndex ? (
450
- <IconButton
451
- accessibilityLabel={`Show next column: ${labels[activeChildIndex + 1]}`}
452
- iconName="chevron-right"
453
- onClick={() => {
454
- showMobileChild(activeChildIndex + 1);
455
- }}
456
- testID="split-page-column-next"
457
- />
458
- ) : null}
536
+ <View
537
+ style={{
538
+ alignItems: "center",
539
+ flexDirection: "row",
540
+ flexShrink: 0,
541
+ gap: labeledPagerEdgeSpacing,
542
+ }}
543
+ >
544
+ {narrowViewportListButtonLabel ? (
545
+ <View style={navSlotStyle} testID="split-page-column-back-slot">
546
+ <IconButton
547
+ accessibilityHint={narrowViewportListButtonLabel}
548
+ accessibilityLabel={narrowViewportListButtonLabel}
549
+ iconName="arrow-left"
550
+ onClick={() => onItemDeselect()}
551
+ testID="split-page-back-to-list"
552
+ />
553
+ </View>
554
+ ) : null}
555
+ <View accessible={false} style={navSlotStyle} testID="split-page-column-previous-slot">
556
+ {activeChildIndex > 0 ? (
557
+ <IconButton
558
+ accessibilityLabel={`Show previous column: ${labels[activeChildIndex - 1]}`}
559
+ iconName="chevron-left"
560
+ onClick={() => {
561
+ showMobileChild(activeChildIndex - 1);
562
+ }}
563
+ testID="split-page-column-previous"
564
+ />
565
+ ) : null}
566
+ </View>
567
+ </View>
568
+ <View accessible={false} style={navSlotStyle} testID="split-page-column-next-slot">
569
+ {activeChildIndex < lastIndex ? (
570
+ <IconButton
571
+ accessibilityLabel={`Show next column: ${labels[activeChildIndex + 1]}`}
572
+ iconName="chevron-right"
573
+ onClick={() => {
574
+ showMobileChild(activeChildIndex + 1);
575
+ }}
576
+ testID="split-page-column-next"
577
+ />
578
+ ) : null}
579
+ </View>
459
580
  </View>
581
+ <View
582
+ style={{flexShrink: 0, height: labeledPagerEdgeSpacing + navInset}}
583
+ testID="split-page-column-bottom-space"
584
+ />
460
585
  </View>
461
586
  );
462
587
  };
@@ -473,32 +598,41 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
473
598
  ) {
474
599
  return renderLabeledMobilePager(narrowViewportChildLabels);
475
600
  }
601
+ const pageRatio = elementArray.length > 1 ? 0.9 : 1;
476
602
  return (
477
- <SwiperFlatList
478
- nestedScrollEnabled
479
- paginationStyle={{justifyContent: "center", width: "95%"}}
480
- renderAll
481
- showPagination
482
- style={{width: "100%"}}
603
+ <View
604
+ onLayout={onNarrowPagerLayout}
605
+ ref={narrowPagerRef}
606
+ style={{flex: 1, height: "100%", minHeight: 0, width: "100%"}}
607
+ testID="split-page-dotted-pager"
483
608
  >
484
- {elementArray.map((element, i) => {
485
- return (
486
- <View
487
- key={i}
488
- style={{
489
- borderRadius: childColumnBorderRadius,
490
- height: elementArray.length > 1 ? "90%" : "100%",
491
- overflow: "hidden",
492
- padding: 4,
493
- paddingBottom: bottomNavBarHeight,
494
- width: windowWidth - 8,
495
- }}
496
- >
497
- {element}
498
- </View>
499
- );
500
- })}
501
- </SwiperFlatList>
609
+ <SwiperFlatList
610
+ nestedScrollEnabled
611
+ paginationStyle={{justifyContent: "center", width: "95%"}}
612
+ renderAll
613
+ showPagination
614
+ style={{height: "100%", width: "100%"}}
615
+ >
616
+ {elementArray.map((element, i) => {
617
+ return (
618
+ <View
619
+ key={i}
620
+ style={{
621
+ borderRadius: childColumnBorderRadius,
622
+ height: narrowPageHeight(pageRatio),
623
+ overflow: "hidden",
624
+ padding: 4,
625
+ paddingBottom: bottomNavBarHeight,
626
+ width: windowWidth - 8,
627
+ }}
628
+ testID={`split-page-dotted-child-${i}`}
629
+ >
630
+ {element}
631
+ </View>
632
+ );
633
+ })}
634
+ </SwiperFlatList>
635
+ </View>
502
636
  );
503
637
  };
504
638