@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.
@@ -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);
package/dist/index.js CHANGED
@@ -27988,6 +27988,7 @@ function getDragPermission({
27988
27988
  }
27989
27989
 
27990
27990
  // src/react/components.tsx
27991
+ var useSafeLayoutEffect = typeof window === "undefined" ? import_react9.default.useEffect : import_react9.default.useLayoutEffect;
27991
27992
  function Root2({
27992
27993
  open: openProp,
27993
27994
  onOpenChange,
@@ -28073,6 +28074,23 @@ function Root2({
28073
28074
  const drawerHeightRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
28074
28075
  const drawerWidthRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
28075
28076
  const initialDrawerHeight = import_react9.default.useRef(0);
28077
+ const releaseHiddenFocusBeforeOpen2 = import_react9.default.useCallback(() => {
28078
+ if (!modal || autoFocus || typeof document === "undefined") {
28079
+ return;
28080
+ }
28081
+ const activeElement = document.activeElement;
28082
+ if (!activeElement || activeElement === document.body) {
28083
+ return;
28084
+ }
28085
+ const activeElementNode = activeElement;
28086
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28087
+ return;
28088
+ }
28089
+ if (typeof activeElementNode.blur !== "function") {
28090
+ return;
28091
+ }
28092
+ activeElementNode.blur();
28093
+ }, [autoFocus, modal]);
28076
28094
  const onSnapPointChange = import_react9.default.useCallback((activeSnapPointIndex2) => {
28077
28095
  if (snapPoints && activeSnapPointIndex2 === snapPointsOffset.length - 1) openTime.current = /* @__PURE__ */ new Date();
28078
28096
  }, []);
@@ -28131,19 +28149,12 @@ function Root2({
28131
28149
  noBodyStyles,
28132
28150
  autoFocus
28133
28151
  });
28134
- import_react9.default.useEffect(() => {
28135
- if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28136
- return;
28137
- }
28138
- const activeElement = document.activeElement;
28139
- if (!(activeElement instanceof HTMLElement)) {
28140
- return;
28141
- }
28142
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28152
+ useSafeLayoutEffect(() => {
28153
+ if (!isOpen) {
28143
28154
  return;
28144
28155
  }
28145
- activeElement.blur();
28146
- }, [autoFocus, isOpen, modal]);
28156
+ releaseHiddenFocusBeforeOpen2();
28157
+ }, [isOpen, releaseHiddenFocusBeforeOpen2]);
28147
28158
  function getScale() {
28148
28159
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
28149
28160
  }
@@ -28527,6 +28538,7 @@ function Root2({
28527
28538
  onOpenChange: (open) => {
28528
28539
  if (!dismissible && !open) return;
28529
28540
  if (open) {
28541
+ releaseHiddenFocusBeforeOpen2();
28530
28542
  setHasBeenOpened(true);
28531
28543
  } else {
28532
28544
  closeDrawer2(true);
@@ -28880,7 +28892,7 @@ var VISUALLY_HIDDEN_STYLE = {
28880
28892
  whiteSpace: "nowrap",
28881
28893
  border: 0
28882
28894
  };
28883
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28895
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28884
28896
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28885
28897
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28886
28898
  const baseProps = {
@@ -28914,7 +28926,7 @@ function VanillaNode({
28914
28926
  dataAttribute
28915
28927
  }) {
28916
28928
  const ref = import_react10.default.useRef(null);
28917
- useSafeLayoutEffect(() => {
28929
+ useSafeLayoutEffect2(() => {
28918
28930
  const element = ref.current;
28919
28931
  if (!element) {
28920
28932
  return;
@@ -29120,6 +29132,7 @@ function openAncestorChain(parentId) {
29120
29132
  openAncestorChain(nextParentId);
29121
29133
  }
29122
29134
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29135
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29123
29136
  parentRuntime.controller.setOpen(true);
29124
29137
  notifyOpenStateChange(parentRuntime, true);
29125
29138
  renderVanillaDrawer(parentRuntime.id);
@@ -29139,6 +29152,7 @@ function bindTriggerElement(runtime) {
29139
29152
  }
29140
29153
  const triggerElement = runtime.options.triggerElement;
29141
29154
  const handleClick = () => {
29155
+ releaseHiddenFocusBeforeOpen(runtime.options);
29142
29156
  runtime.controller.setOpen(true);
29143
29157
  renderVanillaDrawer(runtime.id);
29144
29158
  };
@@ -29174,6 +29188,16 @@ function notifyOpenStateChange(runtime, open) {
29174
29188
  function canUseDOM3() {
29175
29189
  return typeof window !== "undefined" && typeof document !== "undefined";
29176
29190
  }
29191
+ function releaseHiddenFocusBeforeOpen(options) {
29192
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29193
+ return;
29194
+ }
29195
+ const activeElement = document.activeElement;
29196
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29197
+ return;
29198
+ }
29199
+ activeElement.blur();
29200
+ }
29177
29201
  function getRuntimeDrawerElement(runtime) {
29178
29202
  if (!runtime.element) {
29179
29203
  return null;
@@ -29227,6 +29251,9 @@ function renderVanillaDrawer(id) {
29227
29251
  open: snapshot.state.isOpen,
29228
29252
  onOpenChange: (open) => {
29229
29253
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29254
+ if (open) {
29255
+ releaseHiddenFocusBeforeOpen(runtime.options);
29256
+ }
29230
29257
  runtime.controller.setOpen(open);
29231
29258
  if (previousOpen !== open) {
29232
29259
  notifyOpenStateChange(runtime, open);
@@ -29273,6 +29300,9 @@ function buildVanillaController(id) {
29273
29300
  if (!runtime) {
29274
29301
  return createDrawer({ id, open }).getSnapshot();
29275
29302
  }
29303
+ if (open) {
29304
+ releaseHiddenFocusBeforeOpen(runtime.options);
29305
+ }
29276
29306
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29277
29307
  const snapshot = runtime.controller.setOpen(open);
29278
29308
  if (previousOpen !== open) {
@@ -29343,6 +29373,9 @@ function createDrawer(options = {}) {
29343
29373
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29344
29374
  }
29345
29375
  }
29376
+ if (nextOptions.open && !previousOpen) {
29377
+ releaseHiddenFocusBeforeOpen(nextOptions);
29378
+ }
29346
29379
  renderVanillaDrawer(id);
29347
29380
  if (nextOptions.parentId && nextOptions.open) {
29348
29381
  openAncestorChain(nextOptions.parentId);
package/dist/index.mjs CHANGED
@@ -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);
@@ -1473,6 +1473,7 @@ function getDragPermission({ targetTagName, hasNoDragAttribute, direction, timeS
1473
1473
  };
1474
1474
  }
1475
1475
 
1476
+ const useSafeLayoutEffect$1 = typeof window === 'undefined' ? React__namespace.default.useEffect : React__namespace.default.useLayoutEffect;
1476
1477
  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 }) {
1477
1478
  var _drawerRef_current, _drawerRef_current1;
1478
1479
  const animationEndTimeoutRef = React__namespace.default.useRef(null);
@@ -1530,6 +1531,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1530
1531
  const drawerHeightRef = React__namespace.default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1531
1532
  const drawerWidthRef = React__namespace.default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1532
1533
  const initialDrawerHeight = React__namespace.default.useRef(0);
1534
+ const releaseHiddenFocusBeforeOpen = React__namespace.default.useCallback(()=>{
1535
+ var _drawerRef_current;
1536
+ if (!modal || autoFocus || typeof document === 'undefined') {
1537
+ return;
1538
+ }
1539
+ const activeElement = document.activeElement;
1540
+ if (!activeElement || activeElement === document.body) {
1541
+ return;
1542
+ }
1543
+ const activeElementNode = activeElement;
1544
+ if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1545
+ return;
1546
+ }
1547
+ if (typeof activeElementNode.blur !== 'function') {
1548
+ return;
1549
+ }
1550
+ activeElementNode.blur();
1551
+ }, [
1552
+ autoFocus,
1553
+ modal
1554
+ ]);
1533
1555
  const onSnapPointChange = React__namespace.default.useCallback((activeSnapPointIndex)=>{
1534
1556
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1535
1557
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1580,23 +1602,14 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1580
1602
  noBodyStyles,
1581
1603
  autoFocus
1582
1604
  });
1583
- React__namespace.default.useEffect(()=>{
1584
- var _drawerRef_current;
1585
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1605
+ useSafeLayoutEffect$1(()=>{
1606
+ if (!isOpen) {
1586
1607
  return;
1587
1608
  }
1588
- const activeElement = document.activeElement;
1589
- if (!(activeElement instanceof HTMLElement)) {
1590
- return;
1591
- }
1592
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1593
- return;
1594
- }
1595
- activeElement.blur();
1609
+ releaseHiddenFocusBeforeOpen();
1596
1610
  }, [
1597
- autoFocus,
1598
1611
  isOpen,
1599
- modal
1612
+ releaseHiddenFocusBeforeOpen
1600
1613
  ]);
1601
1614
  function getScale() {
1602
1615
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -2005,6 +2018,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
2005
2018
  onOpenChange: (open)=>{
2006
2019
  if (!dismissible && !open) return;
2007
2020
  if (open) {
2021
+ releaseHiddenFocusBeforeOpen();
2008
2022
  setHasBeenOpened(true);
2009
2023
  } else {
2010
2024
  closeDrawer(true);
@@ -2599,6 +2613,7 @@ function openAncestorChain(parentId) {
2599
2613
  openAncestorChain(nextParentId);
2600
2614
  }
2601
2615
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
2616
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
2602
2617
  parentRuntime.controller.setOpen(true);
2603
2618
  notifyOpenStateChange(parentRuntime, true);
2604
2619
  renderVanillaDrawer(parentRuntime.id);
@@ -2618,6 +2633,7 @@ function bindTriggerElement(runtime) {
2618
2633
  }
2619
2634
  const triggerElement = runtime.options.triggerElement;
2620
2635
  const handleClick = ()=>{
2636
+ releaseHiddenFocusBeforeOpen(runtime.options);
2621
2637
  runtime.controller.setOpen(true);
2622
2638
  renderVanillaDrawer(runtime.id);
2623
2639
  };
@@ -2653,6 +2669,16 @@ function notifyOpenStateChange(runtime, open) {
2653
2669
  function canUseDOM() {
2654
2670
  return typeof window !== 'undefined' && typeof document !== 'undefined';
2655
2671
  }
2672
+ function releaseHiddenFocusBeforeOpen(options) {
2673
+ if (!canUseDOM() || options.modal === false || options.autoFocus) {
2674
+ return;
2675
+ }
2676
+ const activeElement = document.activeElement;
2677
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== 'function') {
2678
+ return;
2679
+ }
2680
+ activeElement.blur();
2681
+ }
2656
2682
  function getRuntimeDrawerElement(runtime) {
2657
2683
  if (!runtime.element) {
2658
2684
  return null;
@@ -2703,6 +2729,9 @@ function renderVanillaDrawer(id) {
2703
2729
  open: snapshot.state.isOpen,
2704
2730
  onOpenChange: (open)=>{
2705
2731
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
2732
+ if (open) {
2733
+ releaseHiddenFocusBeforeOpen(runtime.options);
2734
+ }
2706
2735
  runtime.controller.setOpen(open);
2707
2736
  if (previousOpen !== open) {
2708
2737
  notifyOpenStateChange(runtime, open);
@@ -2755,6 +2784,9 @@ function buildVanillaController(id) {
2755
2784
  open
2756
2785
  }).getSnapshot();
2757
2786
  }
2787
+ if (open) {
2788
+ releaseHiddenFocusBeforeOpen(runtime.options);
2789
+ }
2758
2790
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
2759
2791
  const snapshot = runtime.controller.setOpen(open);
2760
2792
  if (previousOpen !== open) {
@@ -2848,6 +2880,9 @@ function createDrawer(options = {}) {
2848
2880
  notifyOpenStateChange(existing, snapshot.state.isOpen);
2849
2881
  }
2850
2882
  }
2883
+ if (nextOptions.open && !previousOpen) {
2884
+ releaseHiddenFocusBeforeOpen(nextOptions);
2885
+ }
2851
2886
  renderVanillaDrawer(id);
2852
2887
  if (nextOptions.parentId && nextOptions.open) {
2853
2888
  openAncestorChain(nextOptions.parentId);
@@ -1451,6 +1451,7 @@ function getDragPermission({ targetTagName, hasNoDragAttribute, direction, timeS
1451
1451
  };
1452
1452
  }
1453
1453
 
1454
+ const useSafeLayoutEffect$1 = typeof window === 'undefined' ? React__default.useEffect : React__default.useLayoutEffect;
1454
1455
  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 }) {
1455
1456
  var _drawerRef_current, _drawerRef_current1;
1456
1457
  const animationEndTimeoutRef = React__default.useRef(null);
@@ -1508,6 +1509,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1508
1509
  const drawerHeightRef = React__default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1509
1510
  const drawerWidthRef = React__default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1510
1511
  const initialDrawerHeight = React__default.useRef(0);
1512
+ const releaseHiddenFocusBeforeOpen = React__default.useCallback(()=>{
1513
+ var _drawerRef_current;
1514
+ if (!modal || autoFocus || typeof document === 'undefined') {
1515
+ return;
1516
+ }
1517
+ const activeElement = document.activeElement;
1518
+ if (!activeElement || activeElement === document.body) {
1519
+ return;
1520
+ }
1521
+ const activeElementNode = activeElement;
1522
+ if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1523
+ return;
1524
+ }
1525
+ if (typeof activeElementNode.blur !== 'function') {
1526
+ return;
1527
+ }
1528
+ activeElementNode.blur();
1529
+ }, [
1530
+ autoFocus,
1531
+ modal
1532
+ ]);
1511
1533
  const onSnapPointChange = React__default.useCallback((activeSnapPointIndex)=>{
1512
1534
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1513
1535
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1558,23 +1580,14 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1558
1580
  noBodyStyles,
1559
1581
  autoFocus
1560
1582
  });
1561
- React__default.useEffect(()=>{
1562
- var _drawerRef_current;
1563
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1583
+ useSafeLayoutEffect$1(()=>{
1584
+ if (!isOpen) {
1564
1585
  return;
1565
1586
  }
1566
- const activeElement = document.activeElement;
1567
- if (!(activeElement instanceof HTMLElement)) {
1568
- return;
1569
- }
1570
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1571
- return;
1572
- }
1573
- activeElement.blur();
1587
+ releaseHiddenFocusBeforeOpen();
1574
1588
  }, [
1575
- autoFocus,
1576
1589
  isOpen,
1577
- modal
1590
+ releaseHiddenFocusBeforeOpen
1578
1591
  ]);
1579
1592
  function getScale() {
1580
1593
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -1983,6 +1996,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1983
1996
  onOpenChange: (open)=>{
1984
1997
  if (!dismissible && !open) return;
1985
1998
  if (open) {
1999
+ releaseHiddenFocusBeforeOpen();
1986
2000
  setHasBeenOpened(true);
1987
2001
  } else {
1988
2002
  closeDrawer(true);
@@ -2577,6 +2591,7 @@ function openAncestorChain(parentId) {
2577
2591
  openAncestorChain(nextParentId);
2578
2592
  }
2579
2593
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
2594
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
2580
2595
  parentRuntime.controller.setOpen(true);
2581
2596
  notifyOpenStateChange(parentRuntime, true);
2582
2597
  renderVanillaDrawer(parentRuntime.id);
@@ -2596,6 +2611,7 @@ function bindTriggerElement(runtime) {
2596
2611
  }
2597
2612
  const triggerElement = runtime.options.triggerElement;
2598
2613
  const handleClick = ()=>{
2614
+ releaseHiddenFocusBeforeOpen(runtime.options);
2599
2615
  runtime.controller.setOpen(true);
2600
2616
  renderVanillaDrawer(runtime.id);
2601
2617
  };
@@ -2631,6 +2647,16 @@ function notifyOpenStateChange(runtime, open) {
2631
2647
  function canUseDOM() {
2632
2648
  return typeof window !== 'undefined' && typeof document !== 'undefined';
2633
2649
  }
2650
+ function releaseHiddenFocusBeforeOpen(options) {
2651
+ if (!canUseDOM() || options.modal === false || options.autoFocus) {
2652
+ return;
2653
+ }
2654
+ const activeElement = document.activeElement;
2655
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== 'function') {
2656
+ return;
2657
+ }
2658
+ activeElement.blur();
2659
+ }
2634
2660
  function getRuntimeDrawerElement(runtime) {
2635
2661
  if (!runtime.element) {
2636
2662
  return null;
@@ -2681,6 +2707,9 @@ function renderVanillaDrawer(id) {
2681
2707
  open: snapshot.state.isOpen,
2682
2708
  onOpenChange: (open)=>{
2683
2709
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
2710
+ if (open) {
2711
+ releaseHiddenFocusBeforeOpen(runtime.options);
2712
+ }
2684
2713
  runtime.controller.setOpen(open);
2685
2714
  if (previousOpen !== open) {
2686
2715
  notifyOpenStateChange(runtime, open);
@@ -2733,6 +2762,9 @@ function buildVanillaController(id) {
2733
2762
  open
2734
2763
  }).getSnapshot();
2735
2764
  }
2765
+ if (open) {
2766
+ releaseHiddenFocusBeforeOpen(runtime.options);
2767
+ }
2736
2768
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
2737
2769
  const snapshot = runtime.controller.setOpen(open);
2738
2770
  if (previousOpen !== open) {
@@ -2826,6 +2858,9 @@ function createDrawer(options = {}) {
2826
2858
  notifyOpenStateChange(existing, snapshot.state.isOpen);
2827
2859
  }
2828
2860
  }
2861
+ if (nextOptions.open && !previousOpen) {
2862
+ releaseHiddenFocusBeforeOpen(nextOptions);
2863
+ }
2829
2864
  renderVanillaDrawer(id);
2830
2865
  if (nextOptions.parentId && nextOptions.open) {
2831
2866
  openAncestorChain(nextOptions.parentId);