@samline/drawer 2.0.6 → 2.0.8

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.
@@ -1528,6 +1528,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1528
1528
  const drawerHeightRef = React__namespace.default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1529
1529
  const drawerWidthRef = React__namespace.default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1530
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
+ ]);
1531
1552
  const onSnapPointChange = React__namespace.default.useCallback((activeSnapPointIndex)=>{
1532
1553
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1533
1554
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1579,26 +1600,13 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1579
1600
  autoFocus
1580
1601
  });
1581
1602
  useSafeLayoutEffect(()=>{
1582
- var _drawerRef_current;
1583
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1603
+ if (!isOpen) {
1584
1604
  return;
1585
1605
  }
1586
- const activeElement = document.activeElement;
1587
- if (!activeElement || activeElement === document.body) {
1588
- return;
1589
- }
1590
- const activeElementNode = activeElement;
1591
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1592
- return;
1593
- }
1594
- if (typeof activeElementNode.blur !== 'function') {
1595
- return;
1596
- }
1597
- activeElementNode.blur();
1606
+ releaseHiddenFocusBeforeOpen();
1598
1607
  }, [
1599
- autoFocus,
1600
1608
  isOpen,
1601
- modal
1609
+ releaseHiddenFocusBeforeOpen
1602
1610
  ]);
1603
1611
  function getScale() {
1604
1612
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -2007,6 +2015,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
2007
2015
  onOpenChange: (open)=>{
2008
2016
  if (!dismissible && !open) return;
2009
2017
  if (open) {
2018
+ releaseHiddenFocusBeforeOpen();
2010
2019
  setHasBeenOpened(true);
2011
2020
  } else {
2012
2021
  closeDrawer(true);
@@ -1508,6 +1508,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1508
1508
  const drawerHeightRef = React__default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1509
1509
  const drawerWidthRef = React__default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1510
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
+ ]);
1511
1532
  const onSnapPointChange = React__default.useCallback((activeSnapPointIndex)=>{
1512
1533
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1513
1534
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1559,26 +1580,13 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1559
1580
  autoFocus
1560
1581
  });
1561
1582
  useSafeLayoutEffect(()=>{
1562
- var _drawerRef_current;
1563
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1583
+ if (!isOpen) {
1564
1584
  return;
1565
1585
  }
1566
- const activeElement = document.activeElement;
1567
- if (!activeElement || activeElement === document.body) {
1568
- return;
1569
- }
1570
- const activeElementNode = activeElement;
1571
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1572
- return;
1573
- }
1574
- if (typeof activeElementNode.blur !== 'function') {
1575
- return;
1576
- }
1577
- activeElementNode.blur();
1586
+ releaseHiddenFocusBeforeOpen();
1578
1587
  }, [
1579
- autoFocus,
1580
1588
  isOpen,
1581
- modal
1589
+ releaseHiddenFocusBeforeOpen
1582
1590
  ]);
1583
1591
  function getScale() {
1584
1592
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -1987,6 +1995,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1987
1995
  onOpenChange: (open)=>{
1988
1996
  if (!dismissible && !open) return;
1989
1997
  if (open) {
1998
+ releaseHiddenFocusBeforeOpen();
1990
1999
  setHasBeenOpened(true);
1991
2000
  } else {
1992
2001
  closeDrawer(true);
package/dist/index.js CHANGED
@@ -28074,6 +28074,23 @@ function Root2({
28074
28074
  const drawerHeightRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
28075
28075
  const drawerWidthRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
28076
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]);
28077
28094
  const onSnapPointChange = import_react9.default.useCallback((activeSnapPointIndex2) => {
28078
28095
  if (snapPoints && activeSnapPointIndex2 === snapPointsOffset.length - 1) openTime.current = /* @__PURE__ */ new Date();
28079
28096
  }, []);
@@ -28133,22 +28150,11 @@ function Root2({
28133
28150
  autoFocus
28134
28151
  });
28135
28152
  useSafeLayoutEffect(() => {
28136
- if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28137
- return;
28138
- }
28139
- const activeElement = document.activeElement;
28140
- if (!activeElement || activeElement === document.body) {
28153
+ if (!isOpen) {
28141
28154
  return;
28142
28155
  }
28143
- const activeElementNode = activeElement;
28144
- if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28145
- return;
28146
- }
28147
- if (typeof activeElementNode.blur !== "function") {
28148
- return;
28149
- }
28150
- activeElementNode.blur();
28151
- }, [autoFocus, isOpen, modal]);
28156
+ releaseHiddenFocusBeforeOpen2();
28157
+ }, [isOpen, releaseHiddenFocusBeforeOpen2]);
28152
28158
  function getScale() {
28153
28159
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
28154
28160
  }
