react-modal-sheet 3.5.0 → 4.0.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/README.md +37 -17
- package/dist/index.d.mts +5 -3
- package/dist/index.d.ts +5 -3
- package/dist/index.js +68 -94
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +50 -76
- package/dist/index.mjs.map +1 -1
- package/package.json +38 -49
- package/src/SheetBackdrop.tsx +3 -5
- package/src/SheetContainer.tsx +3 -5
- package/src/SheetContent.tsx +3 -5
- package/src/SheetHeader.tsx +3 -5
- package/src/SheetScroller.tsx +2 -4
- package/src/context.tsx +8 -3
- package/src/hooks.tsx +10 -11
- package/src/index.tsx +24 -16
- package/src/sheet.tsx +12 -12
- package/src/styles.ts +2 -3
- package/src/types.tsx +1 -1
- package/src/use-prevent-scroll.ts +0 -1
- package/src/utils.ts +2 -6
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import React2, { createContext, forwardRef, useRef, useEffect, useImperativeHandle, useMemo, Children, cloneElement, useLayoutEffect, useCallback, useState, useContext } from 'react';
|
|
2
2
|
import { createPortal } from 'react-dom';
|
|
3
|
-
import { useMotionValue, useReducedMotion, useTransform, animate, motion, AnimatePresence, transform } from '
|
|
3
|
+
import { useMotionValue, useReducedMotion, useTransform, animate, motion, AnimatePresence, transform } from 'motion/react';
|
|
4
4
|
|
|
5
5
|
// src/sheet.tsx
|
|
6
6
|
|
|
@@ -29,8 +29,7 @@ function useModalEffect({
|
|
|
29
29
|
function setup() {
|
|
30
30
|
const root = document.querySelector(`#${rootId}`);
|
|
31
31
|
const body = document.querySelector("body");
|
|
32
|
-
if (!root)
|
|
33
|
-
return;
|
|
32
|
+
if (!root) return;
|
|
34
33
|
body.style.backgroundColor = "#000";
|
|
35
34
|
root.style.overflow = "hidden";
|
|
36
35
|
root.style.transitionTimingFunction = "cubic-bezier(0.32, 0.72, 0, 1)";
|
|
@@ -41,8 +40,7 @@ function useModalEffect({
|
|
|
41
40
|
function cleanup() {
|
|
42
41
|
const root = document.querySelector(`#${rootId}`);
|
|
43
42
|
const body = document.querySelector("body");
|
|
44
|
-
if (!root)
|
|
45
|
-
return;
|
|
43
|
+
if (!root) return;
|
|
46
44
|
setTimeout(() => {
|
|
47
45
|
body.style.removeProperty("background-color");
|
|
48
46
|
root.style.removeProperty("overflow");
|
|
@@ -57,17 +55,14 @@ function useModalEffect({
|
|
|
57
55
|
}
|
|
58
56
|
useIsomorphicLayoutEffect(() => {
|
|
59
57
|
return () => {
|
|
60
|
-
if (rootId)
|
|
61
|
-
cleanup();
|
|
58
|
+
if (rootId) cleanup();
|
|
62
59
|
};
|
|
63
60
|
}, []);
|
|
64
61
|
useEffect(() => {
|
|
65
62
|
const root = document.querySelector(`#${rootId}`);
|
|
66
|
-
if (!root)
|
|
67
|
-
return;
|
|
63
|
+
if (!root) return;
|
|
68
64
|
function onCompleted() {
|
|
69
|
-
if (y.get() - 10 >= heightRef.current)
|
|
70
|
-
cleanup();
|
|
65
|
+
if (y.get() - 10 >= heightRef.current) cleanup();
|
|
71
66
|
}
|
|
72
67
|
const removeStartListener = y.on("animationStart", () => {
|
|
73
68
|
var _a;
|
|
@@ -142,14 +137,14 @@ function useDimensions() {
|
|
|
142
137
|
return dimensions;
|
|
143
138
|
}
|
|
144
139
|
function usePrevious(state) {
|
|
145
|
-
const ref = useRef();
|
|
140
|
+
const ref = useRef(void 0);
|
|
146
141
|
useEffect(() => {
|
|
147
142
|
ref.current = state;
|
|
148
143
|
});
|
|
149
144
|
return ref.current;
|
|
150
145
|
}
|
|
151
146
|
function useEffectEvent(handler) {
|
|
152
|
-
const handlerRef = useRef();
|
|
147
|
+
const handlerRef = useRef(void 0);
|
|
153
148
|
useIsomorphicLayoutEffect(() => {
|
|
154
149
|
handlerRef.current = handler;
|
|
155
150
|
});
|
|
@@ -169,8 +164,7 @@ var SheetContext = createContext(
|
|
|
169
164
|
);
|
|
170
165
|
var useSheetContext = () => {
|
|
171
166
|
const context = useContext(SheetContext);
|
|
172
|
-
if (!context)
|
|
173
|
-
throw Error("Sheet context error");
|
|
167
|
+
if (!context) throw new Error("Sheet context error");
|
|
174
168
|
return context;
|
|
175
169
|
};
|
|
176
170
|
var SheetScrollerContext = createContext(void 0);
|
|
@@ -180,12 +174,10 @@ function SheetScrollerContextProvider({
|
|
|
180
174
|
const sheetContext = useSheetContext();
|
|
181
175
|
const [disableDrag, setDisableDrag] = useState(!!sheetContext.disableDrag);
|
|
182
176
|
function setDragEnabled() {
|
|
183
|
-
if (!sheetContext.disableDrag)
|
|
184
|
-
setDisableDrag(false);
|
|
177
|
+
if (!sheetContext.disableDrag) setDisableDrag(false);
|
|
185
178
|
}
|
|
186
179
|
function setDragDisabled() {
|
|
187
|
-
if (!disableDrag)
|
|
188
|
-
setDisableDrag(true);
|
|
180
|
+
if (!disableDrag) setDisableDrag(true);
|
|
189
181
|
}
|
|
190
182
|
return /* @__PURE__ */ React2.createElement(
|
|
191
183
|
SheetScrollerContext.Provider,
|
|
@@ -197,8 +189,7 @@ function SheetScrollerContextProvider({
|
|
|
197
189
|
}
|
|
198
190
|
var useSheetScrollerContext = () => {
|
|
199
191
|
const context = useContext(SheetScrollerContext);
|
|
200
|
-
if (!context)
|
|
201
|
-
throw Error("Sheet scroller context error");
|
|
192
|
+
if (!context) throw new Error("Sheet scroller context error");
|
|
202
193
|
return context;
|
|
203
194
|
};
|
|
204
195
|
|
|
@@ -217,8 +208,7 @@ function getClosest(nums, goal) {
|
|
|
217
208
|
}
|
|
218
209
|
function inDescendingOrder(arr) {
|
|
219
210
|
for (let i = 0; i < arr.length; i++) {
|
|
220
|
-
if (arr[i + 1] > arr[i])
|
|
221
|
-
return false;
|
|
211
|
+
if (arr[i + 1] > arr[i]) return false;
|
|
222
212
|
}
|
|
223
213
|
return true;
|
|
224
214
|
}
|
|
@@ -245,16 +235,14 @@ function mergeRefs(refs) {
|
|
|
245
235
|
};
|
|
246
236
|
}
|
|
247
237
|
function isTouchDevice() {
|
|
248
|
-
if (typeof window === "undefined")
|
|
249
|
-
return false;
|
|
238
|
+
if (typeof window === "undefined") return false;
|
|
250
239
|
return "ontouchstart" in window || navigator.maxTouchPoints > 0;
|
|
251
240
|
}
|
|
252
241
|
function testPlatform(re) {
|
|
253
242
|
var _a;
|
|
254
243
|
return typeof window !== "undefined" && window.navigator != null ? re.test(
|
|
255
244
|
// @ts-expect-error
|
|
256
|
-
|
|
257
|
-
((_a = window.navigator["userAgentData"]) == null ? void 0 : _a.platform) || window.navigator.platform
|
|
245
|
+
((_a = window.navigator.userAgentData) == null ? void 0 : _a.platform) || window.navigator.platform
|
|
258
246
|
) : false;
|
|
259
247
|
}
|
|
260
248
|
function cached(fn) {
|
|
@@ -517,7 +505,8 @@ var styles = {
|
|
|
517
505
|
backgroundColor: "rgba(0, 0, 0, 0.2)",
|
|
518
506
|
touchAction: "none",
|
|
519
507
|
// Disable iOS body scrolling
|
|
520
|
-
border: "none"
|
|
508
|
+
border: "none",
|
|
509
|
+
WebkitTapHighlightColor: "transparent"
|
|
521
510
|
},
|
|
522
511
|
container: {
|
|
523
512
|
zIndex: 2,
|
|
@@ -562,7 +551,6 @@ var styles = {
|
|
|
562
551
|
overflowY: "auto"
|
|
563
552
|
}
|
|
564
553
|
};
|
|
565
|
-
var styles_default = styles;
|
|
566
554
|
|
|
567
555
|
// src/sheet.tsx
|
|
568
556
|
var Sheet = forwardRef(
|
|
@@ -601,7 +589,7 @@ var Sheet = forwardRef(
|
|
|
601
589
|
const y = useMotionValue(windowHeight);
|
|
602
590
|
const zIndex = useTransform(
|
|
603
591
|
y,
|
|
604
|
-
(value) => value + 2 >= windowHeight ? -1 :
|
|
592
|
+
(value) => value + 2 >= windowHeight ? -1 : (style == null ? void 0 : style.zIndex) ?? 9999
|
|
605
593
|
);
|
|
606
594
|
const visibility = useTransform(
|
|
607
595
|
y,
|
|
@@ -624,8 +612,7 @@ var Sheet = forwardRef(
|
|
|
624
612
|
let snapPoints = snapPointsProp;
|
|
625
613
|
if (snapPoints) {
|
|
626
614
|
snapPoints = snapPoints.map((point) => {
|
|
627
|
-
if (point > 0 && point <= 1)
|
|
628
|
-
return Math.round(point * windowHeight);
|
|
615
|
+
if (point > 0 && point <= 1) return Math.round(point * windowHeight);
|
|
629
616
|
return point < 0 ? windowHeight + point : point;
|
|
630
617
|
});
|
|
631
618
|
console.assert(
|
|
@@ -637,16 +624,13 @@ var Sheet = forwardRef(
|
|
|
637
624
|
}
|
|
638
625
|
const onDrag = useEffectEvent((_, { delta }) => {
|
|
639
626
|
const velocity = y.getVelocity();
|
|
640
|
-
if (velocity > 0)
|
|
641
|
-
|
|
642
|
-
if (velocity < 0)
|
|
643
|
-
indicatorRotation.set(-10);
|
|
627
|
+
if (velocity > 0) indicatorRotation.set(10);
|
|
628
|
+
if (velocity < 0) indicatorRotation.set(-10);
|
|
644
629
|
y.set(Math.max(y.get() + delta.y, 0));
|
|
645
630
|
});
|
|
646
631
|
const onDragStart = useEffectEvent(() => {
|
|
647
632
|
const focusedElement = document.activeElement;
|
|
648
|
-
if (!focusedElement || !sheetRef.current)
|
|
649
|
-
return;
|
|
633
|
+
if (!focusedElement || !sheetRef.current) return;
|
|
650
634
|
const isInput = focusedElement.tagName === "INPUT" || focusedElement.tagName === "TEXTAREA";
|
|
651
635
|
if (isInput && sheetRef.current.contains(focusedElement)) {
|
|
652
636
|
focusedElement.blur();
|
|
@@ -671,22 +655,22 @@ var Sheet = forwardRef(
|
|
|
671
655
|
snapTo = sheetHeight;
|
|
672
656
|
}
|
|
673
657
|
snapTo = validateSnapTo({ snapTo, sheetHeight });
|
|
674
|
-
|
|
658
|
+
animate(y, snapTo, animationOptions);
|
|
675
659
|
if (snapPoints && onSnap) {
|
|
676
660
|
const snapValue = Math.abs(Math.round(snapPoints[0] - snapTo));
|
|
677
|
-
const snapIndex = snapPoints.indexOf(
|
|
661
|
+
const snapIndex = snapPoints.indexOf(
|
|
662
|
+
getClosest(snapPoints, snapValue)
|
|
663
|
+
);
|
|
678
664
|
onSnap(snapIndex);
|
|
679
665
|
}
|
|
680
666
|
const roundedSheetHeight = Math.round(sheetHeight);
|
|
681
667
|
const shouldClose = snapTo + 2 >= roundedSheetHeight;
|
|
682
|
-
if (shouldClose)
|
|
683
|
-
onClose();
|
|
668
|
+
if (shouldClose) onClose();
|
|
684
669
|
}
|
|
685
670
|
indicatorRotation.set(0);
|
|
686
671
|
});
|
|
687
672
|
useEffect(() => {
|
|
688
|
-
if (!snapPoints || !onSnap)
|
|
689
|
-
return;
|
|
673
|
+
if (!snapPoints || !onSnap) return;
|
|
690
674
|
const snapIndex = isOpen ? initialSnap : snapPoints.length - 1;
|
|
691
675
|
onSnap(snapIndex);
|
|
692
676
|
}, [isOpen]);
|
|
@@ -701,11 +685,9 @@ var Sheet = forwardRef(
|
|
|
701
685
|
snapTo: sheetHeight - snapPoint,
|
|
702
686
|
sheetHeight
|
|
703
687
|
});
|
|
704
|
-
|
|
705
|
-
if (onSnap)
|
|
706
|
-
|
|
707
|
-
if (snapTo >= sheetHeight)
|
|
708
|
-
onClose();
|
|
688
|
+
animate(y, snapTo, animationOptions);
|
|
689
|
+
if (onSnap) onSnap(snapIndex);
|
|
690
|
+
if (snapTo >= sheetHeight) onClose();
|
|
709
691
|
}
|
|
710
692
|
}
|
|
711
693
|
}));
|
|
@@ -747,20 +729,18 @@ var Sheet = forwardRef(
|
|
|
747
729
|
{
|
|
748
730
|
...rest,
|
|
749
731
|
ref,
|
|
750
|
-
style: { ...
|
|
732
|
+
style: { ...styles.wrapper, zIndex, visibility, ...style }
|
|
751
733
|
},
|
|
752
734
|
/* @__PURE__ */ React2.createElement(AnimatePresence, null, isOpen ? /* @__PURE__ */ React2.createElement(SheetScrollerContextProvider, null, Children.map(
|
|
753
735
|
children,
|
|
754
736
|
(child, i) => cloneElement(child, { key: `sheet-child-${i}` })
|
|
755
737
|
)) : null)
|
|
756
738
|
));
|
|
757
|
-
if (IS_SSR)
|
|
758
|
-
return sheet;
|
|
739
|
+
if (IS_SSR) return sheet;
|
|
759
740
|
return createPortal(sheet, mountPoint ?? document.body);
|
|
760
741
|
}
|
|
761
742
|
);
|
|
762
743
|
Sheet.displayName = "Sheet";
|
|
763
|
-
var sheet_default = Sheet;
|
|
764
744
|
var SheetContainer = forwardRef(
|
|
765
745
|
({ children, style = {}, className = "", ...rest }, ref) => {
|
|
766
746
|
const {
|
|
@@ -786,7 +766,7 @@ var SheetContainer = forwardRef(
|
|
|
786
766
|
ref: mergeRefs([sheetRef, ref]),
|
|
787
767
|
className: `react-modal-sheet-container ${className}`,
|
|
788
768
|
style: {
|
|
789
|
-
...
|
|
769
|
+
...styles.container,
|
|
790
770
|
...style,
|
|
791
771
|
...detent === "full-height" && { height },
|
|
792
772
|
...detent === "content-height" && { maxHeight: height },
|
|
@@ -802,7 +782,6 @@ var SheetContainer = forwardRef(
|
|
|
802
782
|
}
|
|
803
783
|
);
|
|
804
784
|
SheetContainer.displayName = "SheetContainer";
|
|
805
|
-
var SheetContainer_default = SheetContainer;
|
|
806
785
|
var SheetContent = forwardRef(
|
|
807
786
|
({ children, style, disableDrag, className = "", ...rest }, ref) => {
|
|
808
787
|
const sheetContext = useSheetContext();
|
|
@@ -815,7 +794,7 @@ var SheetContent = forwardRef(
|
|
|
815
794
|
...rest,
|
|
816
795
|
ref: mergeRefs([ref, constraintsRef]),
|
|
817
796
|
className: `react-modal-sheet-content ${className}`,
|
|
818
|
-
style: { ...
|
|
797
|
+
style: { ...styles.content, ...style },
|
|
819
798
|
...dragProps,
|
|
820
799
|
dragConstraints: constraintsRef,
|
|
821
800
|
onMeasureDragConstraints
|
|
@@ -825,7 +804,6 @@ var SheetContent = forwardRef(
|
|
|
825
804
|
}
|
|
826
805
|
);
|
|
827
806
|
SheetContent.displayName = "SheetContent";
|
|
828
|
-
var SheetContent_default = SheetContent;
|
|
829
807
|
var SheetHeader = forwardRef(
|
|
830
808
|
({ children, style, disableDrag, ...rest }, ref) => {
|
|
831
809
|
const { indicatorRotation, dragProps } = useSheetContext();
|
|
@@ -844,29 +822,28 @@ var SheetHeader = forwardRef(
|
|
|
844
822
|
{
|
|
845
823
|
...rest,
|
|
846
824
|
ref: mergeRefs([ref, constraintsRef]),
|
|
847
|
-
style: { ...
|
|
825
|
+
style: { ...styles.headerWrapper, ...style },
|
|
848
826
|
..._dragProps,
|
|
849
827
|
dragConstraints: constraintsRef,
|
|
850
828
|
onMeasureDragConstraints
|
|
851
829
|
},
|
|
852
|
-
children || /* @__PURE__ */ React2.createElement("div", { className: "react-modal-sheet-header", style:
|
|
830
|
+
children || /* @__PURE__ */ React2.createElement("div", { className: "react-modal-sheet-header", style: styles.header }, /* @__PURE__ */ React2.createElement(
|
|
853
831
|
motion.span,
|
|
854
832
|
{
|
|
855
833
|
className: "react-modal-sheet-drag-indicator",
|
|
856
|
-
style: { ...
|
|
834
|
+
style: { ...styles.indicator, transform: indicator1Transform }
|
|
857
835
|
}
|
|
858
836
|
), /* @__PURE__ */ React2.createElement(
|
|
859
837
|
motion.span,
|
|
860
838
|
{
|
|
861
839
|
className: "react-modal-sheet-drag-indicator",
|
|
862
|
-
style: { ...
|
|
840
|
+
style: { ...styles.indicator, transform: indicator2Transform }
|
|
863
841
|
}
|
|
864
842
|
))
|
|
865
843
|
);
|
|
866
844
|
}
|
|
867
845
|
);
|
|
868
846
|
SheetHeader.displayName = "SheetHeader";
|
|
869
|
-
var SheetHeader_default = SheetHeader;
|
|
870
847
|
var isClickable = (props) => !!props.onClick || !!props.onTap;
|
|
871
848
|
var SheetBackdrop = forwardRef(
|
|
872
849
|
({ style = {}, className = "", ...rest }, ref) => {
|
|
@@ -878,7 +855,7 @@ var SheetBackdrop = forwardRef(
|
|
|
878
855
|
...rest,
|
|
879
856
|
ref,
|
|
880
857
|
className: `react-modal-sheet-backdrop ${className}`,
|
|
881
|
-
style: { ...
|
|
858
|
+
style: { ...styles.backdrop, ...style, pointerEvents },
|
|
882
859
|
initial: { opacity: 0 },
|
|
883
860
|
animate: { opacity: 1 },
|
|
884
861
|
exit: { opacity: 0 }
|
|
@@ -887,15 +864,13 @@ var SheetBackdrop = forwardRef(
|
|
|
887
864
|
}
|
|
888
865
|
);
|
|
889
866
|
SheetBackdrop.displayName = "SheetBackdrop";
|
|
890
|
-
var SheetBackdrop_default = SheetBackdrop;
|
|
891
867
|
var SheetScroller = forwardRef(
|
|
892
868
|
({ draggableAt = "top", children, style, className = "", ...rest }, ref) => {
|
|
893
869
|
const sheetScrollerContext = useSheetScrollerContext();
|
|
894
870
|
function determineDragState(element) {
|
|
895
871
|
const { scrollTop, scrollHeight, clientHeight } = element;
|
|
896
872
|
const isScrollable2 = scrollHeight > clientHeight;
|
|
897
|
-
if (!isScrollable2)
|
|
898
|
-
return;
|
|
873
|
+
if (!isScrollable2) return;
|
|
899
874
|
const isAtTop = scrollTop <= 0;
|
|
900
875
|
const isAtBottom = scrollHeight - scrollTop === clientHeight;
|
|
901
876
|
const shouldEnable = draggableAt === "top" && isAtTop || draggableAt === "bottom" && isAtBottom || draggableAt === "both" && (isAtTop || isAtBottom);
|
|
@@ -924,7 +899,7 @@ var SheetScroller = forwardRef(
|
|
|
924
899
|
...rest,
|
|
925
900
|
ref,
|
|
926
901
|
className: `react-modal-sheet-scroller ${className}`,
|
|
927
|
-
style: { ...
|
|
902
|
+
style: { ...styles.scroller, ...style },
|
|
928
903
|
...scrollProps
|
|
929
904
|
},
|
|
930
905
|
children
|
|
@@ -932,17 +907,16 @@ var SheetScroller = forwardRef(
|
|
|
932
907
|
}
|
|
933
908
|
);
|
|
934
909
|
SheetScroller.displayName = "SheetScroller";
|
|
935
|
-
var SheetScroller_default = SheetScroller;
|
|
936
910
|
|
|
937
911
|
// src/index.tsx
|
|
938
|
-
var
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
912
|
+
var Sheet2 = Object.assign(Sheet, {
|
|
913
|
+
Container: SheetContainer,
|
|
914
|
+
Header: SheetHeader,
|
|
915
|
+
Content: SheetContent,
|
|
916
|
+
Backdrop: SheetBackdrop,
|
|
917
|
+
Scroller: SheetScroller
|
|
918
|
+
});
|
|
945
919
|
|
|
946
920
|
export { Sheet2 as Sheet };
|
|
947
|
-
//# sourceMappingURL=
|
|
921
|
+
//# sourceMappingURL=index.mjs.map
|
|
948
922
|
//# sourceMappingURL=index.mjs.map
|