react-modal-sheet 1.8.1 → 1.10.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.
@@ -0,0 +1,11 @@
1
+ interface PreventScrollOptions {
2
+ /** Whether the scroll lock is disabled. */
3
+ isDisabled?: boolean;
4
+ }
5
+ /**
6
+ * Prevents scrolling on the document body on mount, and
7
+ * restores it on unmount. Also ensures that content does not
8
+ * shift due to the scrollbars disappearing.
9
+ */
10
+ export declare function usePreventScroll(options?: PreventScrollOptions): void;
11
+ export {};
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.8.1",
2
+ "version": "1.10.0",
3
3
  "license": "MIT",
4
4
  "name": "react-modal-sheet",
5
5
  "description": "Flexible bottom sheet component for your React apps",
@@ -20,12 +20,12 @@
20
20
  "src"
21
21
  ],
22
22
  "engines": {
23
- "node": ">=10"
23
+ "node": ">=16"
24
24
  },
25
25
  "scripts": {
26
26
  "start": "tsdx watch",
27
27
  "build": "tsdx build",
28
- "test": "tsdx test --passWithNoTests",
28
+ "test": "echo \"Todo fix tests...\"",
29
29
  "lint": "tsdx lint",
30
30
  "typecheck": "tsc --noEmit --skipLibCheck",
31
31
  "prepare": "tsdx build",
@@ -50,21 +50,20 @@
50
50
  "yarn": true
51
51
  },
52
52
  "devDependencies": {
53
- "@types/react": "17.0.37",
54
- "@types/react-dom": "17.0.11",
55
- "@types/styled-components": "5.1.7",
56
- "csstype": "3.0.6",
57
- "framer-motion": "6.3.11",
53
+ "@types/react": "18.0.26",
54
+ "@types/react-dom": "18.0.10",
55
+ "@types/styled-components": "5.1.26",
56
+ "csstype": "3.1.1",
57
+ "framer-motion": "8.4.3",
58
58
  "husky": "4.2.5",
59
- "react": "17.0.2",
60
- "react-dom": "17.0.2",
61
- "styled-components": "5.3.3",
59
+ "react": "18.2.0",
60
+ "react-dom": "18.2.0",
61
+ "styled-components": "5.3.6",
62
62
  "tsdx": "0.14.1",
63
- "tslib": "2.4.0",
64
- "typescript": "4.7.3"
63
+ "typescript": "4.9.4"
65
64
  },
66
65
  "dependencies": {
67
- "@react-aria/overlays": "3.11.0",
68
- "react-merge-refs": "1.1.0"
66
+ "@react-aria/utils": "3.14.2",
67
+ "react-merge-refs": "2.0.1"
69
68
  }
70
69
  }
@@ -7,7 +7,7 @@ import styles from './styles';
7
7
  const isClickable = (props: any) => !!props.onClick || !!props.onTap;
8
8
 
