@terreno/ui 57.10.1 → 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 type {SplitPageListItem, 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
@@ -51,6 +102,7 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
51
102
  narrowViewportListButtonLabel,
52
103
  narrowViewportSelectionActive,
53
104
  narrowViewportSelectionKey,
105
+ childColumnRounding = "md",
54
106
  showItemList,
55
107
  }: SplitPageProps<TItem>) => {
56
108
  const {theme} = useTheme();
@@ -61,12 +113,15 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
61
113
  const activeChildIndexRef = useRef(activeChildIndex);
62
114
  activeChildIndexRef.current = activeChildIndex;
63
115
  const swiperRef = useRef<ElementRef<typeof SwiperFlatList> | null>(null);
116
+ const narrowPagerRef = useRef<ElementRef<typeof View> | null>(null);
64
117
  const [desktopScrollWidth, setDesktopScrollWidth] = useState(0);
65
118
  const [measuredPageWidth, setMeasuredPageWidth] = useState(0);
119
+ const [measuredPageHeight, setMeasuredPageHeight] = useState(0);
66
120
 
67
121
  const isNarrowLayout =
68
122
  narrowBelowWidth === undefined ? isNarrowViewport() : windowWidth <= narrowBelowWidth;
69
123
  const isDetailActive = selectedId !== undefined || narrowViewportSelectionActive === true;
124
+ const childColumnBorderRadius = getRounding(childColumnRounding);
70
125
 
71
126
  const elementArray = Children.toArray(children).filter((c) => c !== null);
72
127
 
@@ -107,6 +162,50 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
107
162
  });
108
163
  }, [measuredPageWidth, windowWidth]);
109
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
+
110
209
  if (!children && !renderContent) {
111
210
  console.warn("A child node is required");
112
211
  return null;
@@ -203,23 +302,19 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
203
302
  width={activeTabs.length > 1 ? "100%" : "60%"}
204
303
  >
205
304
  {activeTabs.map((tabIndex, i) => {
206
- return (
207
- <ScrollView
208
- contentContainerStyle={{
209
- flex: 1,
210
- }}
211
- key={tabIndex}
212
- style={{
213
- flex: 1,
214
- height: "100%",
215
- paddingLeft: i ? 16 : 0,
216
- paddingRight: i ? 0 : 16,
217
- width: "60%",
218
- }}
219
- >
220
- {elementArray[tabIndex]}
221
- </ScrollView>
222
- );
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
+ });
223
318
  })}
224
319
  </Box>
225
320
  </View>
@@ -254,22 +349,19 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
254
349
  testID="split-page-desktop-children-scroll"
255
350
  >
256
351
  {elementArray.map((element, index) => {
257
- return (
258
- <ScrollView
259
- contentContainerStyle={{flexGrow: 1}}
260
- key={index}
261
- style={{
262
- flexGrow: 0,
263
- flexShrink: 0,
264
- height: "100%",
265
- maxWidth: childWidth,
266
- width: childWidth,
267
- }}
268
- testID={`split-page-desktop-child-${index}`}
269
- >
270
- {element}
271
- </ScrollView>
272
- );
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
+ });
273
365
  })}
274
366
  </ScrollView>
275
367
  );
@@ -278,21 +370,17 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
278
370
  return (
279
371
  <Box alignItems="center" direction="row" flex="grow" justifyContent="center" paddingX={2}>
280
372
  {elementArray.map((element, index) => {
281
- return (
282
- <ScrollView
283
- contentContainerStyle={{
284
- flex: 1,
285
- }}
286
- key={index}
287
- style={{
288
- flex: 1,
289
- height: "100%",
290
- width: "60%",
291
- }}
292
- >
293
- {element}
294
- </ScrollView>
295
- );
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
+ });
296
384
  })}
297
385
  </Box>
298
386
  );
@@ -354,19 +442,47 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
354
442
  swiperRef.current?.scrollToIndex({animated: true, index: destination});
355
443
  };
