react-modal-sheet 3.4.1 → 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/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 'framer-motion';
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
- // eslint-disable-next-line @typescript-eslint/dot-notation
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) {
@@ -364,7 +352,9 @@ function preventScrollMobileSafari() {
364
352
  let scrollable;
365
353
  let lastY = 0;
366
354
  const onTouchStart = (e) => {
367
- scrollable = getScrollParent(e.target, true);
355
+ var _a;
356
+ const target = (_a = e.composedPath()) == null ? void 0 : _a[0];
357
+ scrollable = getScrollParent(target, true);
368
358
  if (scrollable === document.documentElement && scrollable === document.body) {
369
359
  return;
370
360
  }
@@ -390,7 +380,8 @@ function preventScrollMobileSafari() {
390
380
  lastY = y;
391
381
  };
392
382
  const onTouchEnd = (e) => {
393
- const target = e.target;
383
+ var _a;
384
+ const target = (_a = e.composedPath()) == null ? void 0 : _a[0];
394
385
  if (willOpenKeyboard(target) && target !== document.activeElement) {
395
386
  e.preventDefault();
396
387
  target.style.transform = "translateY(-2000px)";
@@ -401,7 +392,8 @@ function preventScrollMobileSafari() {
401
392
  }
402
393
  };
403
394
  const onFocus = (e) => {
404
- const target = e.target;
395
+ var _a;
396
+ const target = (_a = e.composedPath()) == null ? void 0 : _a[0];
405
397
  if (willOpenKeyboard(target)) {
406
398
  target.style.transform = "translateY(-2000px)";
407
399
  requestAnimationFrame(() => {
@@ -513,7 +505,8 @@ var styles = {
513
505
  backgroundColor: "rgba(0, 0, 0, 0.2)",
514
506
  touchAction: "none",
515
507
  // Disable iOS body scrolling
516
- border: "none"
508
+ border: "none",
509
+ WebkitTapHighlightColor: "transparent"
517
510
  },
518
511
  container: {
519
512
  zIndex: 2,
@@ -558,7 +551,6 @@ var styles = {
558
551
  overflowY: "auto"
559
552
  }
560
553
  };
561
- var styles_default = styles;
562
554
 
563
555
  // src/sheet.tsx
564
556
  var Sheet = forwardRef(
@@ -597,7 +589,7 @@ var Sheet = forwardRef(
597
589
  const y = useMotionValue(windowHeight);
598
590
  const zIndex = useTransform(
599
591
  y,
600
- (value) => value + 2 >= windowHeight ? -1 : 9999999
592
+ (value) => value + 2 >= windowHeight ? -1 : (style == null ? void 0 : style.zIndex) ?? 9999
601
593
  );
602
594
  const visibility = useTransform(
603
595
  y,
@@ -620,8 +612,7 @@ var Sheet = forwardRef(
620
612
  let snapPoints = snapPointsProp;
621
613
  if (snapPoints) {
622
614
  snapPoints = snapPoints.map((point) => {
623
- if (point > 0 && point <= 1)
624
- return Math.round(point * windowHeight);
615
+ if (point > 0 && point <= 1) return Math.round(point * windowHeight);
625
616
  return point < 0 ? windowHeight + point : point;
626
617
  });
627
618
  console.assert(
@@ -633,16 +624,13 @@ var Sheet = forwardRef(
633
624
  }
634
625
  const onDrag = useEffectEvent((_, { delta }) => {
635
626
  const velocity = y.getVelocity();
636
- if (velocity > 0)
637
- indicatorRotation.set(10);
638
- if (velocity < 0)
639
- indicatorRotation.set(-10);
627
+ if (velocity > 0) indicatorRotation.set(10);
628
+ if (velocity < 0) indicatorRotation.set(-10);
640
629
  y.set(Math.max(y.get() + delta.y, 0));
641
630
  });
642
631
  const onDragStart = useEffectEvent(() => {
643
632
  const focusedElement = document.activeElement;
644
- if (!focusedElement || !sheetRef.current)
645
- return;
633
+ if (!focusedElement || !sheetRef.current) return;
646
634
  const isInput = focusedElement.tagName === "INPUT" || focusedElement.tagName === "TEXTAREA";
647
635
  if (isInput && sheetRef.current.contains(focusedElement)) {
648
636
  focusedElement.blur();
@@ -667,22 +655,22 @@ var Sheet = forwardRef(
667
655
  snapTo = sheetHeight;
668
656
  }
669
657
  snapTo = validateSnapTo({ snapTo, sheetHeight });
670
- void animate(y, snapTo, animationOptions);
658
+ animate(y, snapTo, animationOptions);
671
659
  if (snapPoints && onSnap) {
672
660
  const snapValue = Math.abs(Math.round(snapPoints[0] - snapTo));
673
- const snapIndex = snapPoints.indexOf(getClosest(snapPoints, snapValue));
661
+ const snapIndex = snapPoints.indexOf(
662
+ getClosest(snapPoints, snapValue)
663
+ );
674
664
  onSnap(snapIndex);
675
665
  }
676
666
  const roundedSheetHeight = Math.round(sheetHeight);
677
667
  const shouldClose = snapTo + 2 >= roundedSheetHeight;
678
- if (shouldClose)
679
- onClose();
668
+ if (shouldClose) onClose();
680
669
  }
681
670
  indicatorRotation.set(0);
682
671
  });
683
672
  useEffect(() => {
684
- if (!snapPoints || !onSnap)
685
- return;
673
+ if (!snapPoints || !onSnap) return;
686
674
  const snapIndex = isOpen ? initialSnap : snapPoints.length - 1;
687
675
  onSnap(snapIndex);
688
676
  }, [isOpen]);
@@ -697,11 +685,9 @@ var Sheet = forwardRef(
697
685
  snapTo: sheetHeight - snapPoint,
698
686
  sheetHeight
699
687
  });
700
- void animate(y, snapTo, animationOptions);
701
- if (onSnap)
702
- onSnap(snapIndex);
703
- if (snapTo >= sheetHeight)
704
- onClose();
688
+ animate(y, snapTo, animationOptions);
689
+ if (onSnap) onSnap(snapIndex);
690
+ if (snapTo >= sheetHeight) onClose();
705
691
  }
706
692
  }
707
693
  }));
@@ -743,20 +729,18 @@ var Sheet = forwardRef(
743
729
  {
744
730
  ...rest,
745
731
  ref,
746
- style: { ...styles_default.wrapper, zIndex, visibility, ...style }
732
+ style: { ...styles.wrapper, zIndex, visibility, ...style }
747
733
  },
748
734
  /* @__PURE__ */ React2.createElement(AnimatePresence, null, isOpen ? /* @__PURE__ */ React2.createElement(SheetScrollerContextProvider, null, Children.map(
749
735
  children,
750
736
  (child, i) => cloneElement(child, { key: `sheet-child-${i}` })
751
737
  )) : null)
752
738
  ));
753
- if (IS_SSR)
754
- return sheet;
739
+ if (IS_SSR) return sheet;
755
740
  return createPortal(sheet, mountPoint ?? document.body);
756
741
  }
757
742
  );
758
743
  Sheet.displayName = "Sheet";
759
- var sheet_default = Sheet;
760
744
  var SheetContainer = forwardRef(
761
745
  ({ children, style = {}, className = "", ...rest }, ref) => {
762
746
  const {
@@ -782,7 +766,7 @@ var SheetContainer = forwardRef(
782
766
  ref: mergeRefs([sheetRef, ref]),
783
767
  className: `react-modal-sheet-container ${className}`,
784
768
  style: {
785
- ...styles_default.container,
769
+ ...styles.container,
786
770
  ...style,
787
771
  ...detent === "full-height" && { height },
788
772
  ...detent === "content-height" && { maxHeight: height },
@@ -798,7 +782,6 @@ var SheetContainer = forwardRef(
798
782
  }
799
783
  );
800
784
  SheetContainer.displayName = "SheetContainer";
801
- var SheetContainer_default = SheetContainer;
802
785
  var SheetContent = forwardRef(
803
786
  ({ children, style, disableDrag, className = "", ...rest }, ref) => {
804
787
  const sheetContext = useSheetContext();
@@ -811,7 +794,7 @@ var SheetContent = forwardRef(
811
794
  ...rest,
812
795
  ref: mergeRefs([ref, constraintsRef]),
813
796
  className: `react-modal-sheet-content ${className}`,
814
- style: { ...styles_default.content, ...style },
797
+ style: { ...styles.content, ...style },
815
798
  ...dragProps,
816
799
  dragConstraints: constraintsRef,
817
800
  onMeasureDragConstraints
@@ -821,7 +804,6 @@ var SheetContent = forwardRef(
821
804
  }
822
805
  );
823
806
  SheetContent.displayName = "SheetContent";
824
- var SheetContent_default = SheetContent;
825
807
  var SheetHeader = forwardRef(
826
808
  ({ children, style, disableDrag, ...rest }, ref) => {
827
809
  const { indicatorRotation, dragProps } = useSheetContext();
@@ -840,29 +822,28 @@ var SheetHeader = forwardRef(
840
822
  {
841
823
  ...rest,
842
824
  ref: mergeRefs([ref, constraintsRef]),
843
- style: { ...styles_default.headerWrapper, ...style },
825
+ style: { ...styles.headerWrapper, ...style },
844
826
  ..._dragProps,
845
827
  dragConstraints: constraintsRef,
846
828
  onMeasureDragConstraints
847
829
  },
848
- children || /* @__PURE__ */ React2.createElement("div", { className: "react-modal-sheet-header", style: styles_default.header }, /* @__PURE__ */ React2.createElement(
830
+ children || /* @__PURE__ */ React2.createElement("div", { className: "react-modal-sheet-header", style: styles.header }, /* @__PURE__ */ React2.createElement(
849
831
  motion.span,
850
832
  {
851
833
  className: "react-modal-sheet-drag-indicator",
852
- style: { ...styles_default.indicator, transform: indicator1Transform }
834
+ style: { ...styles.indicator, transform: indicator1Transform }
853
835
  }
854
836
  ), /* @__PURE__ */ React2.createElement(
855
837
  motion.span,
856
838
  {
857
839
  className: "react-modal-sheet-drag-indicator",
858
- style: { ...styles_default.indicator, transform: indicator2Transform }
840
+ style: { ...styles.indicator, transform: indicator2Transform }
859
841
  }
860
842
  ))
861
843
  );
862
844
  }
863
845
  );
864
846
  SheetHeader.displayName = "SheetHeader";
865
- var SheetHeader_default = SheetHeader;
866
847
  var isClickable = (props) => !!props.onClick || !!props.onTap;
867
848
  var SheetBackdrop = forwardRef(
868
849
  ({ style = {}, className = "", ...rest }, ref) => {
@@ -874,7 +855,7 @@ var SheetBackdrop = forwardRef(
874
855
  ...rest,
875
856
  ref,
876
857
  className: `react-modal-sheet-backdrop ${className}`,
877
- style: { ...styles_default.backdrop, ...style, pointerEvents },
858
+ style: { ...styles.backdrop, ...style, pointerEvents },
878
859
  initial: { opacity: 0 },
879
860
  animate: { opacity: 1 },
880
861
  exit: { opacity: 0 }
@@ -883,15 +864,13 @@ var SheetBackdrop = forwardRef(
883
864
  }
884
865
  );
885
866
  SheetBackdrop.displayName = "SheetBackdrop";
886
- var SheetBackdrop_default = SheetBackdrop;
887
867
  var SheetScroller = forwardRef(
888
868
  ({ draggableAt = "top", children, style, className = "", ...rest }, ref) => {
889
869
  const sheetScrollerContext = useSheetScrollerContext();
890
870
  function determineDragState(element) {
891
871
  const { scrollTop, scrollHeight, clientHeight } = element;
892
872
  const isScrollable2 = scrollHeight > clientHeight;
893
- if (!isScrollable2)
894
- return;
873
+ if (!isScrollable2) return;
895
874
  const isAtTop = scrollTop <= 0;
896
875
  const isAtBottom = scrollHeight - scrollTop === clientHeight;
897
876
  const shouldEnable = draggableAt === "top" && isAtTop || draggableAt === "bottom" && isAtBottom || draggableAt === "both" && (isAtTop || isAtBottom);
@@ -920,7 +899,7 @@ var SheetScroller = forwardRef(
920
899
  ...rest,
921
900
  ref,
922
901
  className: `react-modal-sheet-scroller ${className}`,
923
- style: { ...styles_default.scroller, ...style },
902
+ style: { ...styles.scroller, ...style },
924
903
  ...scrollProps
925
904
  },
926
905
  children
@@ -928,17 +907,16 @@ var SheetScroller = forwardRef(
928
907
  }
929
908
  );
930
909
  SheetScroller.displayName = "SheetScroller";
931
- var SheetScroller_default = SheetScroller;
932
910
 
933
911
  // src/index.tsx
934
- var _SheetCompound = sheet_default;
935
- _SheetCompound.Container = SheetContainer_default;
936
- _SheetCompound.Header = SheetHeader_default;
937
- _SheetCompound.Content = SheetContent_default;
938
- _SheetCompound.Backdrop = SheetBackdrop_default;
939
- _SheetCompound.Scroller = SheetScroller_default;
940
- var Sheet2 = _SheetCompound;
912
+ var Sheet2 = Object.assign(Sheet, {
913
+ Container: SheetContainer,
914
+ Header: SheetHeader,
915
+ Content: SheetContent,
916
+ Backdrop: SheetBackdrop,
917
+ Scroller: SheetScroller
918
+ });
941
919
 
942
920
  export { Sheet2 as Sheet };
943
- //# sourceMappingURL=out.js.map
921
+ //# sourceMappingURL=index.mjs.map
944
922
  //# sourceMappingURL=index.mjs.map