@@ -28532,6 +28538,7 @@ function Root2({
28532
28538
  onOpenChange: (open) => {
28533
28539
  if (!dismissible && !open) return;
28534
28540
  if (open) {
28541
+ releaseHiddenFocusBeforeOpen2();
28535
28542
  setHasBeenOpened(true);
28536
28543
  } else {
28537
28544
  closeDrawer2(true);
@@ -29125,7 +29132,7 @@ function openAncestorChain(parentId) {
29125
29132
  openAncestorChain(nextParentId);
29126
29133
  }
29127
29134
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29128
- releaseHiddenFocusBeforeOpen(parentRuntime.options);
29135
+ releaseHiddenFocusBeforeOpen(parentRuntime.options, getRuntimeDrawerElement(parentRuntime));
29129
29136
  parentRuntime.controller.setOpen(true);
29130
29137
  notifyOpenStateChange(parentRuntime, true);
29131
29138
  renderVanillaDrawer(parentRuntime.id);
@@ -29140,18 +29147,36 @@ function cleanupRuntimeTrigger(runtime) {
29140
29147
  }
29141
29148
  function bindTriggerElement(runtime) {
29142
29149
  cleanupRuntimeTrigger(runtime);
29150
+ const drawerElement = getRuntimeDrawerElement(runtime);
29151
+ const builtInTriggerElement = getRuntimeTriggerElement(runtime);
29152
+ const cleanups = [];
29153
+ if (builtInTriggerElement) {
29154
+ const handleBuiltInTriggerClick = () => {
29155
+ releaseHiddenFocusBeforeOpen(runtime.options, drawerElement);
29156
+ };
29157
+ builtInTriggerElement.addEventListener("click", handleBuiltInTriggerClick);
29158
+ cleanups.push(() => {
29159
+ builtInTriggerElement.removeEventListener("click", handleBuiltInTriggerClick);
29160
+ });
29161
+ }
29143
29162
  if (!runtime.options.triggerElement) {
29163
+ runtime.cleanupTriggerElement = cleanups.length ? () => {
29164
+ cleanups.forEach((cleanup) => cleanup());
29165
+ } : null;
29144
29166
  return;
29145
29167
  }
29146
29168
  const triggerElement = runtime.options.triggerElement;
29147
29169
  const handleClick = () => {
29148
- releaseHiddenFocusBeforeOpen(runtime.options);
29170
+ releaseHiddenFocusBeforeOpen(runtime.options, drawerElement);
29149
29171
  runtime.controller.setOpen(true);
29150
29172
  renderVanillaDrawer(runtime.id);
29151
29173
  };
29152
29174
  triggerElement.addEventListener("click", handleClick);
29153
- runtime.cleanupTriggerElement = () => {
29175
+ cleanups.push(() => {
29154
29176
  triggerElement.removeEventListener("click", handleClick);
29177
+ });
29178
+ runtime.cleanupTriggerElement = () => {
29179
+ cleanups.forEach((cleanup) => cleanup());
29155
29180
  };
29156
29181
  }
29157
29182
  function notifyOpenStateChange(runtime, open) {
@@ -29181,15 +29206,32 @@ function notifyOpenStateChange(runtime, open) {
29181
29206
  function canUseDOM3() {
29182
29207
  return typeof window !== "undefined" && typeof document !== "undefined";
29183
29208
  }
29184
- function releaseHiddenFocusBeforeOpen(options) {
29209
+ function isElementInsideDrawer(element) {
29210
+ let currentElement = element;
29211
+ while (currentElement) {
29212
+ if (currentElement instanceof HTMLElement && currentElement.hasAttribute("data-drawer")) {
29213
+ return true;
29214
+ }
29215
+ currentElement = currentElement.parentElement;
29216
+ }
29217
+ return false;
29218
+ }
29219
+ function releaseHiddenFocusBeforeOpen(options, drawerElement) {
29185
29220
  if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29186
29221
  return;
29187
29222
  }
29188
29223
  const activeElement = document.activeElement;
29189
- if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29224
+ if (!activeElement || activeElement === document.body) {
29190
29225
  return;
29191
29226
  }
29192
- activeElement.blur();
29227
+ const activeElementNode = activeElement;
29228
+ if (drawerElement?.contains(activeElementNode) || isElementInsideDrawer(activeElementNode)) {
29229
+ return;
29230
+ }
29231
+ if (typeof activeElementNode.blur !== "function") {
29232
+ return;
29233
+ }
29234
+ activeElementNode.blur();
29193
29235
  }
29194
29236
  function getRuntimeDrawerElement(runtime) {
29195
29237
  if (!runtime.element) {
@@ -29197,6 +29239,12 @@ function getRuntimeDrawerElement(runtime) {
29197
29239
  }
29198
29240
  return runtime.element.querySelector("[data-drawer]");
29199
29241
  }
29242
+ function getRuntimeTriggerElement(runtime) {
29243
+ if (!runtime.element) {
29244
+ return null;
29245
+ }
29246
+ return runtime.element.querySelector("[data-drawer-vanilla-trigger]");
29247
+ }
29200
29248
  function getViewportSizeForDirection(direction) {
29201
29249
  if (!canUseDOM3()) {
29202
29250
  return 0;
@@ -29245,7 +29293,7 @@ function renderVanillaDrawer(id) {
29245
29293
  onOpenChange: (open) => {
29246
29294
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29247
29295
  if (open) {
29248
- releaseHiddenFocusBeforeOpen(runtime.options);
29296
+ releaseHiddenFocusBeforeOpen(runtime.options, getRuntimeDrawerElement(runtime));
29249
29297
  }
29250
29298
  runtime.controller.setOpen(open);
29251
29299
  if (previousOpen !== open) {
@@ -29294,7 +29342,7 @@ function buildVanillaController(id) {
29294
29342
  return createDrawer({ id, open }).getSnapshot();
29295
29343
  }
29296
29344
  if (open) {
29297
- releaseHiddenFocusBeforeOpen(runtime.options);
29345
+ releaseHiddenFocusBeforeOpen(runtime.options, getRuntimeDrawerElement(runtime));
29298
29346
  }
29299
29347
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29300
29348
  const snapshot = runtime.controller.setOpen(open);
@@ -29367,7 +29415,7 @@ function createDrawer(options = {}) {
29367
29415
  }
29368
29416
  }
29369
29417
  if (nextOptions.open && !previousOpen) {
29370
- releaseHiddenFocusBeforeOpen(nextOptions);
29418
+ releaseHiddenFocusBeforeOpen(nextOptions, existing ? getRuntimeDrawerElement(existing) : null);
29371
29419
  }
29372
29420
  renderVanillaDrawer(id);
29373
29421
  if (nextOptions.parentId && nextOptions.open) {
package/dist/index.mjs CHANGED
@@ -28049,6 +28049,23 @@ function Root2({
28049
28049
  const drawerHeightRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
28050
28050
  const drawerWidthRef = import_react9.default.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
28051
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]);
28052
28069
  const onSnapPointChange = import_react9.default.useCallback((activeSnapPointIndex2) => {
28053
28070
  if (snapPoints && activeSnapPointIndex2 === snapPointsOffset.length - 1) openTime.current = /* @__PURE__ */ new Date();
28054
28071
  }, []);
@@ -28108,22 +28125,11 @@ function Root2({
28108
28125
  autoFocus
28109
28126
  });
28110
28127
  useSafeLayoutEffect(() => {
28111
- if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28112
- return;
28113
- }
28114
- const activeElement = document.activeElement;
28115
- if (!activeElement || activeElement === document.body) {
28128
+ if (!isOpen) {
28116
28129
  return;
28117
28130
  }
28118
- const activeElementNode = activeElement;
28119
- if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28120
- return;
28121
- }
28122
- if (typeof activeElementNode.blur !== "function") {
28123
- return;
28124
- }
28125
- activeElementNode.blur();
28126
- }, [autoFocus, isOpen, modal]);
28131
+ releaseHiddenFocusBeforeOpen2();
28132
+ }, [isOpen, releaseHiddenFocusBeforeOpen2]);
28127
28133
  function getScale() {
28128
28134
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
28129
28135
  }
@@ -28507,6 +28513,7 @@ function Root2({
28507
28513
  onOpenChange: (open) => {
28508
28514
  if (!dismissible && !open) return;
28509
28515
  if (open) {
28516
+ releaseHiddenFocusBeforeOpen2();
28510
28517
  setHasBeenOpened(true);
28511
28518
  } else {
28512
28519
  closeDrawer2(true);
@@ -29100,7 +29107,7 @@ function openAncestorChain(parentId) {
29100
29107
  openAncestorChain(nextParentId);
29101
29108
  }
29102
29109
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29103
- releaseHiddenFocusBeforeOpen(parentRuntime.options);
29110
+ releaseHiddenFocusBeforeOpen(parentRuntime.options, getRuntimeDrawerElement(parentRuntime));
29104
29111
  parentRuntime.controller.setOpen(true);
29105
29112
  notifyOpenStateChange(parentRuntime, true);
29106
29113
  renderVanillaDrawer(parentRuntime.id);
@@ -29115,18 +29122,36 @@ function cleanupRuntimeTrigger(runtime) {
29115
29122
  }
29116
29123
  function bindTriggerElement(runtime) {
29117
29124
  cleanupRuntimeTrigger(runtime);
29125
+ const drawerElement = getRuntimeDrawerElement(runtime);
29126
+ const builtInTriggerElement = getRuntimeTriggerElement(runtime);
29127
+ const cleanups = [];
29128
+ if (builtInTriggerElement) {
29129
+ const handleBuiltInTriggerClick = () => {
29130
+ releaseHiddenFocusBeforeOpen(runtime.options, drawerElement);
29131
+ };
29132
+ builtInTriggerElement.addEventListener("click", handleBuiltInTriggerClick);
29133
+ cleanups.push(() => {
29134
+ builtInTriggerElement.removeEventListener("click", handleBuiltInTriggerClick);
29135
+ });
29136
+ }
29118
29137
  if (!runtime.options.triggerElement) {
29138
+ runtime.cleanupTriggerElement = cleanups.length ? () => {
29139
+ cleanups.forEach((cleanup) => cleanup());
29140
+ } : null;
29119
29141
  return;
29120
29142
  }
29121
29143
  const triggerElement = runtime.options.triggerElement;
29122
29144
  const handleClick = () => {
29123
- releaseHiddenFocusBeforeOpen(runtime.options);
29145
+ releaseHiddenFocusBeforeOpen(runtime.options, drawerElement);
29124
29146
  runtime.controller.setOpen(true);
29125
29147
  renderVanillaDrawer(runtime.id);
29126
29148
  };
29127
29149
  triggerElement.addEventListener("click", handleClick);
29128
- runtime.cleanupTriggerElement = () => {
29150
+ cleanups.push(() => {
29129
29151
  triggerElement.removeEventListener("click", handleClick);
29152
+ });
29153
+ runtime.cleanupTriggerElement = () => {
29154
+ cleanups.forEach((cleanup) => cleanup());
29130
29155
  };
29131
29156
  }
29132
29157
  function notifyOpenStateChange(runtime, open) {
@@ -29156,15 +29181,32 @@ function notifyOpenStateChange(runtime, open) {
29156
29181
  function canUseDOM3() {
29157
29182
  return typeof window !== "undefined" && typeof document !== "undefined";
29158
29183
  }
29159
- function releaseHiddenFocusBeforeOpen(options) {
29184
+ function isElementInsideDrawer(element) {
29185
+ let currentElement = element;
29186
+ while (currentElement) {
29187
+ if (currentElement instanceof HTMLElement && currentElement.hasAttribute("data-drawer")) {
29188
+ return true;
29189
+ }
29190
+ currentElement = currentElement.parentElement;
29191
+ }
29192
+ return false;
29193
+ }
29194
+ function releaseHiddenFocusBeforeOpen(options, drawerElement) {
29160
29195
  if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29161
29196
  return;
29162
29197
  }
29163
29198
  const activeElement = document.activeElement;
29164
- if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29199
+ if (!activeElement || activeElement === document.body) {
29165
29200
  return;
29166
29201
  }
29167
- activeElement.blur();
29202
+ const activeElementNode = activeElement;
29203
+ if (drawerElement?.contains(activeElementNode) || isElementInsideDrawer(activeElementNode)) {
29204
+ return;
29205
+ }
29206
+ if (typeof activeElementNode.blur !== "function") {
29207
+ return;
29208
+ }
29209
+ activeElementNode.blur();
29168
29210
  }
29169
29211
  function getRuntimeDrawerElement(runtime) {
29170
29212
  if (!runtime.element) {
@@ -29172,6 +29214,12 @@ function getRuntimeDrawerElement(runtime) {
29172
29214
  }
29173
29215
  return runtime.element.querySelector("[data-drawer]");
29174
29216
  }
29217
+ function getRuntimeTriggerElement(runtime) {
29218
+ if (!runtime.element) {
29219
+ return null;
29220
+ }
29221
+ return runtime.element.querySelector("[data-drawer-vanilla-trigger]");
29222
+ }
29175
29223
  function getViewportSizeForDirection(direction) {
29176
29224
  if (!canUseDOM3()) {
29177
29225
  return 0;
@@ -29220,7 +29268,7 @@ function renderVanillaDrawer(id) {
29220
29268
  onOpenChange: (open) => {
29221
29269
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29222
29270
  if (open) {
29223
- releaseHiddenFocusBeforeOpen(runtime.options);
29271
+ releaseHiddenFocusBeforeOpen(runtime.options, getRuntimeDrawerElement(runtime));
29224
29272
  }
29225
29273
  runtime.controller.setOpen(open);
29226
29274
  if (previousOpen !== open) {
@@ -29269,7 +29317,7 @@ function buildVanillaController(id) {
29269
29317
  return createDrawer({ id, open }).getSnapshot();
29270
29318
  }
29271
29319
  if (open) {
29272
- releaseHiddenFocusBeforeOpen(runtime.options);
29320
+ releaseHiddenFocusBeforeOpen(runtime.options, getRuntimeDrawerElement(runtime));
29273
29321
  }
29274
29322
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29275
29323
  const snapshot = runtime.controller.setOpen(open);
@@ -29342,7 +29390,7 @@ function createDrawer(options = {}) {
29342
29390
  }
29343
29391
  }
29344
29392
  if (nextOptions.open && !previousOpen) {
29345
- releaseHiddenFocusBeforeOpen(nextOptions);
29393
+ releaseHiddenFocusBeforeOpen(nextOptions, existing ? getRuntimeDrawerElement(existing) : null);
29346
29394
  }
29347
29395
  renderVanillaDrawer(id);
29348
29396
  if (nextOptions.parentId && nextOptions.open) {
@@ -1531,6 +1531,27 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1531
1531
  const drawerHeightRef = React__namespace.default.useRef(((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.getBoundingClientRect().height) || 0);
1532
1532
  const drawerWidthRef = React__namespace.default.useRef(((_drawerRef_current1 = drawerRef.current) == null ? void 0 : _drawerRef_current1.getBoundingClientRect().width) || 0);
1533
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
+ ]);
1534
1555
  const onSnapPointChange = React__namespace.default.useCallback((activeSnapPointIndex)=>{
1535
1556
  // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
1536
1557
  if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1) openTime.current = new Date();
@@ -1582,26 +1603,13 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1582
1603
  autoFocus
1583
1604
  });
1584
1605
  useSafeLayoutEffect$1(()=>{
1585
- var _drawerRef_current;
1586
- if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1587
- return;
1588
- }
1589
- const activeElement = document.activeElement;
1590
- if (!activeElement || activeElement === document.body) {
1591
- return;
1592
- }
1593
- const activeElementNode = activeElement;
1594
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElementNode)) || (activeElementNode.closest == null ? void 0 : activeElementNode.closest.call(activeElementNode, '[data-drawer]'))) {
1606
+ if (!isOpen) {
1595
1607
  return;
1596
1608
  }
1597
- if (typeof activeElementNode.blur !== 'function') {
1598
- return;
1599
- }
1600
- activeElementNode.blur();
1609
+ releaseHiddenFocusBeforeOpen();
1601
1610
  }, [
1602
- autoFocus,
1603
1611
  isOpen,
1604
- modal
1612
+ releaseHiddenFocusBeforeOpen
1605
1613
  ]);
1606
1614
  function getScale() {
1607
1615
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -2010,6 +2018,7 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
2010
2018
  onOpenChange: (open)=>{
2011
2019
  if (!dismissible && !open) return;
2012
2020
  if (open) {
2021
+ releaseHiddenFocusBeforeOpen();
2013
2022
  setHasBeenOpened(true);
2014
2023
  } else {
2015
2024
  closeDrawer(true);
@@ -2604,7 +2613,7 @@ function openAncestorChain(parentId) {
2604
2613
  openAncestorChain(nextParentId);
2605
2614
  }
2606
2615
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
2607
- releaseHiddenFocusBeforeOpen(parentRuntime.options);
2616
+ releaseHiddenFocusBeforeOpen(parentRuntime.options, getRuntimeDrawerElement(parentRuntime));
2608
2617
  parentRuntime.controller.setOpen(true);
2609
2618
  notifyOpenStateChange(parentRuntime, true);
2610
2619
  renderVanillaDrawer(parentRuntime.id);
@@ -2619,18 +2628,36 @@ function cleanupRuntimeTrigger(runtime) {
2619
2628
  }
2620
2629
  function bindTriggerElement(runtime) {
2621
2630
  cleanupRuntimeTrigger(runtime);
2631
+ const drawerElement = getRuntimeDrawerElement(runtime);
2632
+ const builtInTriggerElement = getRuntimeTriggerElement(runtime);
2633
+ const cleanups = [];
2634
+ if (builtInTriggerElement) {
2635
+ const handleBuiltInTriggerClick = ()=>{
2636
+ releaseHiddenFocusBeforeOpen(runtime.options, drawerElement);
2637
+ };
2638
+ builtInTriggerElement.addEventListener('click', handleBuiltInTriggerClick);
2639
+ cleanups.push(()=>{
2640
+ builtInTriggerElement.removeEventListener('click', handleBuiltInTriggerClick);
2641
+ });
2642
+ }
2622
2643
  if (!runtime.options.triggerElement) {
2644
+ runtime.cleanupTriggerElement = cleanups.length ? ()=>{
2645
+ cleanups.forEach((cleanup)=>cleanup());
2646
+ } : null;
2623
2647
  return;
2624
2648
  }
2625
2649
  const triggerElement = runtime.options.triggerElement;
2626
2650
  const handleClick = ()=>{
2627
- releaseHiddenFocusBeforeOpen(runtime.options);
2651
+ releaseHiddenFocusBeforeOpen(runtime.options, drawerElement);
2628
2652
  runtime.controller.setOpen(true);
2629
2653
  renderVanillaDrawer(runtime.id);
2630
2654
  };
2631
2655
  triggerElement.addEventListener('click', handleClick);
2632
- runtime.cleanupTriggerElement = ()=>{
2656
+ cleanups.push(()=>{
2633
2657
  triggerElement.removeEventListener('click', handleClick);
2658
+ });
2659
+ runtime.cleanupTriggerElement = ()=>{
2660
+ cleanups.forEach((cleanup)=>cleanup());
2634
2661
  };
2635
2662
  }
2636
2663
  function notifyOpenStateChange(runtime, open) {
@@ -2660,15 +2687,32 @@ function notifyOpenStateChange(runtime, open) {
2660
2687
  function canUseDOM() {
2661
2688
  return typeof window !== 'undefined' && typeof document !== 'undefined';
2662
2689
  }
2663
- function releaseHiddenFocusBeforeOpen(options) {
2690
+ function isElementInsideDrawer(element) {
2691
+ let currentElement = element;
2692
+ while(currentElement){
2693
+ if (currentElement instanceof HTMLElement && currentElement.hasAttribute('data-drawer')) {
2694
+ return true;
2695
+ }
2696
+ currentElement = currentElement.parentElement;
2697
+ }
2698
+ return false;
2699
+ }
2700
+ function releaseHiddenFocusBeforeOpen(options, drawerElement) {
2664
2701
  if (!canUseDOM() || options.modal === false || options.autoFocus) {
2665
2702
  return;
2666
2703
  }
2667
2704
  const activeElement = document.activeElement;
2668
- if (!activeElement || activeElement === document.body || typeof activeElement.blur !== 'function') {
2705
+ if (!activeElement || activeElement === document.body) {
2706
+ return;
2707
+ }
2708
+ const activeElementNode = activeElement;
2709
+ if ((drawerElement == null ? void 0 : drawerElement.contains(activeElementNode)) || isElementInsideDrawer(activeElementNode)) {
2669
2710
  return;
2670
2711
  }
2671
- activeElement.blur();
2712
+ if (typeof activeElementNode.blur !== 'function') {
2713
+ return;
2714
+ }
2715
+ activeElementNode.blur();
2672
2716
  }
2673
2717
  function getRuntimeDrawerElement(runtime) {
2674
2718
  if (!runtime.element) {
@@ -2676,6 +2720,12 @@ function getRuntimeDrawerElement(runtime) {
2676
2720
  }
2677
2721
  return runtime.element.querySelector('[data-drawer]');
2678
2722
  }
2723
+ function getRuntimeTriggerElement(runtime) {
2724
+ if (!runtime.element) {
2725
+ return null;
2726
+ }
2727
+ return runtime.element.querySelector('[data-drawer-vanilla-trigger]');
2728
+ }
2679
2729
  function getViewportSizeForDirection(direction) {
2680
2730
  if (!canUseDOM()) {
2681
2731
  return 0;
@@ -2721,7 +2771,7 @@ function renderVanillaDrawer(id) {
2721
2771
  onOpenChange: (open)=>{
2722
2772
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
2723
2773
  if (open) {
2724
- releaseHiddenFocusBeforeOpen(runtime.options);
2774
+ releaseHiddenFocusBeforeOpen(runtime.options, getRuntimeDrawerElement(runtime));
2725
2775
  }
2726
2776
  runtime.controller.setOpen(open);
2727
2777
  if (previousOpen !== open) {
@@ -2776,7 +2826,7 @@ function buildVanillaController(id) {
2776
2826
  }).getSnapshot();
2777
2827
  }
2778
2828
  if (open) {
2779
- releaseHiddenFocusBeforeOpen(runtime.options);
2829
+ releaseHiddenFocusBeforeOpen(runtime.options, getRuntimeDrawerElement(runtime));
2780
2830
  }
2781
2831
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
2782
2832
  const snapshot = runtime.controller.setOpen(open);
@@ -2872,7 +2922,7 @@ function createDrawer(options = {}) {
2872
2922
  }
2873
2923
  }
2874
2924
  if (nextOptions.open && !previousOpen) {
2875
- releaseHiddenFocusBeforeOpen(nextOptions);
2925
+ releaseHiddenFocusBeforeOpen(nextOptions, existing ? getRuntimeDrawerElement(existing) : null);
2876
2926
  }
2877
2927
  renderVanillaDrawer(id);
2878
2928
  if (nextOptions.parentId && nextOptions.open) {