356
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
+
357
456
  const renderLabeledMobilePager = (labels: string[]) => {
358
457
  const pageWidth = measuredPageWidth > 0 ? measuredPageWidth : windowWidth;
359
458
  const lastIndex = elementArray.length - 1;
360
- 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
+ };
361
475
  return (
362
476
  <View
363
- onLayout={(event: LayoutChangeEvent) => {
364
- const nextWidth = event.nativeEvent.layout.width;
365
- setMeasuredPageWidth((current) => (current === nextWidth ? current : nextWidth));
366
- }}
367
- style={{flex: 1, width: "100%"}}
477
+ onLayout={onNarrowPagerLayout}
478
+ ref={narrowPagerRef}
479
+ style={{flex: 1, height: "100%", minHeight: 0, width: "100%"}}
368
480
  testID="split-page-mobile-children"
369
481
  >
482
+ <View
483
+ style={{flexShrink: 0, height: labeledPagerEdgeSpacing}}
484
+ testID="split-page-column-top-space"
485
+ />
370
486
  <SwiperFlatList
371
487
  getItemLayout={(_data, index) => ({
372
488
  index,
@@ -379,16 +495,23 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
379
495
  }}
380
496
  ref={swiperRef}
381
497
  renderAll
382
- style={{width: "100%"}}
498
+ style={{
499
+ flexGrow: columnHeight === undefined ? 1 : 0,
500
+ flexShrink: 1,
501
+ height: columnHeight,
502
+ minHeight: 0,
503
+ width: "100%",
504
+ }}
383
505
  >
384
506
  {elementArray.map((element, index) => {
385
507
  return (
386
508
  <View
387
509
  key={index}
388
510
  style={{
389
- height: "100%",
511
+ borderRadius: childColumnBorderRadius,
512
+ height: columnHeight ?? "100%",
513
+ overflow: "hidden",
390
514
  padding: 0,
391
- paddingBottom: bottomNavBarHeight,
392
515
  width: pageWidth,
393
516
  }}
394
517
  testID={`split-page-mobile-child-${index}`}
@@ -398,61 +521,67 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
398
521
  );
399
522
  })}
400
523
  </SwiperFlatList>
401
- {narrowViewportListButtonLabel ? (
402
- <View
403
- style={{
404
- bottom: controlBottom,
405
- left: 16,
406
- position: "absolute",
407
- zIndex: 1,
408
- }}
409
- >
410
- <IconButton
411
- accessibilityHint={narrowViewportListButtonLabel}
412
- accessibilityLabel={narrowViewportListButtonLabel}
413
- backgroundOpacity={0.88}
414
- iconName="arrow-left"
415
- onClick={() => onItemDeselect()}
416
- testID="split-page-back-to-list"
417
- variant="muted"
418
- />
419
- </View>
420
- ) : null}
421
524
  <View
422
525
  style={{
423
- bottom: controlBottom,
526
+ alignItems: "center",
424
527
  flexDirection: "row",
425
- gap: 8,
426
- position: "absolute",
427
- right: 16,
428
- zIndex: 1,
528
+ flexShrink: 0,
529
+ height: labeledColumnNavButtonHeight,
530
+ justifyContent: "space-between",
531
+ marginTop: labeledPagerEdgeSpacing,
532
+ paddingHorizontal: getSpacing(4),
429
533
  }}
534
+ testID="split-page-column-nav"
430
535
  >
431
- {activeChildIndex > 0 ? (
432
- <IconButton
433
- accessibilityLabel={`Show previous column: ${labels[activeChildIndex - 1]}`}
434
- backgroundOpacity={0.88}
435
- iconName="chevron-left"
436
- onClick={() => {
437
- showMobileChild(activeChildIndex - 1);
438
- }}
439
- testID="split-page-column-previous"
440
- variant="muted"
441
- />
442
- ) : null}
443
- {activeChildIndex < lastIndex ? (
444
- <IconButton
445
- accessibilityLabel={`Show next column: ${labels[activeChildIndex + 1]}`}
446
- backgroundOpacity={0.88}
447
- iconName="chevron-right"
448
- onClick={() => {
449
- showMobileChild(activeChildIndex + 1);
450
- }}
451
- testID="split-page-column-next"
452
- variant="muted"
453
- />
454
- ) : 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>
455
580
  </View>
581
+ <View
582
+ style={{flexShrink: 0, height: labeledPagerEdgeSpacing + navInset}}
583
+ testID="split-page-column-bottom-space"
584
+ />
456
585
  </View>
457
586
  );
458
587
  };
@@ -469,30 +598,41 @@ export const SplitPage = <TItem extends SplitPageListItem = SplitPageListItem>({
469
598
  ) {
470
599
  return renderLabeledMobilePager(narrowViewportChildLabels);
471
600
  }
601
+ const pageRatio = elementArray.length > 1 ? 0.9 : 1;
472
602
  return (
473
- <SwiperFlatList
474
- nestedScrollEnabled
475
- paginationStyle={{justifyContent: "center", width: "95%"}}
476
- renderAll
477
- showPagination
478
- 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"
479
608
  >
480
- {elementArray.map((element, i) => {
481
- return (
482
- <View
483
- key={i}
484
- style={{
485
- height: elementArray.length > 1 ? "90%" : "100%",
486
- padding: 4,
487
- paddingBottom: bottomNavBarHeight,
488
- width: windowWidth - 8,
489
- }}
490
- >
491
- {element}
492
- </View>
493
- );
494
- })}
495
- </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>
496
636
  );
497
637
  };
498
638
 
@@ -350,7 +350,7 @@ exports[`Button renders outline variant 1`] = `
350
350
  "flexDirection": "column",
351
351
  "height": undefined,
352
352
  "justifyContent": "center",
353
- "paddingHorizontal": 20,
353
+ "paddingHorizontal": 18,
354
354
  "paddingVertical": 6,
355
355
  "width": "auto",
356
356
  },