@samline/drawer 2.0.5 → 2.0.7

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.
@@ -1470,6 +1470,7 @@ function getDragPermission({ targetTagName, hasNoDragAttribute, direction, timeS
1470
1470
  };
1471
1471
  }
1472
1472
 
1473
+ const useSafeLayoutEffect = typeof window === 'undefined' ? React__namespace.default.useEffect : React__namespace.default.useLayoutEffect;
1473
1474
  function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRelease: onReleaseProp, snapPoints, shouldScaleBackground = false, setBackgroundColorOnScale = true, closeThreshold = CLOSE_THRESHOLD, scrollLockTimeout = SCROLL_LOCK_TIMEOUT, dismissible = true, handleOnly = false, fadeFromIndex = snapPoints && snapPoints.length - 1, activeSnapPoint: activeSnapPointProp, setActiveSnapPoint: setActiveSnapPointProp, fixed, modal = true, onClose, nested, noBodyStyles = false, direction = 'bottom', defaultOpen = false, disablePreventScroll = true, snapToSequentialPoint = false, preventScrollRestoration = false, repositionInputs = true, onAnimationEnd, container, autoFocus = false }) {
1474
1475
  var _drawerRef_current, _drawerRef_current1;
1475
1476
  const animationEndTimeoutRef = React__namespace.default.useRef(null);
@@ -1527,6 +1528,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1527
1528
  const drawerHeightRef = React__namespace.default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1528
1529
  const drawerWidthRef = React__namespace.default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1529
1530
  const initialDrawerHeight = React__namespace.default.useRef(0);
1531
+ const releaseHiddenFocusBeforeOpen = React__namespace.default.useCallback(()=>{
1532
+ var _drawerRef_current;
1533
+ if (!modal || autoFocus || typeof document === 'undefined') {
1534
+ return;
1535
+ }
1536
+ const activeElement = document.activeElement;
1537
+ if (!activeElement || activeElement === document.body) {
1538
+ return;
1539
+ }
1540
+ const activeElementNode = activeElement;
1541
+ if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1542
+ return;
1543
+ }
1544
+ if (typeof activeElementNode.blur !== 'function') {
1545
+ return;
1546
+ }
1547
+ activeElementNode.blur();
1548
+ }, [
1549
+ autoFocus,
1550
+ modal
1551
+ ]);
1530
1552
  const onSnapPointChange = React__namespace.default.useCallback((activeSnapPointIndex)=>{
1531
1553
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1532
1554
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1577,23 +1599,14 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1577
1599
  noBodyStyles,
1578
1600
  autoFocus
1579
1601
  });
