@dloizides/ui-layout 1.5.0 → 1.7.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 +36 -0
- package/dist/index.js +132 -30
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -1
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import React8, { createContext, useMemo, useRef, useEffect, useState, useCallback, useContext } from 'react';
|
|
2
|
-
import { StyleSheet, View, Text, Pressable, TouchableOpacity,
|
|
1
|
+
import React8, { createContext, useMemo, useRef, useEffect, useState, useCallback, useContext, useLayoutEffect } from 'react';
|
|
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 { createPortal } from 'react-dom';
|
|
5
6
|
import { SvgIcon } from '@dloizides/ui-icons';
|
|
6
7
|
|
|
7
8
|
// src/Section/Section.tsx
|
|
@@ -305,6 +306,37 @@ var DropdownVariant = /* @__PURE__ */ ((DropdownVariant2) => {
|
|
|
305
306
|
DropdownVariant2["Modal"] = "modal";
|
|
306
307
|
return DropdownVariant2;
|
|
307
308
|
})(DropdownVariant || {});
|
|
309
|
+
|
|
310
|
+
// src/ModalDropdown/menuStacking.ts
|
|
311
|
+
var MENU_Z_INDEX = 1e3;
|
|
312
|
+
var ANCHOR_OPEN_Z_INDEX = 1e3;
|
|
313
|
+
var MENU_ELEVATION = 8;
|
|
314
|
+
var MENU_TOP_GAP = 4;
|
|
315
|
+
var MENU_MAX_HEIGHT = 300;
|
|
316
|
+
var BORDER_RADIUS = 8;
|
|
317
|
+
var BORDER_WIDTH2 = 1;
|
|
318
|
+
var MENU_PADDING = 4;
|
|
319
|
+
var MENU_BOX_SHADOW = "0px 2px 8px rgba(0, 0, 0, 0.15)";
|
|
320
|
+
function buildPortalPopoverStyle(rect) {
|
|
321
|
+
return {
|
|
322
|
+
// RN's ViewStyle union omits 'fixed', but react-native-web honours it at runtime.
|
|
323
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- web-only position value
|
|
324
|
+
position: "fixed",
|
|
325
|
+
top: rect.bottom + MENU_TOP_GAP,
|
|
326
|
+
left: rect.left,
|
|
327
|
+
width: rect.width,
|
|
328
|
+
zIndex: MENU_Z_INDEX,
|
|
329
|
+
borderWidth: BORDER_WIDTH2,
|
|
330
|
+
borderRadius: BORDER_RADIUS,
|
|
331
|
+
padding: MENU_PADDING,
|
|
332
|
+
maxHeight: MENU_MAX_HEIGHT,
|
|
333
|
+
boxShadow: MENU_BOX_SHADOW,
|
|
334
|
+
elevation: MENU_ELEVATION
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function buildAnchorStackStyle(isMenuOpen) {
|
|
338
|
+
return isMenuOpen ? { zIndex: ANCHOR_OPEN_Z_INDEX, elevation: MENU_ELEVATION } : null;
|
|
339
|
+
}
|
|
308
340
|
var OPTION_BORDER_RADIUS = 4;
|
|
309
341
|
var BODY_FONT_SIZE = 14;
|
|
310
342
|
var SELECTED_FONT_WEIGHT = "600";
|
|
@@ -349,6 +381,11 @@ var OptionRow = ({
|
|
|
349
381
|
}
|
|
350
382
|
);
|
|
351
383
|
};
|
|
384
|
+
function refContains(ref, target) {
|
|
385
|
+
if (ref === void 0) return false;
|
|
386
|
+
const node = ref.current;
|
|
387
|
+
return node !== null && target !== null && node.contains(target);
|
|
388
|
+
}
|
|
352
389
|
var clampIndex = (index, itemCount) => {
|
|
353
390
|
if (index < 0) return 0;
|
|
354
391
|
const lastIndex = itemCount - 1;
|
|
@@ -386,15 +423,15 @@ function buildKeyHandler(params) {
|
|
|
386
423
|
};
|
|
387
424
|
}
|
|
388
425
|
function useMenuKeyboard(params) {
|
|
389
|
-
const { containerRef, enabled, onClose } = params;
|
|
426
|
+
const { containerRef, menuRef, enabled, onClose } = params;
|
|
390
427
|
useEffect(() => {
|
|
391
428
|
if (Platform.OS !== "web" || !enabled) return;
|
|
392
429
|
const onKeyDown = buildKeyHandler(params);
|
|
393
430
|
const onMouseDown = (event) => {
|
|
394
|
-
const node = containerRef.current;
|
|
395
431
|
const target = event.target instanceof Node ? event.target : null;
|
|
396
|
-
const
|
|
397
|
-
|
|
432
|
+
const insideAnchor = refContains(containerRef, target);
|
|
433
|
+
const insideMenu = refContains(menuRef, target);
|
|
434
|
+
if (!insideAnchor && !insideMenu) onClose();
|
|
398
435
|
};
|
|
399
436
|
document.addEventListener("keydown", onKeyDown);
|
|
400
437
|
document.addEventListener("mousedown", onMouseDown);
|
|
@@ -402,17 +439,14 @@ function useMenuKeyboard(params) {
|
|
|
402
439
|
document.removeEventListener("keydown", onKeyDown);
|
|
403
440
|
document.removeEventListener("mousedown", onMouseDown);
|
|
404
441
|
};
|
|
405
|
-
}, [containerRef, enabled, onClose, params]);
|
|
442
|
+
}, [containerRef, menuRef, enabled, onClose, params]);
|
|
406
443
|
}
|
|
407
|
-
var
|
|
408
|
-
var
|
|
409
|
-
var
|
|
410
|
-
var
|
|
411
|
-
var MENU_TOP_GAP = 4;
|
|
412
|
-
var MENU_Z_INDEX = 1e3;
|
|
413
|
-
var MENU_ELEVATION = 8;
|
|
414
|
-
var MENU_BOX_SHADOW = "0px 2px 8px rgba(0, 0, 0, 0.15)";
|
|
444
|
+
var IS_WEB = Platform.OS === "web";
|
|
445
|
+
var BORDER_RADIUS2 = 8;
|
|
446
|
+
var BORDER_WIDTH3 = 1;
|
|
447
|
+
var MENU_PADDING2 = 4;
|
|
415
448
|
var styles7 = StyleSheet.create({
|
|
449
|
+
/** Native (in-tree) popover — anchored absolutely just under the trigger. */
|
|
416
450
|
popover: {
|
|
417
451
|
position: "absolute",
|
|
418
452
|
top: "100%",
|
|
@@ -420,14 +454,38 @@ var styles7 = StyleSheet.create({
|
|
|
420
454
|
right: 0,
|
|
421
455
|
marginTop: MENU_TOP_GAP,
|
|
422
456
|
zIndex: MENU_Z_INDEX,
|
|
423
|
-
borderWidth:
|
|
424
|
-
borderRadius:
|
|
425
|
-
padding:
|
|
457
|
+
borderWidth: BORDER_WIDTH3,
|
|
458
|
+
borderRadius: BORDER_RADIUS2,
|
|
459
|
+
padding: MENU_PADDING2,
|
|
426
460
|
maxHeight: MENU_MAX_HEIGHT,
|
|
427
461
|
boxShadow: MENU_BOX_SHADOW,
|
|
428
462
|
elevation: MENU_ELEVATION
|
|
429
463
|
}
|
|
430
464
|
});
|
|
465
|
+
function readAnchorRect(containerRef) {
|
|
466
|
+
if (!IS_WEB) return null;
|
|
467
|
+
const node = containerRef.current;
|
|
468
|
+
if (node === null || typeof node.getBoundingClientRect !== "function") return null;
|
|
469
|
+
const r = node.getBoundingClientRect();
|
|
470
|
+
return { top: r.top, left: r.left, width: r.width, bottom: r.bottom };
|
|
471
|
+
}
|
|
472
|
+
function useAnchorRect(containerRef) {
|
|
473
|
+
const [rect, setRect] = useState(() => readAnchorRect(containerRef));
|
|
474
|
+
useLayoutEffect(() => {
|
|
475
|
+
if (!IS_WEB) return void 0;
|
|
476
|
+
const measure = () => {
|
|
477
|
+
setRect(readAnchorRect(containerRef));
|
|
478
|
+
};
|
|
479
|
+
measure();
|
|
480
|
+
window.addEventListener("scroll", measure, true);
|
|
481
|
+
window.addEventListener("resize", measure);
|
|
482
|
+
return () => {
|
|
483
|
+
window.removeEventListener("scroll", measure, true);
|
|
484
|
+
window.removeEventListener("resize", measure);
|
|
485
|
+
};
|
|
486
|
+
}, [containerRef]);
|
|
487
|
+
return rect;
|
|
488
|
+
}
|
|
431
489
|
var InlineMenu = ({
|
|
432
490
|
testID,
|
|
433
491
|
accessibilityLabel,
|
|
@@ -439,6 +497,8 @@ var InlineMenu = ({
|
|
|
439
497
|
}) => {
|
|
440
498
|
const { theme } = useUi();
|
|
441
499
|
const { colors } = theme;
|
|
500
|
+
const menuRef = useRef(null);
|
|
501
|
+
const rect = useAnchorRect(containerRef);
|
|
442
502
|
const selectedIndex = useMemo(
|
|
443
503
|
() => Math.max(0, options.findIndex((opt) => opt.value === value)),
|
|
444
504
|
[options, value]
|
|
@@ -459,19 +519,22 @@ var InlineMenu = ({
|
|
|
459
519
|
);
|
|
460
520
|
useMenuKeyboard({
|
|
461
521
|
containerRef,
|
|
522
|
+
menuRef,
|
|
462
523
|
enabled: true,
|
|
463
524
|
itemCount: options.length,
|
|
464
525
|
onHighlightChange: setHighlightedIndex,
|
|
465
526
|
onSelectHighlighted: selectHighlighted,
|
|
466
527
|
onClose
|
|
467
528
|
});
|
|
468
|
-
const popoverStyle = useMemo(
|
|
469
|
-
|
|
470
|
-
[
|
|
471
|
-
|
|
472
|
-
|
|
529
|
+
const popoverStyle = useMemo(() => {
|
|
530
|
+
const themed = { borderColor: colors.border, backgroundColor: colors.surface };
|
|
531
|
+
if (IS_WEB) return [buildPortalPopoverStyle(rect ?? { left: 0, width: 0, bottom: 0 }), themed];
|
|
532
|
+
return [styles7.popover, themed];
|
|
533
|
+
}, [colors.border, colors.surface, rect]);
|
|
534
|
+
const menu = /* @__PURE__ */ jsx(
|
|
473
535
|
View,
|
|
474
536
|
{
|
|
537
|
+
ref: menuRef,
|
|
475
538
|
accessibilityRole: "menu",
|
|
476
539
|
"aria-label": accessibilityLabel,
|
|
477
540
|
style: popoverStyle,
|
|
@@ -489,6 +552,8 @@ var InlineMenu = ({
|
|
|
489
552
|
)) })
|
|
490
553
|
}
|
|
491
554
|
);
|
|
555
|
+
if (IS_WEB) return createPortal(menu, document.body);
|
|
556
|
+
return menu;
|
|
492
557
|
};
|
|
493
558
|
var MENU_BREAKPOINT = 768;
|
|
494
559
|
var WEB_PLATFORM = "web";
|
|
@@ -501,8 +566,8 @@ function useResolvedDropdownVariant(explicit) {
|
|
|
501
566
|
const { width } = useWindowDimensions();
|
|
502
567
|
return resolveDropdownVariant(explicit, width, Platform.OS);
|
|
503
568
|
}
|
|
504
|
-
var
|
|
505
|
-
var
|
|
569
|
+
var BORDER_RADIUS3 = 8;
|
|
570
|
+
var BORDER_WIDTH4 = 1;
|
|
506
571
|
var BODY_FONT_SIZE2 = 14;
|
|
507
572
|
var MODAL_PADDING = 8;
|
|
508
573
|
var MODAL_MIN_WIDTH = 200;
|
|
@@ -512,8 +577,8 @@ var CONTAINER_PADDING_V = 10;
|
|
|
512
577
|
var styles8 = StyleSheet.create({
|
|
513
578
|
anchor: { position: "relative" },
|
|
514
579
|
container: {
|
|
515
|
-
borderWidth:
|
|
516
|
-
borderRadius:
|
|
580
|
+
borderWidth: BORDER_WIDTH4,
|
|
581
|
+
borderRadius: BORDER_RADIUS3,
|
|
517
582
|
paddingHorizontal: CONTAINER_PADDING_H,
|
|
518
583
|
paddingVertical: CONTAINER_PADDING_V
|
|
519
584
|
},
|
|
@@ -525,7 +590,7 @@ var styles8 = StyleSheet.create({
|
|
|
525
590
|
backgroundColor: MODAL_OVERLAY_COLOR
|
|
526
591
|
},
|
|
527
592
|
modalContent: {
|
|
528
|
-
borderRadius:
|
|
593
|
+
borderRadius: BORDER_RADIUS3,
|
|
529
594
|
padding: MODAL_PADDING,
|
|
530
595
|
minWidth: MODAL_MIN_WIDTH,
|
|
531
596
|
maxHeight: MODAL_MAX_HEIGHT
|
|
@@ -569,6 +634,10 @@ var ModalDropdown = ({
|
|
|
569
634
|
() => [styles8.container, { borderColor: colors.border, backgroundColor: colors.surface }],
|
|
570
635
|
[colors.border, colors.surface]
|
|
571
636
|
);
|
|
637
|
+
const anchorStyle = useMemo(
|
|
638
|
+
() => [styles8.anchor, buildAnchorStackStyle(isMenu && isOpen)],
|
|
639
|
+
[isMenu, isOpen]
|
|
640
|
+
);
|
|
572
641
|
const modalContentStyle = useMemo(
|
|
573
642
|
() => [styles8.modalContent, { backgroundColor: colors.surface }],
|
|
574
643
|
[colors.surface]
|
|
@@ -586,7 +655,7 @@ var ModalDropdown = ({
|
|
|
586
655
|
[handleSelect, testID, value]
|
|
587
656
|
);
|
|
588
657
|
const keyExtractor = useCallback((item) => String(item.value), []);
|
|
589
|
-
return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style:
|
|
658
|
+
return /* @__PURE__ */ jsxs(View, { ref: anchorRef, style: anchorStyle, children: [
|
|
590
659
|
/* @__PURE__ */ jsx(
|
|
591
660
|
TouchableOpacity,
|
|
592
661
|
{
|
|
@@ -594,6 +663,7 @@ var ModalDropdown = ({
|
|
|
594
663
|
accessibilityHint,
|
|
595
664
|
accessibilityLabel,
|
|
596
665
|
accessibilityRole: "button",
|
|
666
|
+
accessibilityState: { expanded: isOpen },
|
|
597
667
|
"aria-expanded": isOpen,
|
|
598
668
|
style: containerStyle,
|
|
599
669
|
testID,
|
|
@@ -725,7 +795,12 @@ function enableAndroidLayoutAnimationOnce() {
|
|
|
725
795
|
setter(true);
|
|
726
796
|
}
|
|
727
797
|
}
|
|
798
|
+
function prefersReducedMotion() {
|
|
799
|
+
if (Platform.OS !== "web" || typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
|
|
800
|
+
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
801
|
+
}
|
|
728
802
|
function animateNextLayout() {
|
|
803
|
+
if (prefersReducedMotion()) return;
|
|
729
804
|
enableAndroidLayoutAnimationOnce();
|
|
730
805
|
const configure = LayoutAnimation.configureNext;
|
|
731
806
|
if (typeof configure !== "function") return;
|
|
@@ -848,6 +923,11 @@ var styles11 = StyleSheet.create({
|
|
|
848
923
|
boxBody: { paddingHorizontal: 0, paddingBottom: BOX_BODY_PADDING_BOTTOM, paddingTop: BOX_BODY_PADDING_TOP }
|
|
849
924
|
});
|
|
850
925
|
var ACTIVATION_KEYS = ["Enter", " ", "Spacebar"];
|
|
926
|
+
var INSTANT_DURATION_MS = 0;
|
|
927
|
+
function prefersReducedMotion2() {
|
|
928
|
+
if (Platform.OS !== "web" || typeof window === "undefined" || typeof window.matchMedia !== "function") return false;
|
|
929
|
+
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
930
|
+
}
|
|
851
931
|
var AccordionItem = ({
|
|
852
932
|
id,
|
|
853
933
|
title,
|
|
@@ -872,7 +952,7 @@ var AccordionItem = ({
|
|
|
872
952
|
useEffect(() => {
|
|
873
953
|
Animated.timing(rotation, {
|
|
874
954
|
toValue: expanded ? ROTATE_EXPANDED : ROTATE_COLLAPSED,
|
|
875
|
-
duration: ROTATE_DURATION_MS,
|
|
955
|
+
duration: prefersReducedMotion2() ? INSTANT_DURATION_MS : ROTATE_DURATION_MS,
|
|
876
956
|
easing: Easing.out(Easing.ease),
|
|
877
957
|
useNativeDriver: Platform.OS !== "web"
|
|
878
958
|
}).start();
|
|
@@ -927,12 +1007,34 @@ var AccordionItem = ({
|
|
|
927
1007
|
...keyboardProps,
|
|
928
1008
|
children: [
|
|
929
1009
|
/* @__PURE__ */ jsxs(View, { style: [styles11.headerLeft, boxed ? styles11.headerLeftRow : null], children: [
|
|
930
|
-
boxed ?
|
|
1010
|
+
boxed ? (
|
|
1011
|
+
// Decorative — the open/closed state is already conveyed by aria-expanded on the header.
|
|
1012
|
+
/* @__PURE__ */ jsx(
|
|
1013
|
+
Animated.View,
|
|
1014
|
+
{
|
|
1015
|
+
accessibilityElementsHidden: true,
|
|
1016
|
+
"aria-hidden": true,
|
|
1017
|
+
importantForAccessibility: "no-hide-descendants",
|
|
1018
|
+
style: [styles11.marker, { transform: [{ rotate }] }],
|
|
1019
|
+
testID: chevronTestID,
|
|
1020
|
+
children: /* @__PURE__ */ jsx(Text, { style: [styles11.markerText, { color: colors.textSecondary }], children: BOX_MARKER })
|
|
1021
|
+
}
|
|
1022
|
+
)
|
|
1023
|
+
) : null,
|
|
931
1024
|
titleNode
|
|
932
1025
|
] }),
|
|
933
1026
|
/* @__PURE__ */ jsxs(View, { style: styles11.headerRight, children: [
|
|
934
1027
|
right,
|
|
935
|
-
boxed ? null : /* @__PURE__ */ jsx(
|
|
1028
|
+
boxed ? null : /* @__PURE__ */ jsx(
|
|
1029
|
+
Animated.View,
|
|
1030
|
+
{
|
|
1031
|
+
accessibilityElementsHidden: true,
|
|
1032
|
+
"aria-hidden": true,
|
|
1033
|
+
importantForAccessibility: "no-hide-descendants",
|
|
1034
|
+
style: [styles11.chevron, { transform: [{ rotate }] }],
|
|
1035
|
+
children: /* @__PURE__ */ jsx(SvgIcon, { color: colors.textSecondary, name: "chevronDown", size: CHEVRON_SIZE, testID: chevronTestID })
|
|
1036
|
+
}
|
|
1037
|
+
)
|
|
936
1038
|
] })
|
|
937
1039
|
]
|
|
938
1040
|
}
|