react-modal-sheet 4.1.0 → 4.3.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 -3
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +81 -47
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +81 -47
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/SheetContainer.tsx +4 -7
- package/src/SheetScroller.tsx +34 -15
- package/src/context.tsx +4 -4
- package/src/sheet.tsx +33 -27
- package/src/styles.ts +2 -0
- package/src/types.tsx +1 -0
- package/src/utils.ts +34 -0
package/dist/index.mjs
CHANGED
|
@@ -46,6 +46,26 @@ function getSnapPoints({
|
|
|
46
46
|
);
|
|
47
47
|
return snapPointValues;
|
|
48
48
|
}
|
|
49
|
+
function getClosestSnapPoint({
|
|
50
|
+
snapPoints,
|
|
51
|
+
currentY,
|
|
52
|
+
sheetHeight,
|
|
53
|
+
detent
|
|
54
|
+
}) {
|
|
55
|
+
const snapInverse = snapPoints.map(
|
|
56
|
+
(p) => sheetHeight - Math.min(p, sheetHeight)
|
|
57
|
+
);
|
|
58
|
+
if (detent === "content-height" && !snapInverse.includes(0)) {
|
|
59
|
+
snapInverse.unshift(0);
|
|
60
|
+
}
|
|
61
|
+
const snapTo = getClosest(snapInverse, currentY);
|
|
62
|
+
const snapIndex = snapInverse.indexOf(snapTo);
|
|
63
|
+
const snapY = validateSnapTo({
|
|
64
|
+
snapTo: getClosest(snapInverse, currentY),
|
|
65
|
+
sheetHeight
|
|
66
|
+
});
|
|
67
|
+
return { snapY, snapIndex };
|
|
68
|
+
}
|
|
49
69
|
function getClosest(nums, goal) {
|
|
50
70
|
let closest = nums[0];
|
|
51
71
|
let minDifference = Math.abs(nums[0] - goal);
|
|
@@ -136,11 +156,11 @@ var isIOS = cached(function() {
|
|
|
136
156
|
var SheetContext = createContext(
|
|
137
157
|
void 0
|
|
138
158
|
);
|
|
139
|
-
|
|
159
|
+
function useSheetContext() {
|
|
140
160
|
const context = useContext(SheetContext);
|
|
141
161
|
if (!context) throw new Error("Sheet context error");
|
|
142
162
|
return context;
|
|
143
|
-
}
|
|
163
|
+
}
|
|
144
164
|
var SheetScrollerContext = createContext(void 0);
|
|
145
165
|
function SheetScrollerContextProvider({
|
|
146
166
|
children
|
|
@@ -161,11 +181,11 @@ function SheetScrollerContextProvider({
|
|
|
161
181
|
children
|
|
162
182
|
);
|
|
163
183
|
}
|
|
164
|
-
|
|
184
|
+
function useSheetScrollerContext() {
|
|
165
185
|
const context = useContext(SheetScrollerContext);
|
|
166
186
|
if (!context) throw new Error("Sheet scroller context error");
|
|
167
187
|
return context;
|
|
168
|
-
}
|
|
188
|
+
}
|
|
169
189
|
|
|
170
190
|
// src/styles.ts
|
|
171
191
|
var styles = {
|
|
@@ -188,6 +208,7 @@ var styles = {
|
|
|
188
208
|
backgroundColor: "rgba(0, 0, 0, 0.2)",
|
|
189
209
|
touchAction: "none",
|
|
190
210
|
border: "none",
|
|
211
|
+
userSelect: "none",
|
|
191
212
|
WebkitTapHighlightColor: "transparent"
|
|
192
213
|
},
|
|
193
214
|
container: {
|
|
@@ -230,7 +251,8 @@ var styles = {
|
|
|
230
251
|
},
|
|
231
252
|
scroller: {
|
|
232
253
|
height: "100%",
|
|
233
|
-
overflowY: "auto"
|
|
254
|
+
overflowY: "auto",
|
|
255
|
+
overscrollBehaviorY: "none"
|
|
234
256
|
}
|
|
235
257
|
};
|
|
236
258
|
var useIsomorphicLayoutEffect = IS_SSR ? useEffect : useLayoutEffect;
|
|
@@ -661,11 +683,21 @@ var Sheet = forwardRef(
|
|
|
661
683
|
onCloseEnd
|
|
662
684
|
};
|
|
663
685
|
});
|
|
664
|
-
const onDrag = useStableCallback((_,
|
|
686
|
+
const onDrag = useStableCallback((_, info) => {
|
|
687
|
+
const currentY = y.get();
|
|
688
|
+
if (snapPointsProp) {
|
|
689
|
+
const sheetHeight = getSheetHeight(sheetRef);
|
|
690
|
+
const snapPoints = getSnapPoints({ snapPointsProp, sheetHeight });
|
|
691
|
+
const topSnapPoint = sheetHeight - snapPoints[0];
|
|
692
|
+
if (info.delta.y < 0 && currentY <= topSnapPoint) {
|
|
693
|
+
y.set(topSnapPoint);
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
665
697
|
const velocity = y.getVelocity();
|
|
666
698
|
if (velocity > 0) indicatorRotation.set(10);
|
|
667
699
|
if (velocity < 0) indicatorRotation.set(-10);
|
|
668
|
-
y.set(Math.max(
|
|
700
|
+
y.set(Math.max(currentY + info.delta.y, 0));
|
|
669
701
|
});
|
|
670
702
|
const onDragStart = useStableCallback(() => {
|
|
671
703
|
const focusedElement = document.activeElement;
|
|
@@ -684,18 +716,13 @@ var Sheet = forwardRef(
|
|
|
684
716
|
let yTo = 0;
|
|
685
717
|
const snapPoints = snapPointsProp ? getSnapPoints({ snapPointsProp, sheetHeight }) : void 0;
|
|
686
718
|
if (snapPoints) {
|
|
687
|
-
const
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
}
|
|
693
|
-
const snapTo = getClosest(snapInverse, currentY);
|
|
694
|
-
const snapIndex = snapInverse.indexOf(snapTo);
|
|
695
|
-
yTo = validateSnapTo({
|
|
696
|
-
snapTo: getClosest(snapInverse, currentY),
|
|
697
|
-
sheetHeight
|
|
719
|
+
const { snapY, snapIndex } = getClosestSnapPoint({
|
|
720
|
+
snapPoints,
|
|
721
|
+
currentY,
|
|
722
|
+
sheetHeight,
|
|
723
|
+
detent
|
|
698
724
|
});
|
|
725
|
+
yTo = snapY;
|
|
699
726
|
onSnap == null ? void 0 : onSnap(snapIndex);
|
|
700
727
|
} else if (currentY / sheetHeight > dragCloseThreshold) {
|
|
701
728
|
yTo = sheetHeight;
|
|
@@ -719,9 +746,9 @@ var Sheet = forwardRef(
|
|
|
719
746
|
const snapIndex = initialSnap;
|
|
720
747
|
const sheetHeight = getSheetHeight(sheetRef);
|
|
721
748
|
const snapPoints = getSnapPoints({ snapPointsProp, sheetHeight });
|
|
722
|
-
const
|
|
723
|
-
if (
|
|
724
|
-
yTo =
|
|
749
|
+
const snapY = getSnapY({ sheetHeight, snapPoints, snapIndex });
|
|
750
|
+
if (snapY !== null) {
|
|
751
|
+
yTo = snapY;
|
|
725
752
|
}
|
|
726
753
|
}
|
|
727
754
|
animate(y, yTo, animationOptions);
|
|
@@ -735,11 +762,11 @@ var Sheet = forwardRef(
|
|
|
735
762
|
}
|
|
736
763
|
const sheetHeight = getSheetHeight(sheetRef);
|
|
737
764
|
const snapPoints = getSnapPoints({ snapPointsProp, sheetHeight });
|
|
738
|
-
const
|
|
739
|
-
if (
|
|
740
|
-
animate(y,
|
|
765
|
+
const snapY = getSnapY({ sheetHeight, snapPoints, snapIndex });
|
|
766
|
+
if (snapY !== null) {
|
|
767
|
+
animate(y, snapY, animationOptions);
|
|
741
768
|
onSnap == null ? void 0 : onSnap(snapIndex);
|
|
742
|
-
if (
|
|
769
|
+
if (snapY + 1 >= sheetHeight) {
|
|
743
770
|
onClose();
|
|
744
771
|
}
|
|
745
772
|
}
|
|
@@ -847,19 +874,15 @@ var SheetContainer = forwardRef(
|
|
|
847
874
|
animationOptions
|
|
848
875
|
} = useSheetContext();
|
|
849
876
|
const { handleAnimationComplete } = useEventCallbacks(isOpen, callbacks);
|
|
877
|
+
const height = detent === "full-height" ? MAX_HEIGHT : void 0;
|
|
878
|
+
const maxHeight = detent === "content-height" ? MAX_HEIGHT : void 0;
|
|
850
879
|
return /* @__PURE__ */ React2.createElement(
|
|
851
880
|
motion.div,
|
|
852
881
|
{
|
|
853
882
|
...rest,
|
|
854
883
|
ref: mergeRefs([sheetRef, ref]),
|
|
855
884
|
className: `react-modal-sheet-container ${className}`,
|
|
856
|
-
style: {
|
|
857
|
-
...styles.container,
|
|
858
|
-
...style,
|
|
859
|
-
...detent === "full-height" && { height: MAX_HEIGHT },
|
|
860
|
-
...detent === "content-height" && { maxHeight: MAX_HEIGHT },
|
|
861
|
-
y
|
|
862
|
-
},
|
|
885
|
+
style: { ...styles.container, height, maxHeight, ...style, y },
|
|
863
886
|
initial: { y: windowHeight },
|
|
864
887
|
exit: { y: windowHeight, transition: animationOptions },
|
|
865
888
|
onAnimationComplete: handleAnimationComplete
|
|
@@ -960,9 +983,20 @@ var SheetBackdrop = forwardRef(
|
|
|
960
983
|
);
|
|
961
984
|
SheetBackdrop.displayName = "SheetBackdrop";
|
|
962
985
|
var SheetScroller = forwardRef(
|
|
963
|
-
({
|
|
964
|
-
|
|
986
|
+
({
|
|
987
|
+
disableScroll = false,
|
|
988
|
+
draggableAt = "top",
|
|
989
|
+
children,
|
|
990
|
+
style,
|
|
991
|
+
className = "",
|
|
992
|
+
...rest
|
|
993
|
+
}, ref) => {
|
|
994
|
+
const { setDragEnabled, setDragDisabled } = useSheetScrollerContext();
|
|
965
995
|
function determineDragState(element) {
|
|
996
|
+
if (disableScroll) {
|
|
997
|
+
setDragEnabled();
|
|
998
|
+
return;
|
|
999
|
+
}
|
|
966
1000
|
const { scrollTop, scrollHeight, clientHeight } = element;
|
|
967
1001
|
const isScrollable2 = scrollHeight > clientHeight;
|
|
968
1002
|
if (!isScrollable2) return;
|
|
@@ -970,22 +1004,18 @@ var SheetScroller = forwardRef(
|
|
|
970
1004
|
const isAtBottom = scrollHeight - scrollTop === clientHeight;
|
|
971
1005
|
const shouldEnable = draggableAt === "top" && isAtTop || draggableAt === "bottom" && isAtBottom || draggableAt === "both" && (isAtTop || isAtBottom);
|
|
972
1006
|
if (shouldEnable) {
|
|
973
|
-
|
|
1007
|
+
setDragEnabled();
|
|
974
1008
|
} else {
|
|
975
|
-
|
|
1009
|
+
setDragDisabled();
|
|
976
1010
|
}
|
|
977
1011
|
}
|
|
978
|
-
function onScroll(
|
|
979
|
-
determineDragState(
|
|
980
|
-
if (rest.onScroll)
|
|
981
|
-
rest.onScroll(e);
|
|
982
|
-
}
|
|
1012
|
+
function onScroll(event) {
|
|
1013
|
+
determineDragState(event.currentTarget);
|
|
1014
|
+
if (rest.onScroll) rest.onScroll(event);
|
|
983
1015
|
}
|
|
984
|
-
function onTouchStart(
|
|
985
|
-
determineDragState(
|
|
986
|
-
if (rest.onTouchStart)
|
|
987
|
-
rest.onTouchStart(e);
|
|
988
|
-
}
|
|
1016
|
+
function onTouchStart(event) {
|
|
1017
|
+
determineDragState(event.currentTarget);
|
|
1018
|
+
if (rest.onTouchStart) rest.onTouchStart(event);
|
|
989
1019
|
}
|
|
990
1020
|
const scrollProps = isTouchDevice() ? { onScroll, onTouchStart } : void 0;
|
|
991
1021
|
return /* @__PURE__ */ React2.createElement(
|
|
@@ -994,7 +1024,11 @@ var SheetScroller = forwardRef(
|
|
|
994
1024
|
...rest,
|
|
995
1025
|
ref,
|
|
996
1026
|
className: `react-modal-sheet-scroller ${className}`,
|
|
997
|
-
style: {
|
|
1027
|
+
style: {
|
|
1028
|
+
...styles.scroller,
|
|
1029
|
+
...style,
|
|
1030
|
+
overflowY: disableScroll ? "hidden" : "auto"
|
|
1031
|
+
},
|
|
998
1032
|
...scrollProps
|
|
999
1033
|
},
|
|
1000
1034
|
children
|