1580
- React__namespace.default.useEffect(()=>{
1581
- var _drawerRef_current;
1582
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1602
+ useSafeLayoutEffect(()=>{
1603
+ if (!isOpen) {
1583
1604
  return;
1584
1605
  }
1585
- const activeElement = document.activeElement;
1586
- if (!(activeElement instanceof HTMLElement)) {
1587
- return;
1588
- }
1589
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1590
- return;
1591
- }
1592
- activeElement.blur();
1606
+ releaseHiddenFocusBeforeOpen();
1593
1607
  }, [
1594
- autoFocus,
1595
1608
  isOpen,
1596
- modal
1609
+ releaseHiddenFocusBeforeOpen
1597
1610
  ]);
1598
1611
  function getScale() {
1599
1612
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -2002,6 +2015,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
2002
2015
  onOpenChange: (open)=>{
2003
2016
  if (!dismissible && !open) return;
2004
2017
  if (open) {
2018
+ releaseHiddenFocusBeforeOpen();
2005
2019
  setHasBeenOpened(true);
2006
2020
  } else {
2007
2021
  closeDrawer(true);
@@ -1450,6 +1450,7 @@ function getDragPermission({ targetTagName, hasNoDragAttribute, direction, timeS
1450
1450
  };
1451
1451
  }
1452
1452
 
1453
+ const useSafeLayoutEffect = typeof window === 'undefined' ? React__default.useEffect : React__default.useLayoutEffect;
1453
1454
  function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRelease: onReleaseProp, snapPoints, shouldScaleBackground = false, setBackgroundColorOnScale = true, closeThreshold = CLOSE_THRESHOLD, scrollLockTimeout = SCROLL_LOCK_TIMEOUT, dismissible = true, handleOnly = false, fadeFromIndex = snapPoints && snapPoints.length - 1, activeSnapPoint: activeSnapPointProp, setActiveSnapPoint: setActiveSnapPointProp, fixed, modal = true, onClose, nested, noBodyStyles = false, direction = 'bottom', defaultOpen = false, disablePreventScroll = true, snapToSequentialPoint = false, preventScrollRestoration = false, repositionInputs = true, onAnimationEnd, container, autoFocus = false }) {
1454
1455
  var _drawerRef_current, _drawerRef_current1;
1455
1456
  const animationEndTimeoutRef = React__default.useRef(null);
@@ -1507,6 +1508,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1507
1508
  const drawerHeightRef = React__default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1508
1509
  const drawerWidthRef = React__default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1509
1510
  const initialDrawerHeight = React__default.useRef(0);
1511
+ const releaseHiddenFocusBeforeOpen = React__default.useCallback(()=>{
1512
+ var _drawerRef_current;
1513
+ if (!modal || autoFocus || typeof document === 'undefined') {
1514
+ return;
1515
+ }
1516
+ const activeElement = document.activeElement;
1517
+ if (!activeElement || activeElement === document.body) {
1518
+ return;
1519
+ }
1520
+ const activeElementNode = activeElement;
1521
+ if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1522
+ return;
1523
+ }
1524
+ if (typeof activeElementNode.blur !== 'function') {
1525
+ return;
1526
+ }
1527
+ activeElementNode.blur();
1528
+ }, [
1529
+ autoFocus,
1530
+ modal
1531
+ ]);
1510
1532
  const onSnapPointChange = React__default.useCallback((activeSnapPointIndex)=>{
1511
1533
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1512
1534
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1557,23 +1579,14 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1557
1579
  noBodyStyles,
1558
1580
  autoFocus
1559
1581
  });
1560
- React__default.useEffect(()=>{
1561
- var _drawerRef_current;
1562
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1582
+ useSafeLayoutEffect(()=>{
1583
+ if (!isOpen) {
1563
1584
  return;
1564
1585
  }
1565
- const activeElement = document.activeElement;
1566
- if (!(activeElement instanceof HTMLElement)) {
1567
- return;
1568
- }
1569
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1570
- return;
1571
- }
1572
- activeElement.blur();
1586
+ releaseHiddenFocusBeforeOpen();
1573
1587
  }, [
1574
- autoFocus,
1575
1588
  isOpen,
1576
- modal
1589
+ releaseHiddenFocusBeforeOpen
1577
1590
  ]);
1578
1591
  function getScale() {
1579
1592
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -1982,6 +1995,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1982
1995
  onOpenChange: (open)=>{
1983
1996
  if (!dismissible && !open) return;
1984
1997
  if (open) {
1998
+ releaseHiddenFocusBeforeOpen();
1985
1999
  setHasBeenOpened(true);
1986
2000
  } else {
1987
2001
  closeDrawer(true);
@@ -27991,6 +27991,7 @@ function getDragPermission({
27991
27991
  }
27992
27992
 
27993
27993
  // src/react/components.tsx
27994
+ var useSafeLayoutEffect = typeof window === "undefined" ? import_react9.default.useEffect : import_react9.default.useLayoutEffect;
27994
27995
  function Root2({
27995
27996
  open: openProp,
27996
27997
  onOpenChange,
@@ -28076,6 +28077,23 @@ function Root2({
28076
28077
  const drawerHeightRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
28077
28078
  const drawerWidthRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
28078
28079
  const initialDrawerHeight = import_react9.default.useRef(0);
28080
+ const releaseHiddenFocusBeforeOpen2 = import_react9.default.useCallback(() => {
28081
+ if (!modal || autoFocus || typeof document === "undefined") {
28082
+ return;
28083
+ }
28084
+ const activeElement = document.activeElement;
28085
+ if (!activeElement || activeElement === document.body) {
28086
+ return;
28087
+ }
28088
+ const activeElementNode = activeElement;
28089
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28090
+ return;
28091
+ }
28092
+ if (typeof activeElementNode.blur !== "function") {
28093
+ return;
28094
+ }
28095
+ activeElementNode.blur();
28096
+ }, [autoFocus, modal]);
28079
28097
  const onSnapPointChange = import_react9.default.useCallback((activeSnapPointIndex2) => {
28080
28098
  if (snapPoints && activeSnapPointIndex2 === snapPointsOffset.length - 1) openTime.current = /* @__PURE__ */ new Date();
28081
28099
  }, []);
@@ -28134,19 +28152,12 @@ function Root2({
28134
28152
  noBodyStyles,
28135
28153
  autoFocus
28136
28154
  });
28137
- import_react9.default.useEffect(() => {
28138
- if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28139
- return;
28140
- }
28141
- const activeElement = document.activeElement;
28142
- if (!(activeElement instanceof HTMLElement)) {
28143
- return;
28144
- }
28145
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28155
+ useSafeLayoutEffect(() => {
28156
+ if (!isOpen) {
28146
28157
  return;
28147
28158
  }
28148
- activeElement.blur();
28149
- }, [autoFocus, isOpen, modal]);
28159
+ releaseHiddenFocusBeforeOpen2();
28160
+ }, [isOpen, releaseHiddenFocusBeforeOpen2]);
28150
28161
  function getScale() {
28151
28162
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
28152
28163
  }
@@ -28530,6 +28541,7 @@ function Root2({
28530
28541
  onOpenChange: (open) => {
28531
28542
  if (!dismissible && !open) return;
28532
28543
  if (open) {
28544
+ releaseHiddenFocusBeforeOpen2();
28533
28545
  setHasBeenOpened(true);
28534
28546
  } else {
28535
28547
  closeDrawer2(true);
@@ -28883,7 +28895,7 @@ var VISUALLY_HIDDEN_STYLE = {
28883
28895
  whiteSpace: "nowrap",
28884
28896
  border: 0
28885
28897
  };
28886
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28898
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28887
28899
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28888
28900
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28889
28901
  const baseProps = {
@@ -28917,7 +28929,7 @@ function VanillaNode({
28917
28929
  dataAttribute
28918
28930
  }) {
28919
28931
  const ref = import_react10.default.useRef(null);
28920
- useSafeLayoutEffect(() => {
28932
+ useSafeLayoutEffect2(() => {
28921
28933
  const element = ref.current;
28922
28934
  if (!element) {
28923
28935
  return;
@@ -29123,6 +29135,7 @@ function openAncestorChain(parentId) {
29123
29135
  openAncestorChain(nextParentId);
29124
29136
  }
29125
29137
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29138
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29126
29139
  parentRuntime.controller.setOpen(true);
29127
29140
  notifyOpenStateChange(parentRuntime, true);
29128
29141
  renderVanillaDrawer(parentRuntime.id);
@@ -29142,6 +29155,7 @@ function bindTriggerElement(runtime) {
29142
29155
  }
29143
29156
  const triggerElement = runtime.options.triggerElement;
29144
29157
  const handleClick = () => {
29158
+ releaseHiddenFocusBeforeOpen(runtime.options);
29145
29159
  runtime.controller.setOpen(true);
29146
29160
  renderVanillaDrawer(runtime.id);
29147
29161
  };
@@ -29177,6 +29191,16 @@ function notifyOpenStateChange(runtime, open) {
29177
29191
  function canUseDOM3() {
29178
29192
  return typeof window !== "undefined" && typeof document !== "undefined";
29179
29193
  }
29194
+ function releaseHiddenFocusBeforeOpen(options) {
29195
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29196
+ return;
29197
+ }
29198
+ const activeElement = document.activeElement;
29199
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29200
+ return;
29201
+ }
29202
+ activeElement.blur();
29203
+ }
29180
29204
  function getRuntimeDrawerElement(runtime) {
29181
29205
  if (!runtime.element) {
29182
29206
  return null;
@@ -29230,6 +29254,9 @@ function renderVanillaDrawer(id) {
29230
29254
  open: snapshot.state.isOpen,
29231
29255
  onOpenChange: (open) => {
29232
29256
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29257
+ if (open) {
29258
+ releaseHiddenFocusBeforeOpen(runtime.options);
29259
+ }
29233
29260
  runtime.controller.setOpen(open);
29234
29261
  if (previousOpen !== open) {
29235
29262
  notifyOpenStateChange(runtime, open);
@@ -29276,6 +29303,9 @@ function buildVanillaController(id) {
29276
29303
  if (!runtime) {
29277
29304
  return createDrawer({ id, open }).getSnapshot();
29278
29305
  }
29306
+ if (open) {
29307
+ releaseHiddenFocusBeforeOpen(runtime.options);
29308
+ }
29279
29309
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29280
29310
  const snapshot = runtime.controller.setOpen(open);
29281
29311
  if (previousOpen !== open) {
@@ -29346,6 +29376,9 @@ function createDrawer(options = {}) {
29346
29376
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29347
29377
  }
29348
29378
  }
29379
+ if (nextOptions.open && !previousOpen) {
29380
+ releaseHiddenFocusBeforeOpen(nextOptions);
29381
+ }
29349
29382
  renderVanillaDrawer(id);
29350
29383
  if (nextOptions.parentId && nextOptions.open) {
29351
29384
  openAncestorChain(nextOptions.parentId);
@@ -27963,6 +27963,7 @@ function getDragPermission({
27963
27963
  }
27964
27964
 
27965
27965
  // src/react/components.tsx
27966
+ var useSafeLayoutEffect = typeof window === "undefined" ? import_react9.default.useEffect : import_react9.default.useLayoutEffect;
27966
27967
  function Root2({
27967
27968
  open: openProp,
27968
27969
  onOpenChange,
@@ -28048,6 +28049,23 @@ function Root2({
28048
28049
  const drawerHeightRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
28049
28050
  const drawerWidthRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
28050
28051
  const initialDrawerHeight = import_react9.default.useRef(0);
28052
+ const releaseHiddenFocusBeforeOpen2 = import_react9.default.useCallback(() => {
28053
+ if (!modal || autoFocus || typeof document === "undefined") {
28054
+ return;
28055
+ }
28056
+ const activeElement = document.activeElement;
28057
+ if (!activeElement || activeElement === document.body) {
28058
+ return;
28059
+ }
28060
+ const activeElementNode = activeElement;
28061
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28062
+ return;
28063
+ }
28064
+ if (typeof activeElementNode.blur !== "function") {
28065
+ return;
28066
+ }
28067
+ activeElementNode.blur();
28068
+ }, [autoFocus, modal]);
28051
28069
  const onSnapPointChange = import_react9.default.useCallback((activeSnapPointIndex2) => {
28052
28070
  if (snapPoints && activeSnapPointIndex2 === snapPointsOffset.length - 1) openTime.current = /* @__PURE__ */ new Date();
28053
28071
  }, []);
@@ -28106,19 +28124,12 @@ function Root2({
28106
28124
  noBodyStyles,
28107
28125
  autoFocus
28108
28126
  });
28109
- import_react9.default.useEffect(() => {
28110
- if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28111
- return;
28112
- }
28113
- const activeElement = document.activeElement;
28114
- if (!(activeElement instanceof HTMLElement)) {
28115
- return;
28116
- }
28117
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28127
+ useSafeLayoutEffect(() => {
28128
+ if (!isOpen) {
28118
28129
  return;
28119
28130
  }
28120
- activeElement.blur();
28121
- }, [autoFocus, isOpen, modal]);
28131
+ releaseHiddenFocusBeforeOpen2();
28132
+ }, [isOpen, releaseHiddenFocusBeforeOpen2]);
28122
28133
  function getScale() {
28123
28134
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
28124
28135
  }
@@ -28502,6 +28513,7 @@ function Root2({
28502
28513
  onOpenChange: (open) => {
28503
28514
  if (!dismissible && !open) return;
28504
28515
  if (open) {
28516
+ releaseHiddenFocusBeforeOpen2();
28505
28517
  setHasBeenOpened(true);
28506
28518
  } else {
28507
28519
  closeDrawer2(true);
@@ -28855,7 +28867,7 @@ var VISUALLY_HIDDEN_STYLE = {
28855
28867
  whiteSpace: "nowrap",
28856
28868
  border: 0
28857
28869
  };
28858
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28870
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28859
28871
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28860
28872
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28861
28873
  const baseProps = {
@@ -28889,7 +28901,7 @@ function VanillaNode({
28889
28901
  dataAttribute
28890
28902
  }) {
28891
28903
  const ref = import_react10.default.useRef(null);
28892
- useSafeLayoutEffect(() => {
28904
+ useSafeLayoutEffect2(() => {
28893
28905
  const element = ref.current;
28894
28906
  if (!element) {
28895
28907
  return;
@@ -29095,6 +29107,7 @@ function openAncestorChain(parentId) {
29095
29107
  openAncestorChain(nextParentId);
29096
29108
  }
29097
29109
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29110
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29098
29111
  parentRuntime.controller.setOpen(true);
29099
29112
  notifyOpenStateChange(parentRuntime, true);
29100
29113
  renderVanillaDrawer(parentRuntime.id);
@@ -29114,6 +29127,7 @@ function bindTriggerElement(runtime) {
29114
29127
  }
29115
29128
  const triggerElement = runtime.options.triggerElement;
29116
29129
  const handleClick = () => {
29130
+ releaseHiddenFocusBeforeOpen(runtime.options);
29117
29131
  runtime.controller.setOpen(true);
29118
29132
  renderVanillaDrawer(runtime.id);
29119
29133
  };
@@ -29149,6 +29163,16 @@ function notifyOpenStateChange(runtime, open) {
29149
29163
  function canUseDOM3() {
29150
29164
  return typeof window !== "undefined" && typeof document !== "undefined";
29151
29165
  }
29166
+ function releaseHiddenFocusBeforeOpen(options) {
29167
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29168
+ return;
29169
+ }
29170
+ const activeElement = document.activeElement;
29171
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29172
+ return;
29173
+ }
29174
+ activeElement.blur();
29175
+ }
29152
29176
  function getRuntimeDrawerElement(runtime) {
29153
29177
  if (!runtime.element) {
29154
29178
  return null;
@@ -29202,6 +29226,9 @@ function renderVanillaDrawer(id) {
29202
29226
  open: snapshot.state.isOpen,
29203
29227
  onOpenChange: (open) => {
29204
29228
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29229
+ if (open) {
29230
+ releaseHiddenFocusBeforeOpen(runtime.options);
29231
+ }
29205
29232
  runtime.controller.setOpen(open);
29206
29233
  if (previousOpen !== open) {
29207
29234
  notifyOpenStateChange(runtime, open);
@@ -29248,6 +29275,9 @@ function buildVanillaController(id) {
29248
29275
  if (!runtime) {
29249
29276
  return createDrawer({ id, open }).getSnapshot();
29250
29277
  }
29278
+ if (open) {
29279
+ releaseHiddenFocusBeforeOpen(runtime.options);
29280
+ }
29251
29281
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29252
29282
  const snapshot = runtime.controller.setOpen(open);
29253
29283
  if (previousOpen !== open) {
@@ -29318,6 +29348,9 @@ function createDrawer(options = {}) {
29318
29348
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29319
29349
  }
29320
29350
  }
29351
+ if (nextOptions.open && !previousOpen) {
29352
+ releaseHiddenFocusBeforeOpen(nextOptions);
29353
+ }
29321
29354
  renderVanillaDrawer(id);
29322
29355
  if (nextOptions.parentId && nextOptions.open) {
29323
29356
  openAncestorChain(nextOptions.parentId);
@@ -1470,6 +1470,7 @@ function getDragPermission({ targetTagName, hasNoDragAttribute, direction, timeS
1470
1470
  };
1471
1471
  }
1472
1472
 
1473
+ const useSafeLayoutEffect = typeof window === 'undefined' ? React__namespace.default.useEffect : React__namespace.default.useLayoutEffect;
1473
1474
  function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRelease: onReleaseProp, snapPoints, shouldScaleBackground = false, setBackgroundColorOnScale = true, closeThreshold = CLOSE_THRESHOLD, scrollLockTimeout = SCROLL_LOCK_TIMEOUT, dismissible = true, handleOnly = false, fadeFromIndex = snapPoints && snapPoints.length - 1, activeSnapPoint: activeSnapPointProp, setActiveSnapPoint: setActiveSnapPointProp, fixed, modal = true, onClose, nested, noBodyStyles = false, direction = 'bottom', defaultOpen = false, disablePreventScroll = true, snapToSequentialPoint = false, preventScrollRestoration = false, repositionInputs = true, onAnimationEnd, container, autoFocus = false }) {
1474
1475
  var _drawerRef_current, _drawerRef_current1;
1475
1476
  const animationEndTimeoutRef = React__namespace.default.useRef(null);
@@ -1527,6 +1528,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1527
1528
  const drawerHeightRef = React__namespace.default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1528
1529
  const drawerWidthRef = React__namespace.default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1529
1530
  const initialDrawerHeight = React__namespace.default.useRef(0);
1531
+ const releaseHiddenFocusBeforeOpen = React__namespace.default.useCallback(()=>{
1532
+ var _drawerRef_current;
1533
+ if (!modal || autoFocus || typeof document === 'undefined') {
1534
+ return;
1535
+ }
1536
+ const activeElement = document.activeElement;
1537
+ if (!activeElement || activeElement === document.body) {
1538
+ return;
1539
+ }
1540
+ const activeElementNode = activeElement;
1541
+ if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1542
+ return;
1543
+ }
1544
+ if (typeof activeElementNode.blur !== 'function') {
1545
+ return;
1546
+ }
1547
+ activeElementNode.blur();
1548
+ }, [
1549
+ autoFocus,
1550
+ modal
1551
+ ]);
1530
1552
  const onSnapPointChange = React__namespace.default.useCallback((activeSnapPointIndex)=>{
1531
1553
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1532
1554
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1577,23 +1599,14 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1577
1599
  noBodyStyles,
1578
1600
  autoFocus
1579
1601
  });
1580
- React__namespace.default.useEffect(()=>{
1581
- var _drawerRef_current;
1582
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1602
+ useSafeLayoutEffect(()=>{
1603
+ if (!isOpen) {
1583
1604
  return;
1584
1605
  }
1585
- const activeElement = document.activeElement;
1586
- if (!(activeElement instanceof HTMLElement)) {
1587
- return;
1588
- }
1589
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1590
- return;
1591
- }
1592
- activeElement.blur();
1606
+ releaseHiddenFocusBeforeOpen();
1593
1607
  }, [
1594
- autoFocus,
1595
1608
  isOpen,
1596
- modal
1609
+ releaseHiddenFocusBeforeOpen
1597
1610
  ]);
1598
1611
  function getScale() {
1599
1612
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -2002,6 +2015,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
2002
2015
  onOpenChange: (open)=>{
2003
2016
  if (!dismissible && !open) return;
2004
2017
  if (open) {
2018
+ releaseHiddenFocusBeforeOpen();
2005
2019
  setHasBeenOpened(true);
2006
2020
  } else {
2007
2021
  closeDrawer(true);
@@ -1450,6 +1450,7 @@ function getDragPermission({ targetTagName, hasNoDragAttribute, direction, timeS
1450
1450
  };
1451
1451
  }
1452
1452
 
1453
+ const useSafeLayoutEffect = typeof window === 'undefined' ? React__default.useEffect : React__default.useLayoutEffect;
1453
1454
  function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRelease: onReleaseProp, snapPoints, shouldScaleBackground = false, setBackgroundColorOnScale = true, closeThreshold = CLOSE_THRESHOLD, scrollLockTimeout = SCROLL_LOCK_TIMEOUT, dismissible = true, handleOnly = false, fadeFromIndex = snapPoints && snapPoints.length - 1, activeSnapPoint: activeSnapPointProp, setActiveSnapPoint: setActiveSnapPointProp, fixed, modal = true, onClose, nested, noBodyStyles = false, direction = 'bottom', defaultOpen = false, disablePreventScroll = true, snapToSequentialPoint = false, preventScrollRestoration = false, repositionInputs = true, onAnimationEnd, container, autoFocus = false }) {
1454
1455
  var _drawerRef_current, _drawerRef_current1;
1455
1456
  const animationEndTimeoutRef = React__default.useRef(null);
@@ -1507,6 +1508,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1507
1508
  const drawerHeightRef = React__default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1508
1509
  const drawerWidthRef = React__default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1509
1510
  const initialDrawerHeight = React__default.useRef(0);
1511
+ const releaseHiddenFocusBeforeOpen = React__default.useCallback(()=>{
1512
+ var _drawerRef_current;
1513
+ if (!modal || autoFocus || typeof document === 'undefined') {
1514
+ return;
1515
+ }
1516
+ const activeElement = document.activeElement;
1517
+ if (!activeElement || activeElement === document.body) {
1518
+ return;
1519
+ }
1520
+ const activeElementNode = activeElement;
1521
+ if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1522
+ return;
1523
+ }
1524
+ if (typeof activeElementNode.blur !== 'function') {
1525
+ return;
1526
+ }
1527
+ activeElementNode.blur();
1528
+ }, [
1529
+ autoFocus,
1530
+ modal
1531
+ ]);
1510
1532
  const onSnapPointChange = React__default.useCallback((activeSnapPointIndex)=>{
1511
1533
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1512
1534
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1557,23 +1579,14 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1557
1579
  noBodyStyles,
1558
1580
  autoFocus
1559
1581
  });
1560
- React__default.useEffect(()=>{
1561
- var _drawerRef_current;
1562
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1582
+ useSafeLayoutEffect(()=>{
1583
+ if (!isOpen) {
1563
1584
  return;
1564
1585
  }
1565
- const activeElement = document.activeElement;
1566
- if (!(activeElement instanceof HTMLElement)) {
1567
- return;
1568
- }
1569
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1570
- return;
1571
- }
1572
- activeElement.blur();
1586
+ releaseHiddenFocusBeforeOpen();
1573
1587
  }, [
1574
- autoFocus,
1575
1588
  isOpen,
1576
- modal
1589
+ releaseHiddenFocusBeforeOpen
1577
1590
  ]);
1578
1591
  function getScale() {
1579
1592
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -1982,6 +1995,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1982
1995
  onOpenChange: (open)=>{
1983
1996
  if (!dismissible && !open) return;
1984
1997
  if (open) {
1998
+ releaseHiddenFocusBeforeOpen();
1985
1999
  setHasBeenOpened(true);
1986
2000
  } else {
1987
2001
  closeDrawer(true);