react-modal-sheet 1.11.1 → 2.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/src/sheet.tsx CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import * as ReactDOM from 'react-dom';
3
+
3
4
  import {
4
5
  animate,
5
6
  AnimatePresence,
@@ -19,14 +20,15 @@ import {
19
20
  } from './hooks';
20
21
 
21
22
  import {
22
- DEFAULT_SPRING_CONFIG,
23
+ REDUCED_MOTION_TWEEN_CONFIG,
24
+ DEFAULT_TWEEN_CONFIG,
23
25
  DRAG_CLOSE_THRESHOLD,
24
26
  DRAG_VELOCITY_THRESHOLD,
25
27
  IS_SSR,
26
28
  } from './constants';
27
29
 
28
30
  import { SheetContextType, SheetProps } from './types';
29
- import { SheetContext } from './context';
31
+ import { SheetScrollerContextProvider, SheetContext } from './context';
30
32
  import { getClosest, inDescendingOrder, validateSnapTo } from './utils';
31
33
  import { usePreventScroll } from './use-prevent-scroll';
32
34
  import styles from './styles';
@@ -48,9 +50,9 @@ const Sheet = React.forwardRef<any, SheetProps>(
48
50
  style,
49
51
  detent = 'full-height',
50
52
  initialSnap = 0,
51
- springConfig = DEFAULT_SPRING_CONFIG,
52
53
  disableDrag = false,
53
54
  prefersReducedMotion = false,
55
+ tweenConfig = DEFAULT_TWEEN_CONFIG,
54
56
  ...rest
55
57
  },
56
58
  ref
@@ -60,9 +62,10 @@ const Sheet = React.forwardRef<any, SheetProps>(
60
62
  const windowHeight = useWindowHeight();
61
63
  const shouldReduceMotion = useReducedMotion();
62
64
  const reduceMotion = Boolean(prefersReducedMotion || shouldReduceMotion);
63
- const animationOptions: Transition = reduceMotion
64
- ? { type: 'tween', duration: 0.01 }
65
- : { type: 'spring', ...springConfig };
65
+ const animationOptions: Transition = {
66
+ type: 'tween',
67
+ ...(reduceMotion ? REDUCED_MOTION_TWEEN_CONFIG : tweenConfig),
68
+ };
66
69
 
67
70
  // NOTE: the inital value for `y` doesn't matter since it is overwritten by
68
71
  // the value driven by the `AnimatePresence` component when the sheet is opened
@@ -233,6 +236,7 @@ const Sheet = React.forwardRef<any, SheetProps>(
233
236
  windowHeight,
234
237
  animationOptions,
235
238
  reduceMotion,
239
+ disableDrag,
236
240
  };
237
241
 
238
242
  const sheet = (
@@ -244,11 +248,13 @@ const Sheet = React.forwardRef<any, SheetProps>(
244
248
  >
245
249
  <AnimatePresence>
246
250
  {/* NOTE: AnimatePresence requires us to set keys to children */}
247
- {isOpen
248
- ? React.Children.map(children, (child: any, i) =>
251
+ {isOpen ? (
252
+ <SheetScrollerContextProvider>
253
+ {React.Children.map(children, (child: any, i) =>
249
254
  React.cloneElement(child, { key: `sheet-child-${i}` })
250
- )
251
- : null}
255
+ )}
256
+ </SheetScrollerContextProvider>
257
+ ) : null}
252
258
  </AnimatePresence>
253
259
  </motion.div>
254
260
  </SheetContext.Provider>
package/src/styles.ts CHANGED
@@ -17,7 +17,7 @@ const styles: { [key: string]: CSS.Properties } = {
17
17
  left: 0,
18
18
  width: '100%',
19
19
  height: '100%',
20
- backgroundColor: 'rgba(51, 51, 51, 0.5)',
20
+ backgroundColor: 'rgba(0, 0, 0, 0.2)',
21
21
  touchAction: 'none', // Disable iOS body scrolling
22
22
  border: 'none',
23
23
  },
@@ -53,10 +53,16 @@ const styles: { [key: string]: CSS.Properties } = {
53
53
  backgroundColor: '#ddd',
54
54
  },
55
55
  content: {
56
- flex: 1,
57
- overflow: 'auto',
56
+ flexGrow: 1,
57
+ display: 'flex',
58
+ flexDirection: 'column',
59
+ minHeight: '0px',
58
60
  position: 'relative',
59
61
  },
62
+ scroller: {
63
+ height: '100%',
64
+ overflowY: 'auto',
65
+ },
60
66
  };
61
67
 
62
68
  export default styles;
package/src/types.tsx CHANGED
@@ -2,9 +2,9 @@ import {
2
2
  DragHandlers,
3
3
  MotionValue,
4
4
  MotionProps,
5
- Transition,
6
- Spring,
7
5
  motion,
6
+ EasingDefinition,
7
+ Transition,
8
8
  } from 'framer-motion';
9
9
 
10
10
  export type SheetEvents = {
@@ -21,6 +21,11 @@ type CommonProps = MotionProps & {
21
21
  className?: string;
22
22
  };
23
23
 
24
+ export type SheetTweenConfig = {
25
+ ease: EasingDefinition;
26
+ duration: number;
27
+ };
28
+
24
29
  export type SheetProps = {
25
30
  isOpen: boolean;
26
31
  children: React.ReactNode;
@@ -30,7 +35,7 @@ export type SheetProps = {
30
35
  snapPoints?: number[];
31
36
  detent?: SheetDetent;
32
37
  initialSnap?: number; // index of snap points array
33
- springConfig?: Omit<Spring, 'type'>;
38
+ tweenConfig?: SheetTweenConfig;
34
39
  disableDrag?: boolean;
35
40
  prefersReducedMotion?: boolean;
36
41
  } & SheetEvents &
@@ -62,6 +67,10 @@ export type SheetBackdropProps = Omit<
62
67
  'initial' | 'animate' | 'exit'
63
68
  >;
64
69
 
70
+ export type SheetScrollerProps = React.HTMLAttributes<HTMLDivElement> & {
71
+ draggableAt?: 'top' | 'bottom' | 'both';
72
+ };
73
+
65
74
  export type SheetDragProps = {
66
75
  drag: 'y';
67
76
  dragElastic: number;
@@ -85,6 +94,13 @@ export type SheetContextType = {
85
94
  windowHeight: number;
86
95
  animationOptions: Transition;
87
96
  reduceMotion: boolean;
97
+ disableDrag: boolean;
98
+ };
99
+
100
+ export type SheetScrollerContextType = {
101
+ disableDrag: boolean;
102
+ setDragDisabled: () => void;
103
+ setDragEnabled: () => void;
88
104
  };
89
105
 
90
106
  type ContainerComponent = React.ForwardRefExoticComponent<
@@ -103,11 +119,16 @@ type SheetComponent = React.ForwardRefExoticComponent<
103
119
  SheetProps & React.RefAttributes<any>
104
120
  >;
105
121
 
122
+ type ScrollerComponent = React.ForwardRefExoticComponent<
123
+ SheetScrollerProps & React.RefAttributes<any>
124
+ >;
125
+
106
126
  type SheetCompoundComponent = {
107
127
  Container: ContainerComponent;
108
128
  Header: DraggableComponent;
109
129
  Content: DraggableComponent;
110
130
  Backdrop: BackdropComponent;
131
+ Scroller: ScrollerComponent;
111
132
  };
112
133
 
113
134
  export type SheetCompound = SheetComponent & SheetCompoundComponent;
package/src/utils.ts CHANGED
@@ -15,8 +15,6 @@ export function getClosest(nums: number[], goal: number) {
15
15
  return closest;
16
16
  }
17
17
 
18
- const highlightId = 'react-modal-sheet-highlight';
19
-
20
18
  export function applyRootStyles(rootId: string) {
21
19
  const body = document.querySelector('body') as HTMLBodyElement;
22
20
  const root = document.querySelector(`#${rootId}`) as HTMLDivElement;
@@ -28,33 +26,17 @@ export function applyRootStyles(rootId: string) {
28
26
  body.style.backgroundColor = '#000';
29
27
  root.style.overflow = 'hidden';
30
28
  root.style.willChange = 'transform';
31
- root.style.transition = 'transform 200ms linear';
29
+ root.style.transition =
30
+ 'transform 200ms ease-in-out, border-radius 200ms linear';
32
31
  root.style.transform = `translateY(calc(env(safe-area-inset-top) + ${p / 2}px)) scale(${s})`; // prettier-ignore
33
32
  root.style.borderTopRightRadius = '10px';
34
33
  root.style.borderTopLeftRadius = '10px';
35
-
36
- // Add highlighed overlay to emphasize the modality effect
37
- const highlight = document.createElement('div');
38
- highlight.setAttribute('aria-hidden', 'true');
39
- highlight.id = highlightId;
40
- highlight.style.position = 'absolute';
41
- highlight.style.top = '0px';
42
- highlight.style.left = '0px';
43
- highlight.style.bottom = '0px';
44
- highlight.style.right = '0px';
45
- highlight.style.opacity = '0';
46
- highlight.style.transition = 'opacity 200ms linear';
47
- highlight.style.backgroundColor = 'rgba(150, 150, 150, 0.1)';
48
-
49
- root.appendChild(highlight);
50
- requestAnimationFrame(() => (highlight.style.opacity = '1'));
51
34
  }
52
35
  }
53
36
 
54
37
  export function cleanupRootStyles(rootId: string) {
55
38
  const body = document.querySelector('body') as HTMLBodyElement;
56
39
  const root = document.getElementById(rootId) as HTMLDivElement;
57
- const highlight = document.getElementById(highlightId) as HTMLDivElement;
58
40
 
59
41
  function onTransitionEnd() {
60
42
  root.style.removeProperty('overflow');
@@ -62,15 +44,13 @@ export function cleanupRootStyles(rootId: string) {
62
44
  root.style.removeProperty('transition');
63
45
  body.style.removeProperty('background-color');
64
46
  root.removeEventListener('transitionend', onTransitionEnd);
65
- highlight.parentNode?.removeChild(highlight);
66
47
  }
67
48
 
68
- if (root && highlight) {
49
+ if (root) {
69
50
  // Start animating back
70
51
  root.style.removeProperty('border-top-right-radius');
71
52
  root.style.removeProperty('border-top-left-radius');
72
53
  root.style.removeProperty('transform');
73
- highlight.style.opacity = '0';
74
54
 
75
55
  // Remove temp properties after animation is finished
76
56
  root.addEventListener('transitionend', onTransitionEnd);
@@ -115,3 +95,8 @@ export function mergeRefs<T = any>(
115
95
  });
116
96
  };
117
97
  }
98
+
99
+ export function isTouchDevice() {
100
+ if (typeof window === 'undefined') return false;
101
+ return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
102
+ }