react-modal-sheet 5.1.1 → 5.1.2

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,8 +1,8 @@
1
1
  import {
2
- type DragHandler,
3
- type Transition,
4
2
  animate,
3
+ type DragHandler,
5
4
  motion,
5
+ type Transition,
6
6
  useMotionValue,
7
7
  useReducedMotion,
8
8
  useTransform,
@@ -37,7 +37,7 @@ import {
37
37
  } from './snap';
38
38
  import { styles } from './styles';
39
39
  import { type SheetContextType, type SheetProps } from './types';
40
- import { applyStyles } from './utils';
40
+ import { applyStyles, waitForElement } from './utils';
41
41
 
42
42
  export const Sheet = forwardRef<any, SheetProps>(
43
43
  (
@@ -298,6 +298,13 @@ export const Sheet = forwardRef<any, SheetProps>(
298
298
  onOpen: async () => {
299
299
  onOpenStart?.();
300
300
 
301
+ /**
302
+ * This is not very React-y but we need to wait for the sheet
303
+ * but we need to wait for the sheet to be rendered and visible
304
+ * before we can measure it and animate it to the initial snap point.
305
+ */
306
+ await waitForElement('react-modal-sheet-container');
307
+
301
308
  const initialSnapPoint =
302
309
  initialSnap !== undefined ? getSnapPoint(initialSnap) : null;
303
310
 
package/src/snap.ts CHANGED
@@ -43,8 +43,24 @@ export function computeSnapPoints({
43
43
  snapPointsProp: number[];
44
44
  sheetHeight: number;
45
45
  }): SheetSnapPoint[] {
46
+ if (snapPointsProp[0] !== 0) {
47
+ console.error(
48
+ 'First snap point should be 0 to ensure the sheet can be fully closed. ' +
49
+ `Got: [${snapPointsProp.join(', ')}]`
50
+ );
51
+ snapPointsProp.unshift(0);
52
+ }
53
+
54
+ if (snapPointsProp[snapPointsProp.length - 1] !== 1) {
55
+ console.error(
56
+ 'Last snap point should be 1 to ensure the sheet can be fully opened. ' +
57
+ `Got: [${snapPointsProp.join(', ')}]`
58
+ );
59
+ snapPointsProp.push(1);
60
+ }
61
+
46
62
  if (sheetHeight <= 0) {
47
- console.warn(
63
+ console.error(
48
64
  `Sheet height is ${sheetHeight}, cannot compute snap points. ` +
49
65
  'Make sure the sheet is mounted and has a valid height.'
50
66
  );
package/src/utils.ts CHANGED
@@ -67,3 +67,24 @@ const isIPad = cached(function () {
67
67
  export const isIOS = cached(function () {
68
68
  return isIPhone() || isIPad();
69
69
  });
70
+
71
+ /** Wait for an element to be rendered and visible */
72
+ export function waitForElement(
73
+ className: string,
74
+ interval = 50,
75
+ maxAttempts = 20
76
+ ) {
77
+ return new Promise<HTMLElement | null>((resolve) => {
78
+ let attempts = 0;
79
+ const timer = setInterval(() => {
80
+ const element = document.getElementsByClassName(
81
+ className
82
+ )[0] as HTMLElement;
83
+ attempts++;
84
+ if (element || attempts >= maxAttempts) {
85
+ clearInterval(timer);
86
+ resolve(element);
87
+ }
88
+ }, interval);
89
+ });
90
+ }