@samline/drawer 2.0.4 → 2.0.5

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.
@@ -1105,7 +1105,7 @@ var require_react_development = __commonJS({
1105
1105
  var dispatcher = resolveDispatcher();
1106
1106
  return dispatcher.useReducer(reducer, initialArg, init);
1107
1107
  }
1108
- function useRef10(initialValue) {
1108
+ function useRef11(initialValue) {
1109
1109
  var dispatcher = resolveDispatcher();
1110
1110
  return dispatcher.useRef(initialValue);
1111
1111
  }
@@ -1899,7 +1899,7 @@ var require_react_development = __commonJS({
1899
1899
  exports.useLayoutEffect = useLayoutEffect5;
1900
1900
  exports.useMemo = useMemo6;
1901
1901
  exports.useReducer = useReducer3;
1902
- exports.useRef = useRef10;
1902
+ exports.useRef = useRef11;
1903
1903
  exports.useState = useState10;
1904
1904
  exports.useSyncExternalStore = useSyncExternalStore;
1905
1905
  exports.useTransition = useTransition;
@@ -26988,25 +26988,34 @@ var nonTextInputTypes = /* @__PURE__ */ new Set([
26988
26988
  "submit",
26989
26989
  "reset"
26990
26990
  ]);
26991
- var preventScrollCount = 0;
26992
- var restore;
26991
+ var activePreventScrollLocks = /* @__PURE__ */ new Set();
26992
+ var activePreventScrollRestore = null;
26993
+ function acquirePreventScrollLock(lockId) {
26994
+ activePreventScrollLocks.add(lockId);
26995
+ if (activePreventScrollLocks.size === 1 && isIOS()) {
26996
+ activePreventScrollRestore = preventScrollMobileSafari();
26997
+ }
26998
+ }
26999
+ function releasePreventScrollLock(lockId) {
27000
+ activePreventScrollLocks.delete(lockId);
27001
+ if (activePreventScrollLocks.size === 0) {
27002
+ activePreventScrollRestore?.();
27003
+ activePreventScrollRestore = null;
27004
+ }
27005
+ }
26993
27006
  function usePreventScroll(options = {}) {
26994
27007
  let { isDisabled } = options;
27008
+ const lockIdRef = (0, import_react3.useRef)();
27009
+ if (!lockIdRef.current) {
27010
+ lockIdRef.current = /* @__PURE__ */ Symbol("drawer-prevent-scroll-lock");
27011
+ }
26995
27012
  useIsomorphicLayoutEffect2(() => {
26996
27013
  if (isDisabled) {
26997
27014
  return;
26998
27015
  }
26999
- preventScrollCount++;
27000
- if (preventScrollCount === 1) {
27001
- if (isIOS()) {
27002
- restore = preventScrollMobileSafari();
27003
- }
27004
- }
27016
+ acquirePreventScrollLock(lockIdRef.current);
27005
27017
  return () => {
27006
- preventScrollCount--;
27007
- if (preventScrollCount === 0) {
27008
- restore?.();
27009
- }
27018
+ releasePreventScrollLock(lockIdRef.current);
27010
27019
  };
27011
27020
  }, [isDisabled]);
27012
27021
  }
@@ -27580,12 +27589,23 @@ function useScaleBackground() {
27580
27589
  const { direction, isOpen, shouldScaleBackground, setBackgroundColorOnScale, noBodyStyles } = useDrawerContext();
27581
27590
  const timeoutIdRef = import_react6.default.useRef(null);
27582
27591
  const initialBackgroundColor = (0, import_react6.useMemo)(() => document.body.style.backgroundColor, []);
27592
+ import_react6.default.useEffect(() => {
27593
+ return () => {
27594
+ if (timeoutIdRef.current !== null) {
27595
+ window.clearTimeout(timeoutIdRef.current);
27596
+ timeoutIdRef.current = null;
27597
+ }
27598
+ };
27599
+ }, []);
27583
27600
  function getScale() {
27584
27601
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
27585
27602
  }
27586
27603
  import_react6.default.useEffect(() => {
27587
27604
  if (isOpen && shouldScaleBackground) {
27588
- if (timeoutIdRef.current) clearTimeout(timeoutIdRef.current);
27605
+ if (timeoutIdRef.current !== null) {
27606
+ clearTimeout(timeoutIdRef.current);
27607
+ timeoutIdRef.current = null;
27608
+ }
27589
27609
  const wrapper = document.querySelector("[data-drawer-wrapper]");
27590
27610
  if (!wrapper) return;
27591
27611
  chain(
@@ -27614,6 +27634,7 @@ function useScaleBackground() {
27614
27634
  } else {
27615
27635
  document.body.style.removeProperty("background");
27616
27636
  }
27637
+ timeoutIdRef.current = null;
27617
27638
  }, TRANSITIONS.DURATION * 1e3);
27618
27639
  };
27619
27640
  }
@@ -27623,6 +27644,14 @@ function useScaleBackground() {
27623
27644
  // src/use-position-fixed.ts
27624
27645
  var import_react7 = __toESM(require_react());
27625
27646
  var previousBodyPosition = null;
27647
+ var activeBodyPositionLocks = /* @__PURE__ */ new Set();
27648
+ var bodyPositionTimeoutId = null;
27649
+ function clearBodyPositionTimeout() {
27650
+ if (bodyPositionTimeoutId !== null) {
27651
+ window.clearTimeout(bodyPositionTimeoutId);
27652
+ bodyPositionTimeoutId = null;
27653
+ }
27654
+ }
27626
27655
  function usePositionFixed({
27627
27656
  isOpen,
27628
27657
  modal,
@@ -27633,8 +27662,17 @@ function usePositionFixed({
27633
27662
  }) {
27634
27663
  const [activeUrl, setActiveUrl] = import_react7.default.useState(() => typeof window !== "undefined" ? window.location.href : "");
27635
27664
  const scrollPos = import_react7.default.useRef(0);
27665
+ const lockIdRef = import_react7.default.useRef();
27666
+ if (!lockIdRef.current) {
27667
+ lockIdRef.current = /* @__PURE__ */ Symbol("drawer-body-position-lock");
27668
+ }
27636
27669
  const setPositionFixed = import_react7.default.useCallback(() => {
27637
27670
  if (!isSafari()) return;
27671
+ const lockId = lockIdRef.current;
27672
+ if (activeBodyPositionLocks.has(lockId)) {
27673
+ return;
27674
+ }
27675
+ activeBodyPositionLocks.add(lockId);
27638
27676
  if (previousBodyPosition === null && isOpen && !noBodyStyles) {
27639
27677
  previousBodyPosition = {
27640
27678
  position: document.body.style.position,
@@ -27651,7 +27689,8 @@ function usePositionFixed({
27651
27689
  right: "0px",
27652
27690
  height: "auto"
27653
27691
  });
27654
- window.setTimeout(
27692
+ clearBodyPositionTimeout();
27693
+ bodyPositionTimeoutId = window.setTimeout(
27655
27694
  () => window.requestAnimationFrame(() => {
27656
27695
  const bottomBarHeight = innerHeight - window.innerHeight;
27657
27696
  if (bottomBarHeight && scrollPos.current >= innerHeight) {
@@ -27661,10 +27700,16 @@ function usePositionFixed({
27661
27700
  300
27662
27701
  );
27663
27702
  }
27664
- }, [isOpen]);
27703
+ }, [isOpen, noBodyStyles]);
27665
27704
  const restorePositionSetting = import_react7.default.useCallback(() => {
27666
27705
  if (!isSafari()) return;
27706
+ const lockId = lockIdRef.current;
27707
+ activeBodyPositionLocks.delete(lockId);
27708
+ if (activeBodyPositionLocks.size > 0) {
27709
+ return;
27710
+ }
27667
27711
  if (previousBodyPosition !== null && !noBodyStyles) {
27712
+ clearBodyPositionTimeout();
27668
27713
  const y = -parseInt(document.body.style.top, 10);
27669
27714
  const x = -parseInt(document.body.style.left, 10);
27670
27715
  Object.assign(document.body.style, previousBodyPosition);
@@ -27677,7 +27722,7 @@ function usePositionFixed({
27677
27722
  });
27678
27723
  previousBodyPosition = null;
27679
27724
  }
27680
- }, [activeUrl]);
27725
+ }, [activeUrl, noBodyStyles, setActiveUrl]);
27681
27726
  import_react7.default.useEffect(() => {
27682
27727
  function onScroll() {
27683
27728
  scrollPos.current = window.scrollY;
@@ -27975,6 +28020,12 @@ function Root2({
27975
28020
  container,
27976
28021
  autoFocus = false
27977
28022
  }) {
28023
+ const animationEndTimeoutRef = import_react9.default.useRef(null);
28024
+ const nonModalPointerEventsRafRef = import_react9.default.useRef(null);
28025
+ const shouldAnimateRafRef = import_react9.default.useRef(null);
28026
+ const snapPointsResetTimeoutRef = import_react9.default.useRef(null);
28027
+ const justReleasedTimeoutRef = import_react9.default.useRef(null);
28028
+ const touchEndHandlerRef = import_react9.default.useRef(null);
27978
28029
  const [isOpen = false, setIsOpen] = useControllableState2({
27979
28030
  defaultProp: defaultOpen,
27980
28031
  prop: openProp,
@@ -27983,13 +28034,20 @@ function Root2({
27983
28034
  if (!o && !nested) {
27984
28035
  restorePositionSetting();
27985
28036
  }
27986
- setTimeout(() => {
28037
+ if (animationEndTimeoutRef.current !== null) {
28038
+ window.clearTimeout(animationEndTimeoutRef.current);
28039
+ }
28040
+ animationEndTimeoutRef.current = window.setTimeout(() => {
27987
28041
  onAnimationEnd?.(o);
27988
28042
  }, TRANSITIONS.DURATION * 1e3);
27989
28043
  if (o && !modal) {
27990
28044
  if (typeof window !== "undefined") {
27991
- window.requestAnimationFrame(() => {
28045
+ if (nonModalPointerEventsRafRef.current !== null) {
28046
+ window.cancelAnimationFrame(nonModalPointerEventsRafRef.current);
28047
+ }
28048
+ nonModalPointerEventsRafRef.current = window.requestAnimationFrame(() => {
27992
28049
  document.body.style.pointerEvents = "auto";
28050
+ nonModalPointerEventsRafRef.current = null;
27993
28051
  });
27994
28052
  }
27995
28053
  }
@@ -28098,7 +28156,15 @@ function Root2({
28098
28156
  setIsDragging(true);
28099
28157
  dragStartTime.current = /* @__PURE__ */ new Date();
28100
28158
  if (isIOS()) {
28101
- window.addEventListener("touchend", () => isAllowedToDrag.current = false, { once: true });
28159
+ if (touchEndHandlerRef.current) {
28160
+ window.removeEventListener("touchend", touchEndHandlerRef.current);
28161
+ }
28162
+ const handleTouchEnd = () => {
28163
+ isAllowedToDrag.current = false;
28164
+ touchEndHandlerRef.current = null;
28165
+ };
28166
+ touchEndHandlerRef.current = handleTouchEnd;
28167
+ window.addEventListener("touchend", handleTouchEnd, { once: true });
28102
28168
  }
28103
28169
  event.target.setPointerCapture(event.pointerId);
28104
28170
  pointerStart.current = isVertical(direction) ? event.pageY : event.pageX;
@@ -28205,9 +28271,15 @@ function Root2({
28205
28271
  }
28206
28272
  }
28207
28273
  import_react9.default.useEffect(() => {
28208
- window.requestAnimationFrame(() => {
28274
+ shouldAnimateRafRef.current = window.requestAnimationFrame(() => {
28209
28275
  shouldAnimate.current = true;
28210
28276
  });
28277
+ return () => {
28278
+ if (shouldAnimateRafRef.current !== null) {
28279
+ window.cancelAnimationFrame(shouldAnimateRafRef.current);
28280
+ shouldAnimateRafRef.current = null;
28281
+ }
28282
+ };
28211
28283
  }, []);
28212
28284
  import_react9.default.useEffect(() => {
28213
28285
  function onVisualViewportChange() {
@@ -28248,10 +28320,14 @@ function Root2({
28248
28320
  if (!fromWithin) {
28249
28321
  setIsOpen(false);
28250
28322
  }
28251
- setTimeout(() => {
28323
+ if (snapPointsResetTimeoutRef.current !== null) {
28324
+ window.clearTimeout(snapPointsResetTimeoutRef.current);
28325
+ }
28326
+ snapPointsResetTimeoutRef.current = window.setTimeout(() => {
28252
28327
  if (snapPoints) {
28253
28328
  setActiveSnapPoint(snapPoints[0]);
28254
28329
  }
28330
+ snapPointsResetTimeoutRef.current = null;
28255
28331
  }, TRANSITIONS.DURATION * 1e3);
28256
28332
  }
28257
28333
  function resetDrawer() {
@@ -28326,8 +28402,12 @@ function Root2({
28326
28402
  });
28327
28403
  if (releaseResult.shouldPreventFocus) {
28328
28404
  setJustReleased(true);
28329
- setTimeout(() => {
28405
+ if (justReleasedTimeoutRef.current !== null) {
28406
+ window.clearTimeout(justReleasedTimeoutRef.current);
28407
+ }
28408
+ justReleasedTimeoutRef.current = window.setTimeout(() => {
28330
28409
  setJustReleased(false);
28410
+ justReleasedTimeoutRef.current = null;
28331
28411
  }, 200);
28332
28412
  }
28333
28413
  if (releaseResult.action === "close") {
@@ -28401,11 +28481,44 @@ function Root2({
28401
28481
  });
28402
28482
  }
28403
28483
  }
28484
+ import_react9.default.useEffect(() => {
28485
+ return () => {
28486
+ if (animationEndTimeoutRef.current !== null) {
28487
+ window.clearTimeout(animationEndTimeoutRef.current);
28488
+ }
28489
+ if (nonModalPointerEventsRafRef.current !== null) {
28490
+ window.cancelAnimationFrame(nonModalPointerEventsRafRef.current);
28491
+ }
28492
+ if (snapPointsResetTimeoutRef.current !== null) {
28493
+ window.clearTimeout(snapPointsResetTimeoutRef.current);
28494
+ }
28495
+ if (justReleasedTimeoutRef.current !== null) {
28496
+ window.clearTimeout(justReleasedTimeoutRef.current);
28497
+ }
28498
+ if (nestedOpenChangeTimer.current) {
28499
+ window.clearTimeout(nestedOpenChangeTimer.current);
28500
+ }
28501
+ if (touchEndHandlerRef.current) {
28502
+ window.removeEventListener("touchend", touchEndHandlerRef.current);
28503
+ touchEndHandlerRef.current = null;
28504
+ }
28505
+ };
28506
+ }, []);
28404
28507
  import_react9.default.useEffect(() => {
28405
28508
  if (!modal) {
28406
- window.requestAnimationFrame(() => {
28509
+ if (nonModalPointerEventsRafRef.current !== null) {
28510
+ window.cancelAnimationFrame(nonModalPointerEventsRafRef.current);
28511
+ }
28512
+ nonModalPointerEventsRafRef.current = window.requestAnimationFrame(() => {
28407
28513
  document.body.style.pointerEvents = "auto";
28514
+ nonModalPointerEventsRafRef.current = null;
28408
28515
  });
28516
+ return () => {
28517
+ if (nonModalPointerEventsRafRef.current !== null) {
28518
+ window.cancelAnimationFrame(nonModalPointerEventsRafRef.current);
28519
+ nonModalPointerEventsRafRef.current = null;
28520
+ }
28521
+ };
28409
28522
  }
28410
28523
  }, [modal]);
28411
28524
  return /* @__PURE__ */ import_react9.default.createElement(
@@ -28512,15 +28625,23 @@ var Content2 = import_react9.default.forwardRef(function({ onPointerDownOutside,
28512
28625
  const pointerStartRef = import_react9.default.useRef(null);
28513
28626
  const lastKnownPointerEventRef = import_react9.default.useRef(null);
28514
28627
  const wasBeyondThePointRef = import_react9.default.useRef(false);
28628
+ const delayedSnapPointsRafRef = import_react9.default.useRef(null);
28515
28629
  const hasSnapPoints = snapPoints && snapPoints.length > 0;
28516
28630
  useScaleBackground();
28517
28631
  import_react9.default.useEffect(() => {
28518
28632
  if (hasSnapPoints) {
28519
- window.requestAnimationFrame(() => {
28633
+ delayedSnapPointsRafRef.current = window.requestAnimationFrame(() => {
28520
28634
  setDelayedSnapPoints(true);
28635
+ delayedSnapPointsRafRef.current = null;
28521
28636
  });
28522
28637
  }
28523
- }, []);
28638
+ return () => {
28639
+ if (delayedSnapPointsRafRef.current !== null) {
28640
+ window.cancelAnimationFrame(delayedSnapPointsRafRef.current);
28641
+ delayedSnapPointsRafRef.current = null;
28642
+ }
28643
+ };
28644
+ }, [hasSnapPoints]);
28524
28645
  function handleOnPointerUp(event) {
28525
28646
  pointerStartRef.current = null;
28526
28647
  wasBeyondThePointRef.current = false;
@@ -28629,13 +28750,14 @@ var Handle = import_react9.default.forwardRef(function({ preventCycle = false, c
28629
28750
  onDrag
28630
28751
  } = useDrawerContext();
28631
28752
  const closeTimeoutIdRef = import_react9.default.useRef(null);
28753
+ const cycleTimeoutIdRef = import_react9.default.useRef(null);
28632
28754
  const shouldCancelInteractionRef = import_react9.default.useRef(false);
28633
28755
  function handleStartCycle() {
28634
28756
  if (shouldCancelInteractionRef.current) {
28635
28757
  handleCancelInteraction();
28636
28758
  return;
28637
28759
  }
28638
- window.setTimeout(() => {
28760
+ cycleTimeoutIdRef.current = window.setTimeout(() => {
28639
28761
  handleCycleSnapPoints();
28640
28762
  }, DOUBLE_TAP_TIMEOUT);
28641
28763
  }
@@ -28666,9 +28788,19 @@ var Handle = import_react9.default.forwardRef(function({ preventCycle = false, c
28666
28788
  function handleCancelInteraction() {
28667
28789
  if (closeTimeoutIdRef.current) {
28668
28790
  window.clearTimeout(closeTimeoutIdRef.current);
28791
+ closeTimeoutIdRef.current = null;
28792
+ }
28793
+ if (cycleTimeoutIdRef.current) {
28794
+ window.clearTimeout(cycleTimeoutIdRef.current);
28795
+ cycleTimeoutIdRef.current = null;
28669
28796
  }
28670
28797
  shouldCancelInteractionRef.current = false;
28671
28798
  }
28799
+ import_react9.default.useEffect(() => {
28800
+ return () => {
28801
+ handleCancelInteraction();
28802
+ };
28803
+ }, []);
28672
28804
  return /* @__PURE__ */ import_react9.default.createElement(
28673
28805
  "div",
28674
28806
  {