react-modal-sheet 5.3.1 → 5.4.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
@@ -211,13 +211,16 @@ var SheetBackdrop = forwardRef(
211
211
  ...style,
212
212
  pointerEvents
213
213
  };
214
+ const animationProps = sheetContext.prefersReducedMotion ? {} : {
215
+ initial: { opacity: 0 },
216
+ animate: { opacity: 1 },
217
+ exit: { opacity: 0 },
218
+ transition: { duration: 1 }
219
+ };
214
220
  return /* @__PURE__ */ React4.createElement(
215
221
  Comp,
216
222
  {
217
- initial: { opacity: 0 },
218
- animate: { opacity: 1 },
219
- exit: { opacity: 0 },
220
- transition: { duration: 1 },
223
+ ...animationProps,
221
224
  ...rest,
222
225
  ref,
223
226
  className: `react-modal-sheet-backdrop ${className}`,
@@ -516,6 +519,36 @@ function useDimensions() {
516
519
  }, []);
517
520
  return dimensions;
518
521
  }
522
+ function useFocusedInput({
523
+ isEnabled,
524
+ sheetRef
525
+ }) {
526
+ const [focusedInput, setFocusedInput] = useState(null);
527
+ useEffect(() => {
528
+ if (!isEnabled) return;
529
+ const handleFocusIn = (event) => {
530
+ var _a;
531
+ const focusedElement = event.target;
532
+ const isInput = (focusedElement == null ? void 0 : focusedElement.tagName) === "INPUT" || (focusedElement == null ? void 0 : focusedElement.tagName) === "TEXTAREA";
533
+ if (isInput && ((_a = sheetRef.current) == null ? void 0 : _a.contains(focusedElement))) {
534
+ setFocusedInput(focusedElement);
535
+ }
536
+ };
537
+ const handleFocusOut = (event) => {
538
+ const focusedElement = event.target;
539
+ if (focusedElement === focusedInput) {
540
+ setFocusedInput(null);
541
+ }
542
+ };
543
+ document.addEventListener("focusin", handleFocusIn);
544
+ document.addEventListener("focusout", handleFocusOut);
545
+ return () => {
546
+ document.removeEventListener("focusin", handleFocusIn);
547
+ document.removeEventListener("focusout", handleFocusOut);
548
+ };
549
+ }, [isEnabled, focusedInput]);
550
+ return focusedInput;
551
+ }
519
552
  function useSafeAreaInsets() {
520
553
  const [insets] = useState(() => {
521
554
  const fallback = { top: 0, left: 0, right: 0, bottom: 0 };
@@ -1300,29 +1333,6 @@ var Sheet = forwardRef(
1300
1333
  height: sheetHeight,
1301
1334
  snapTo
1302
1335
  }));
1303
- useEffect(() => {
1304
- if (isOpen && snapPoints.length > 0 && avoidKeyboard && keyboard.isKeyboardOpen) {
1305
- const focusedElement = document.activeElement;
1306
- const visibleSheetHeight = yInverted.get() - keyboard.keyboardHeight;
1307
- if (visibleSheetHeight <= 0 && focusedElement) {
1308
- const currentY = y.get();
1309
- const diffY = Math.max(currentY - Math.abs(visibleSheetHeight), 0);
1310
- requestAnimationFrame(() => {
1311
- var _a;
1312
- const inputRect = focusedElement.getBoundingClientRect();
1313
- const sheetRect = (_a = sheetRef.current) == null ? void 0 : _a.getBoundingClientRect();
1314
- let inputOffset = 0;
1315
- if (sheetRect) {
1316
- inputOffset = inputRect.top - sheetRect.top + inputRect.height;
1317
- }
1318
- animate(y, diffY - inputOffset, animationOptions);
1319
- });
1320
- return () => {
1321
- animate(y, currentY, animationOptions);
1322
- };
1323
- }
1324
- }
1325
- }, [isOpen, keyboard.isKeyboardOpen]);
1326
1336
  useModalEffect({
1327
1337
  y,
1328
1338
  detent,
@@ -1334,6 +1344,41 @@ var Sheet = forwardRef(
1334
1344
  usePreventScroll({
1335
1345
  isDisabled: disableScrollLocking || !isOpen
1336
1346
  });
1347
+ const focusedInput = useFocusedInput({
1348
+ isEnabled: isOpen && avoidKeyboard,
1349
+ sheetRef
1350
+ });
1351
+ useEffect(() => {
1352
+ if (!isOpen || !avoidKeyboard) {
1353
+ return;
1354
+ }
1355
+ async function adjustSheetForKeyboard() {
1356
+ const sheetElement = sheetRef.current;
1357
+ if (!sheetElement || !focusedInput || !keyboard.isKeyboardOpen) {
1358
+ return;
1359
+ }
1360
+ const lastSnapPoint = snapPoints[snapPoints.length - 1];
1361
+ if (lastSnapPoint) {
1362
+ await animate(y, lastSnapPoint.snapValueY, animationOptions);
1363
+ updateSnap(lastSnapPoint.snapIndex);
1364
+ }
1365
+ requestAnimationFrame(() => {
1366
+ const inputRect = focusedInput.getBoundingClientRect();
1367
+ const sheetRect = sheetElement.getBoundingClientRect();
1368
+ const scroller = sheetElement.querySelector(
1369
+ ".react-modal-sheet-content-scroller"
1370
+ );
1371
+ const scrollTarget = Math.max(
1372
+ inputRect.top - sheetRect.top + scroller.scrollTop - inputRect.height,
1373
+ 0
1374
+ );
1375
+ requestAnimationFrame(() => {
1376
+ scroller.scrollTo({ top: scrollTarget, behavior: "smooth" });
1377
+ });
1378
+ });
1379
+ }
1380
+ adjustSheetForKeyboard();
1381
+ }, [isOpen, avoidKeyboard, keyboard.isKeyboardOpen, focusedInput]);
1337
1382
  const state = useSheetState({
1338
1383
  isOpen,
1339
1384
  onOpen: async () => {
@@ -1369,6 +1414,7 @@ var Sheet = forwardRef(
1369
1414
  dragProps,
1370
1415
  indicatorRotation,
1371
1416
  avoidKeyboard,
1417
+ prefersReducedMotion,
1372
1418
  sheetBoundsRef,
1373
1419
  sheetRef,
1374
1420
  unstyled,