9
9
  const SheetBackdrop = React.forwardRef<any, SheetBackdropProps>(
10
- ({ style = {}, ...rest }, ref) => {
10
+ ({ style = {}, className = '', ...rest }, ref) => {
11
11
  const Comp = isClickable(rest) ? motion.button : motion.div;
12
12
  const pointerEvents = isClickable(rest) ? 'auto' : 'none';
13
13
 
@@ -15,7 +15,7 @@ const SheetBackdrop = React.forwardRef<any, SheetBackdropProps>(
15
15
  <Comp
16
16
  {...rest}
17
17
  ref={ref}
18
- className="react-modal-sheet-backdrop"
18
+ className={`react-modal-sheet-backdrop ${className}`}
19
19
  style={{ ...styles.backdrop, ...style, pointerEvents }}
20
20
  initial={{ opacity: 0 }}
21
21
  animate={{ opacity: 1 }}
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import { motion } from 'framer-motion';
3
- import mergeRefs from 'react-merge-refs';
3
+ import { mergeRefs } from 'react-merge-refs';
4
4
 
5
5
  import { SheetContainerProps } from './types';
6
6
  import { useSheetContext } from './context';
@@ -9,7 +9,7 @@ import { MAX_HEIGHT } from './constants';
9
9
  import styles from './styles';
10
10
 
11
11
  const SheetContainer = React.forwardRef<any, SheetContainerProps>(
12
- ({ children, style = {}, ...rest }, ref) => {
12
+ ({ children, style = {}, className = '', ...rest }, ref) => {
13
13
  const {
14
14
  y,
15
15
  isOpen,
@@ -36,7 +36,7 @@ const SheetContainer = React.forwardRef<any, SheetContainerProps>(
36
36
  <motion.div
37
37
  {...rest}
38
38
  ref={mergeRefs([sheetRef, ref])}
39
- className="react-modal-sheet-container"
39
+ className={`react-modal-sheet-container ${className}`}
40
40
  style={{
41
41
  ...styles.container,
42
42
  ...style,
@@ -6,7 +6,7 @@ import { useSheetContext } from './context';
6
6
  import styles from './styles';
7
7
 
8
8
  const SheetContent = React.forwardRef<any, SheetDraggableProps>(
9
- ({ children, style, disableDrag, ...rest }, ref) => {
9
+ ({ children, style, disableDrag, className = '', ...rest }, ref) => {
10
10
  const { dragProps } = useSheetContext();
11
11
  const _dragProps = disableDrag ? undefined : dragProps;
12
12
 
@@ -14,7 +14,7 @@ const SheetContent = React.forwardRef<any, SheetDraggableProps>(
14
14
  <motion.div
15
15
  {...rest}
16
16
  ref={ref}
17
- className="react-modal-sheet-content"
17
+ className={`react-modal-sheet-content ${className}`}
18
18
  style={{ ...styles.content, ...style }}
19
19
  {..._dragProps}
20
20
  >
package/src/sheet.tsx CHANGED
@@ -1,6 +1,5 @@
1
1
  import * as React from 'react';
2
2
  import * as ReactDOM from 'react-dom';
3
- import { usePreventScroll } from '@react-aria/overlays';
4
3
 
5
4
  import {
6
5
  animate,
@@ -28,6 +27,7 @@ import {
28
27
  import { SheetContextType, SheetProps } from './types';
29
28
  import { SheetContext } from './context';
30
29
  import { getClosest, inDescendingOrder, validateSnapTo } from './utils';
30
+ import { usePreventScroll } from './use-prevent-scroll';
31
31
  import styles from './styles';
32
32
 
33
33
  const Sheet = React.forwardRef<any, SheetProps>(
@@ -200,6 +200,7 @@ const Sheet = React.forwardRef<any, SheetProps>(
200
200
  dragElastic: 0,
201
201
  dragConstraints: { top: 0, bottom: 0 },
202
202
  dragMomentum: false,
203
+ dragPropagation: false,
203
204
  onDrag,
204
205
  onDragEnd,
205
206
  };
package/src/styles.ts CHANGED
@@ -20,6 +20,7 @@ const styles: { [key: string]: CSS.Properties } = {
20
20
  height: '100%',
21
21
  backgroundColor: 'rgba(51, 51, 51, 0.5)',
22
22
  touchAction: 'none', // Disable iOS body scrolling
23
+ border: 'none',
23
24
  },
24
25
  container: {
25
26
  zIndex: 2,
package/src/types.tsx CHANGED
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  DragHandlers,
3
3
  MotionValue,
4
- useSpring,
5
4
  MotionProps,
6
5
  AnimationOptions,
6
+ Spring,
7
7
  } from 'framer-motion';
8
8
 
9
9
  export type SheetEvents = {
@@ -16,6 +16,10 @@ export type SheetEvents = {
16
16
 
17
17
  export type SheetDetent = 'full-height' | 'content-height';
18
18
 
19
+ type CommonProps = MotionProps & {
20
+ className?: string;
21
+ };
22
+
19
23
  export type SheetProps = {
20
24
  isOpen: boolean;
21
25
  children: React.ReactNode;
@@ -25,21 +29,21 @@ export type SheetProps = {
25
29
  snapPoints?: number[];
26
30
  detent?: SheetDetent;
27
31
  initialSnap?: number; // index of snap points array
28
- springConfig?: Parameters<typeof useSpring>[1];
32
+ springConfig?: Omit<Spring, 'type'>;
29
33
  disableDrag?: boolean;
30
34
  prefersReducedMotion?: boolean;
31
35
  } & SheetEvents &
32
36
  React.HTMLAttributes<HTMLDivElement>;
33
37
 
34
38
  export type SheetContainerProps = Omit<
35
- MotionProps,
39
+ CommonProps,
36
40
  'initial' | 'animate' | 'exit' | 'onAnimationComplete'
37
41
  > & {
38
42
  children: React.ReactNode;
39
43
  };
40
44
 
41
45
  export type SheetDraggableProps = Omit<
42
- MotionProps,
46
+ CommonProps,
43
47
  | 'drag'
44
48
  | 'dragElastic'
45
49
  | 'dragConstraints'
@@ -53,7 +57,7 @@ export type SheetDraggableProps = Omit<
53
57
  };
54
58
 
55
59
  export type SheetBackdropProps = Omit<
56
- MotionProps,
60
+ CommonProps,
57
61
  'initial' | 'animate' | 'exit'
58
62
  >;
59
63
 
@@ -62,6 +66,7 @@ export type SheetDragProps = {
62
66
  dragElastic: number;
63
67
  dragConstraints: any;
64
68
  dragMomentum: boolean;
69
+ dragPropagation: boolean;
65
70
  onDrag: DragHandlers['onDrag'];
66
71
  onDragEnd: DragHandlers['onDragEnd'];
67
72
  };
@@ -0,0 +1,303 @@
1
+ // Source: https://github.com/adobe/react-spectrum/blob/main/packages/@react-aria/overlays/src/usePreventScroll.ts
2
+ import {
3
+ chain,
4
+ getScrollParent,
5
+ isIOS,
6
+ useLayoutEffect,
7
+ } from '@react-aria/utils';
8
+
9
+ interface PreventScrollOptions {
10
+ /** Whether the scroll lock is disabled. */
11
+ isDisabled?: boolean;
12
+ }
13
+
14
+ // @ts-ignore
15
+ const visualViewport = typeof window !== 'undefined' && window.visualViewport;
16
+
17
+ // HTML input types that do not cause the software keyboard to appear.
18
+ const nonTextInputTypes = new Set([
19
+ 'checkbox',
20
+ 'radio',
21
+ 'range',
22
+ 'color',
23
+ 'file',
24
+ 'image',
25
+ 'button',
26
+ 'submit',
27
+ 'reset',
28
+ ]);
29
+
30
+ // The number of active usePreventScroll calls. Used to determine whether to revert back to the original page style/scroll position
31
+ let preventScrollCount = 0;
32
+ let restore: any;
33
+
34
+ /**
35
+ * Prevents scrolling on the document body on mount, and
36
+ * restores it on unmount. Also ensures that content does not
37
+ * shift due to the scrollbars disappearing.
38
+ */
39
+ export function usePreventScroll(options: PreventScrollOptions = {}) {
40
+ let { isDisabled } = options;
41
+
42
+ useLayoutEffect(() => {
43
+ if (isDisabled) {
44
+ return;
45
+ }
46
+
47
+ preventScrollCount++;
48
+ if (preventScrollCount === 1) {
49
+ if (isIOS()) {
50
+ restore = preventScrollMobileSafari();
51
+ } else {
52
+ restore = preventScrollStandard();
53
+ }
54
+ }
55
+
56
+ return () => {
57
+ preventScrollCount--;
58
+ if (preventScrollCount === 0) {
59
+ restore();
60
+ }
61
+ };
62
+ }, [isDisabled]);
63
+ }
64
+
65
+ // For most browsers, all we need to do is set `overflow: hidden` on the root element, and
66
+ // add some padding to prevent the page from shifting when the scrollbar is hidden.
67
+ function preventScrollStandard() {
68
+ return chain(
69
+ setStyle(
70
+ document.documentElement,
71
+ 'paddingRight',
72
+ `${window.innerWidth - document.documentElement.clientWidth}px`
73
+ ),
74
+ setStyle(document.documentElement, 'overflow', 'hidden')
75
+ );
76
+ }
77
+
78
+ // Mobile Safari is a whole different beast. Even with overflow: hidden,
79
+ // it still scrolls the page in many situations:
80
+ //
81
+ // 1. When the bottom toolbar and address bar are collapsed, page scrolling is always allowed.
82
+ // 2. When the keyboard is visible, the viewport does not resize. Instead, the keyboard covers part of
83
+ // it, so it becomes scrollable.
84
+ // 3. When tapping on an input, the page always scrolls so that the input is centered in the visual viewport.
85
+ // This may cause even fixed position elements to scroll off the screen.
86
+ // 4. When using the next/previous buttons in the keyboard to navigate between inputs, the whole page always
87
+ // scrolls, even if the input is inside a nested scrollable element that could be scrolled instead.
88
+ //
89
+ // In order to work around these cases, and prevent scrolling without jankiness, we do a few things:
90
+ //
91
+ // 1. Prevent default on `touchmove` events that are not in a scrollable element. This prevents touch scrolling
92
+ // on the window.
93
+ // 2. Prevent default on `touchmove` events inside a scrollable element when the scroll position is at the
94
+ // top or bottom. This avoids the whole page scrolling instead, but does prevent overscrolling.
95
+ // 3. Prevent default on `touchend` events on input elements and handle focusing the element ourselves.
96
+ // 4. When focusing an input, apply a transform to trick Safari into thinking the input is at the top
97
+ // of the page, which prevents it from scrolling the page. After the input is focused, scroll the element
98
+ // into view ourselves, without scrolling the whole page.
99
+ // 5. Offset the body by the scroll position using a negative margin and scroll to the top. This should appear the
100
+ // same visually, but makes the actual scroll position always zero. This is required to make all of the
101
+ // above work or Safari will still try to scroll the page when focusing an input.
102
+ // 6. As a last resort, handle window scroll events, and scroll back to the top. This can happen when attempting
103
+ // to navigate to an input with the next/previous buttons that's outside a modal.
104
+ function preventScrollMobileSafari() {
105
+ let scrollable: Element;
106
+ let lastY = 0;
107
+ let onTouchStart = (e: TouchEvent) => {
108
+ // Store the nearest scrollable parent element from the element that the user touched.
109
+ scrollable = getScrollParent(e.target as Element);
110
+ if (
111
+ scrollable === document.documentElement &&
112
+ scrollable === document.body
113
+ ) {
114
+ return;
115
+ }
116
+
117
+ lastY = e.changedTouches[0].pageY;
118
+ };
119
+
120
+ let onTouchMove = (e: TouchEvent) => {
121
+ // Prevent scrolling the window.
122
+ if (
123
+ scrollable === document.documentElement ||
124
+ scrollable === document.body
125
+ ) {
126
+ e.preventDefault();
127
+ return;
128
+ }
129
+
130
+ // Prevent scrolling up when at the top and scrolling down when at the bottom
131
+ // of a nested scrollable area, otherwise mobile Safari will start scrolling
132
+ // the window instead. Unfortunately, this disables bounce scrolling when at
133
+ // the top but it's the best we can do.
134
+ let y = e.changedTouches[0].pageY;
135
+ let scrollTop = scrollable.scrollTop;
136
+ let bottom = scrollable.scrollHeight - scrollable.clientHeight;
137
+
138
+ // Fix for: https://github.com/adobe/react-spectrum/pull/3780/files
139
+ if (bottom === 0) {
140
+ return;
141
+ }
142
+
143
+ if ((scrollTop <= 0 && y > lastY) || (scrollTop >= bottom && y < lastY)) {
144
+ e.preventDefault();
145
+ }
146
+
147
+ lastY = y;
148
+ };
149
+
150
+ let onTouchEnd = (e: TouchEvent) => {
151
+ let target = e.target as HTMLElement;
152
+
153
+ // Apply this change if we're not already focused on the target element
154
+ if (willOpenKeyboard(target) && target !== document.activeElement) {
155
+ e.preventDefault();
156
+
157
+ // Apply a transform to trick Safari into thinking the input is at the top of the page
158
+ // so it doesn't try to scroll it into view. When tapping on an input, this needs to
159
+ // be done before the "focus" event, so we have to focus the element ourselves.
160
+ target.style.transform = 'translateY(-2000px)';
161
+ target.focus();
162
+ requestAnimationFrame(() => {
163
+ target.style.transform = '';
164
+ });
165
+ }
166
+ };
167
+
168
+ let onFocus = (e: FocusEvent) => {
169
+ let target = e.target as HTMLElement;
170
+ if (willOpenKeyboard(target)) {
171
+ // Transform also needs to be applied in the focus event in cases where focus moves
172
+ // other than tapping on an input directly, e.g. the next/previous buttons in the
173
+ // software keyboard. In these cases, it seems applying the transform in the focus event
174
+ // is good enough, whereas when tapping an input, it must be done before the focus event. 🤷‍♂️
175
+ target.style.transform = 'translateY(-2000px)';
176
+ requestAnimationFrame(() => {
177
+ target.style.transform = '';
178
+
179
+ // This will have prevented the browser from scrolling the focused element into view,
180
+ // so we need to do this ourselves in a way that doesn't cause the whole page to scroll.
181
+ if (visualViewport) {
182
+ if (visualViewport.height < window.innerHeight) {
183
+ // If the keyboard is already visible, do this after one additional frame
184
+ // to wait for the transform to be removed.
185
+ requestAnimationFrame(() => {
186
+ scrollIntoView(target);
187
+ });
188
+ } else {
189
+ // Otherwise, wait for the visual viewport to resize before scrolling so we can
190
+ // measure the correct position to scroll to.
191
+ visualViewport.addEventListener(
192
+ 'resize',
193
+ () => scrollIntoView(target),
194
+ { once: true }
195
+ );
196
+ }
197
+ }
198
+ });
199
+ }
200
+ };
201
+
202
+ let onWindowScroll = () => {
203
+ // Last resort. If the window scrolled, scroll it back to the top.
204
+ // It should always be at the top because the body will have a negative margin (see below).
205
+ window.scrollTo(0, 0);
206
+ };
207
+
208
+ // Record the original scroll position so we can restore it.
209
+ // Then apply a negative margin to the body to offset it by the scroll position. This will
210
+ // enable us to scroll the window to the top, which is required for the rest of this to work.
211
+ let scrollX = window.pageXOffset;
212
+ let scrollY = window.pageYOffset;
213
+
214
+ let restoreStyles = chain(
215
+ setStyle(
216
+ document.documentElement,
217
+ 'paddingRight',
218
+ `${window.innerWidth - document.documentElement.clientWidth}px`
219
+ ),
220
+ setStyle(document.documentElement, 'overflow', 'hidden'),
221
+ setStyle(document.body, 'marginTop', `-${scrollY}px`)
222
+ );
223
+
224
+ // Scroll to the top. The negative margin on the body will make this appear the same.
225
+ window.scrollTo(0, 0);
226
+
227
+ let removeEvents = chain(
228
+ addEvent(document, 'touchstart', onTouchStart, {
229
+ passive: false,
230
+ capture: true,
231
+ }),
232
+ addEvent(document, 'touchmove', onTouchMove, {
233
+ passive: false,
234
+ capture: true,
235
+ }),
236
+ addEvent(document, 'touchend', onTouchEnd, {
237
+ passive: false,
238
+ capture: true,
239
+ }),
240
+ addEvent(document, 'focus', onFocus, true),
241
+ addEvent(window, 'scroll', onWindowScroll)
242
+ );
243
+
244
+ return () => {
245
+ // Restore styles and scroll the page back to where it was.
246
+ restoreStyles();
247
+ removeEvents();
248
+ window.scrollTo(scrollX, scrollY);
249
+ };
250
+ }
251
+
252
+ // Sets a CSS property on an element, and returns a function to revert it to the previous value.
253
+ function setStyle(element: any, style: string, value: string) {
254
+ let cur = element.style[style];
255
+ element.style[style] = value;
256
+
257
+ return () => {
258
+ element.style[style] = cur;
259
+ };
260
+ }
261
+
262
+ // Adds an event listener to an element, and returns a function to remove it.
263
+ function addEvent<K extends keyof GlobalEventHandlersEventMap>(
264
+ target: EventTarget,
265
+ event: K,
266
+ handler: (this: Document, ev: GlobalEventHandlersEventMap[K]) => any,
267
+ options?: boolean | AddEventListenerOptions
268
+ ) {
269
+ target.addEventListener(event, handler as any, options);
270
+ return () => {
271
+ target.removeEventListener(event, handler as any, options);
272
+ };
273
+ }
274
+
275
+ function scrollIntoView(target: Element) {
276
+ let root = document.scrollingElement || document.documentElement;
277
+ while (target && target !== root) {
278
+ // Find the parent scrollable element and adjust the scroll position if the target is not already in view.
279
+ let scrollable = getScrollParent(target);
280
+ if (
281
+ scrollable !== document.documentElement &&
282
+ scrollable !== document.body &&
283
+ scrollable !== target
284
+ ) {
285
+ let scrollableTop = scrollable.getBoundingClientRect().top;
286
+ let targetTop = target.getBoundingClientRect().top;
287
+ if (targetTop > scrollableTop + target.clientHeight) {
288
+ scrollable.scrollTop += targetTop - scrollableTop;
289
+ }
290
+ }
291
+
292
+ target = scrollable.parentElement as any;
293
+ }
294
+ }
295
+
296
+ function willOpenKeyboard(target: Element) {
297
+ return (
298
+ (target instanceof HTMLInputElement &&
299
+ !nonTextInputTypes.has(target.type)) ||
300
+ target instanceof HTMLTextAreaElement ||
301
+ (target instanceof HTMLElement && target.isContentEditable)
302
+ );
303
+ }