@samline/drawer 2.0.5 → 2.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -17,6 +17,7 @@ bun add @samline/drawer
17
17
  ## Quick Start
18
18
 
19
19
  ```ts
20
+ import '@samline/drawer/styles.css'
20
21
  import { createDrawer } from '@samline/drawer'
21
22
 
22
23
  const drawer = createDrawer({
@@ -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);
@@ -1577,19 +1578,23 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1577
1578
  noBodyStyles,
1578
1579
  autoFocus
1579
1580
  });
1580
- React__namespace.default.useEffect(()=>{
1581
+ useSafeLayoutEffect(()=>{
1581
1582
  var _drawerRef_current;
1582
1583
  if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1583
1584
  return;
1584
1585
  }
1585
1586
  const activeElement = document.activeElement;
1586
- if (!(activeElement instanceof HTMLElement)) {
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]'))) {
1587
1592
  return;
1588
1593
  }
1589
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1594
+ if (typeof activeElementNode.blur !== 'function') {
1590
1595
  return;
1591
1596
  }
1592
- activeElement.blur();
1597
+ activeElementNode.blur();
1593
1598
  }, [
1594
1599
  autoFocus,
1595
1600
  isOpen,
@@ -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);
@@ -1557,19 +1558,23 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1557
1558
  noBodyStyles,
1558
1559
  autoFocus
1559
1560
  });
1560
- React__default.useEffect(()=>{
1561
+ useSafeLayoutEffect(()=>{
1561
1562
  var _drawerRef_current;
1562
1563
  if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1563
1564
  return;
1564
1565
  }
1565
1566
  const activeElement = document.activeElement;
1566
- if (!(activeElement instanceof HTMLElement)) {
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]'))) {
1567
1572
  return;
1568
1573
  }
1569
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1574
+ if (typeof activeElementNode.blur !== 'function') {
1570
1575
  return;
1571
1576
  }
1572
- activeElement.blur();
1577
+ activeElementNode.blur();
1573
1578
  }, [
1574
1579
  autoFocus,
1575
1580
  isOpen,
@@ -27989,6 +27989,7 @@ function getDragPermission({
27989
27989
  }
27990
27990
 
27991
27991
  // src/react/components.tsx
27992
+ var useSafeLayoutEffect = typeof window === "undefined" ? import_react9.default.useEffect : import_react9.default.useLayoutEffect;
27992
27993
  function Root2({
27993
27994
  open: openProp,
27994
27995
  onOpenChange,
@@ -28132,18 +28133,22 @@ function Root2({
28132
28133
  noBodyStyles,
28133
28134
  autoFocus
28134
28135
  });
28135
- import_react9.default.useEffect(() => {
28136
+ useSafeLayoutEffect(() => {
28136
28137
  if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28137
28138
  return;
28138
28139
  }
28139
28140
  const activeElement = document.activeElement;
28140
- if (!(activeElement instanceof HTMLElement)) {
28141
+ if (!activeElement || activeElement === document.body) {
28142
+ return;
28143
+ }
28144
+ const activeElementNode = activeElement;
28145
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28141
28146
  return;
28142
28147
  }
28143
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28148
+ if (typeof activeElementNode.blur !== "function") {
28144
28149
  return;
28145
28150
  }
28146
- activeElement.blur();
28151
+ activeElementNode.blur();
28147
28152
  }, [autoFocus, isOpen, modal]);
28148
28153
  function getScale() {
28149
28154
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -28881,7 +28886,7 @@ var VISUALLY_HIDDEN_STYLE = {
28881
28886
  whiteSpace: "nowrap",
28882
28887
  border: 0
28883
28888
  };
28884
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28889
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28885
28890
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28886
28891
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28887
28892
  const baseProps = {
@@ -28915,7 +28920,7 @@ function VanillaNode({
28915
28920
  dataAttribute
28916
28921
  }) {
28917
28922
  const ref = import_react10.default.useRef(null);
28918
- useSafeLayoutEffect(() => {
28923
+ useSafeLayoutEffect2(() => {
28919
28924
  const element = ref.current;
28920
28925
  if (!element) {
28921
28926
  return;
@@ -29121,6 +29126,7 @@ function openAncestorChain(parentId) {
29121
29126
  openAncestorChain(nextParentId);
29122
29127
  }
29123
29128
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29129
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29124
29130
  parentRuntime.controller.setOpen(true);
29125
29131
  notifyOpenStateChange(parentRuntime, true);
29126
29132
  renderVanillaDrawer(parentRuntime.id);
@@ -29140,6 +29146,7 @@ function bindTriggerElement(runtime) {
29140
29146
  }
29141
29147
  const triggerElement = runtime.options.triggerElement;
29142
29148
  const handleClick = () => {
29149
+ releaseHiddenFocusBeforeOpen(runtime.options);
29143
29150
  runtime.controller.setOpen(true);
29144
29151
  renderVanillaDrawer(runtime.id);
29145
29152
  };
@@ -29175,6 +29182,16 @@ function notifyOpenStateChange(runtime, open) {
29175
29182
  function canUseDOM3() {
29176
29183
  return typeof window !== "undefined" && typeof document !== "undefined";
29177
29184
  }
29185
+ function releaseHiddenFocusBeforeOpen(options) {
29186
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29187
+ return;
29188
+ }
29189
+ const activeElement = document.activeElement;
29190
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29191
+ return;
29192
+ }
29193
+ activeElement.blur();
29194
+ }
29178
29195
  function getRuntimeDrawerElement(runtime) {
29179
29196
  if (!runtime.element) {
29180
29197
  return null;
@@ -29228,6 +29245,9 @@ function renderVanillaDrawer(id) {
29228
29245
  open: snapshot.state.isOpen,
29229
29246
  onOpenChange: (open) => {
29230
29247
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29248
+ if (open) {
29249
+ releaseHiddenFocusBeforeOpen(runtime.options);
29250
+ }
29231
29251
  runtime.controller.setOpen(open);
29232
29252
  if (previousOpen !== open) {
29233
29253
  notifyOpenStateChange(runtime, open);
@@ -29274,6 +29294,9 @@ function buildVanillaController(id) {
29274
29294
  if (!runtime) {
29275
29295
  return createDrawer({ id, open }).getSnapshot();
29276
29296
  }
29297
+ if (open) {
29298
+ releaseHiddenFocusBeforeOpen(runtime.options);
29299
+ }
29277
29300
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29278
29301
  const snapshot = runtime.controller.setOpen(open);
29279
29302
  if (previousOpen !== open) {
@@ -29344,6 +29367,9 @@ function createDrawer(options = {}) {
29344
29367
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29345
29368
  }
29346
29369
  }
29370
+ if (nextOptions.open && !previousOpen) {
29371
+ releaseHiddenFocusBeforeOpen(nextOptions);
29372
+ }
29347
29373
  renderVanillaDrawer(id);
29348
29374
  if (nextOptions.parentId && nextOptions.open) {
29349
29375
  openAncestorChain(nextOptions.parentId);
@@ -27989,6 +27989,7 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
27989
27989
  }
27990
27990
 
27991
27991
  // src/react/components.tsx
27992
+ var useSafeLayoutEffect = typeof window === "undefined" ? import_react9.default.useEffect : import_react9.default.useLayoutEffect;
27992
27993
  function Root2({
27993
27994
  open: openProp,
27994
27995
  onOpenChange,
@@ -28132,18 +28133,22 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
28132
28133
  noBodyStyles,
28133
28134
  autoFocus
28134
28135
  });
28135
- import_react9.default.useEffect(() => {
28136
+ useSafeLayoutEffect(() => {
28136
28137
  if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28137
28138
  return;
28138
28139
  }
28139
28140
  const activeElement = document.activeElement;
28140
- if (!(activeElement instanceof HTMLElement)) {
28141
+ if (!activeElement || activeElement === document.body) {
28142
+ return;
28143
+ }
28144
+ const activeElementNode = activeElement;
28145
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28141
28146
  return;
28142
28147
  }
28143
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28148
+ if (typeof activeElementNode.blur !== "function") {
28144
28149
  return;
28145
28150
  }
28146
- activeElement.blur();
28151
+ activeElementNode.blur();
28147
28152
  }, [autoFocus, isOpen, modal]);
28148
28153
  function getScale() {
28149
28154
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -28881,7 +28886,7 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
28881
28886
  whiteSpace: "nowrap",
28882
28887
  border: 0
28883
28888
  };
28884
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28889
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28885
28890
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28886
28891
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28887
28892
  const baseProps = {
@@ -28915,7 +28920,7 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
28915
28920
  dataAttribute
28916
28921
  }) {
28917
28922
  const ref = import_react10.default.useRef(null);
28918
- useSafeLayoutEffect(() => {
28923
+ useSafeLayoutEffect2(() => {
28919
28924
  const element = ref.current;
28920
28925
  if (!element) {
28921
28926
  return;
@@ -29121,6 +29126,7 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
29121
29126
  openAncestorChain(nextParentId);
29122
29127
  }
29123
29128
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29129
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29124
29130
  parentRuntime.controller.setOpen(true);
29125
29131
  notifyOpenStateChange(parentRuntime, true);
29126
29132
  renderVanillaDrawer(parentRuntime.id);
@@ -29140,6 +29146,7 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
29140
29146
  }
29141
29147
  const triggerElement = runtime.options.triggerElement;
29142
29148
  const handleClick = () => {
29149
+ releaseHiddenFocusBeforeOpen(runtime.options);
29143
29150
  runtime.controller.setOpen(true);
29144
29151
  renderVanillaDrawer(runtime.id);
29145
29152
  };
@@ -29175,6 +29182,16 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
29175
29182
  function canUseDOM3() {
29176
29183
  return typeof window !== "undefined" && typeof document !== "undefined";
29177
29184
  }
29185
+ function releaseHiddenFocusBeforeOpen(options) {
29186
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29187
+ return;
29188
+ }
29189
+ const activeElement = document.activeElement;
29190
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29191
+ return;
29192
+ }
29193
+ activeElement.blur();
29194
+ }
29178
29195
  function getRuntimeDrawerElement(runtime) {
29179
29196
  if (!runtime.element) {
29180
29197
  return null;
@@ -29228,6 +29245,9 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
29228
29245
  open: snapshot.state.isOpen,
29229
29246
  onOpenChange: (open) => {
29230
29247
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29248
+ if (open) {
29249
+ releaseHiddenFocusBeforeOpen(runtime.options);
29250
+ }
29231
29251
  runtime.controller.setOpen(open);
29232
29252
  if (previousOpen !== open) {
29233
29253
  notifyOpenStateChange(runtime, open);
@@ -29274,6 +29294,9 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
29274
29294
  if (!runtime) {
29275
29295
  return createDrawer({ id, open }).getSnapshot();
29276
29296
  }
29297
+ if (open) {
29298
+ releaseHiddenFocusBeforeOpen(runtime.options);
29299
+ }
29277
29300
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29278
29301
  const snapshot = runtime.controller.setOpen(open);
29279
29302
  if (previousOpen !== open) {
@@ -29344,6 +29367,9 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl
29344
29367
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29345
29368
  }
29346
29369
  }
29370
+ if (nextOptions.open && !previousOpen) {
29371
+ releaseHiddenFocusBeforeOpen(nextOptions);
29372
+ }
29347
29373
  renderVanillaDrawer(id);
29348
29374
  if (nextOptions.parentId && nextOptions.open) {
29349
29375
  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,
@@ -28106,18 +28107,22 @@ function Root2({
28106
28107
  noBodyStyles,
28107
28108
  autoFocus
28108
28109
  });
28109
- import_react9.default.useEffect(() => {
28110
+ useSafeLayoutEffect(() => {
28110
28111
  if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28111
28112
  return;
28112
28113
  }
28113
28114
  const activeElement = document.activeElement;
28114
- if (!(activeElement instanceof HTMLElement)) {
28115
+ if (!activeElement || activeElement === document.body) {
28116
+ return;
28117
+ }
28118
+ const activeElementNode = activeElement;
28119
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28115
28120
  return;
28116
28121
  }
28117
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28122
+ if (typeof activeElementNode.blur !== "function") {
28118
28123
  return;
28119
28124
  }
28120
- activeElement.blur();
28125
+ activeElementNode.blur();
28121
28126
  }, [autoFocus, isOpen, modal]);
28122
28127
  function getScale() {
28123
28128
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -28855,7 +28860,7 @@ var VISUALLY_HIDDEN_STYLE = {
28855
28860
  whiteSpace: "nowrap",
28856
28861
  border: 0
28857
28862
  };
28858
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28863
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28859
28864
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28860
28865
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28861
28866
  const baseProps = {
@@ -28889,7 +28894,7 @@ function VanillaNode({
28889
28894
  dataAttribute
28890
28895
  }) {
28891
28896
  const ref = import_react10.default.useRef(null);
28892
- useSafeLayoutEffect(() => {
28897
+ useSafeLayoutEffect2(() => {
28893
28898
  const element = ref.current;
28894
28899
  if (!element) {
28895
28900
  return;
@@ -29095,6 +29100,7 @@ function openAncestorChain(parentId) {
29095
29100
  openAncestorChain(nextParentId);
29096
29101
  }
29097
29102
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29103
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29098
29104
  parentRuntime.controller.setOpen(true);
29099
29105
  notifyOpenStateChange(parentRuntime, true);
29100
29106
  renderVanillaDrawer(parentRuntime.id);
@@ -29114,6 +29120,7 @@ function bindTriggerElement(runtime) {
29114
29120
  }
29115
29121
  const triggerElement = runtime.options.triggerElement;
29116
29122
  const handleClick = () => {
29123
+ releaseHiddenFocusBeforeOpen(runtime.options);
29117
29124
  runtime.controller.setOpen(true);
29118
29125
  renderVanillaDrawer(runtime.id);
29119
29126
  };
@@ -29149,6 +29156,16 @@ function notifyOpenStateChange(runtime, open) {
29149
29156
  function canUseDOM3() {
29150
29157
  return typeof window !== "undefined" && typeof document !== "undefined";
29151
29158
  }
29159
+ function releaseHiddenFocusBeforeOpen(options) {
29160
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29161
+ return;
29162
+ }
29163
+ const activeElement = document.activeElement;
29164
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29165
+ return;
29166
+ }
29167
+ activeElement.blur();
29168
+ }
29152
29169
  function getRuntimeDrawerElement(runtime) {
29153
29170
  if (!runtime.element) {
29154
29171
  return null;
@@ -29202,6 +29219,9 @@ function renderVanillaDrawer(id) {
29202
29219
  open: snapshot.state.isOpen,
29203
29220
  onOpenChange: (open) => {
29204
29221
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29222
+ if (open) {
29223
+ releaseHiddenFocusBeforeOpen(runtime.options);
29224
+ }
29205
29225
  runtime.controller.setOpen(open);
29206
29226
  if (previousOpen !== open) {
29207
29227
  notifyOpenStateChange(runtime, open);
@@ -29248,6 +29268,9 @@ function buildVanillaController(id) {
29248
29268
  if (!runtime) {
29249
29269
  return createDrawer({ id, open }).getSnapshot();
29250
29270
  }
29271
+ if (open) {
29272
+ releaseHiddenFocusBeforeOpen(runtime.options);
29273
+ }
29251
29274
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29252
29275
  const snapshot = runtime.controller.setOpen(open);
29253
29276
  if (previousOpen !== open) {
@@ -29318,6 +29341,9 @@ function createDrawer(options = {}) {
29318
29341
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29319
29342
  }
29320
29343
  }
29344
+ if (nextOptions.open && !previousOpen) {
29345
+ releaseHiddenFocusBeforeOpen(nextOptions);
29346
+ }
29321
29347
  renderVanillaDrawer(id);
29322
29348
  if (nextOptions.parentId && nextOptions.open) {
29323
29349
  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);
@@ -1577,19 +1578,23 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1577
1578
  noBodyStyles,
1578
1579
  autoFocus
1579
1580
  });
1580
- React__namespace.default.useEffect(()=>{
1581
+ useSafeLayoutEffect(()=>{
1581
1582
  var _drawerRef_current;
1582
1583
  if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1583
1584
  return;
1584
1585
  }
1585
1586
  const activeElement = document.activeElement;
1586
- if (!(activeElement instanceof HTMLElement)) {
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]'))) {
1587
1592
  return;
1588
1593
  }
1589
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1594
+ if (typeof activeElementNode.blur !== 'function') {
1590
1595
  return;
1591
1596
  }
1592
- activeElement.blur();
1597
+ activeElementNode.blur();
1593
1598
  }, [
1594
1599
  autoFocus,
1595
1600
  isOpen,
@@ -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);
@@ -1557,19 +1558,23 @@ function Root({ open: openProp, onOpenChange, children, onDrag: onDragProp, onRe
1557
1558
  noBodyStyles,
1558
1559
  autoFocus
1559
1560
  });
1560
- React__default.useEffect(()=>{
1561
+ useSafeLayoutEffect(()=>{
1561
1562
  var _drawerRef_current;
1562
1563
  if (!isOpen || !modal || autoFocus || typeof document === 'undefined') {
1563
1564
  return;
1564
1565
  }
1565
1566
  const activeElement = document.activeElement;
1566
- if (!(activeElement instanceof HTMLElement)) {
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]'))) {
1567
1572
  return;
1568
1573
  }
1569
- if (((_drawerRef_current = drawerRef.current) == null ? void 0 : _drawerRef_current.contains(activeElement)) || activeElement.closest('[data-drawer]')) {
1574
+ if (typeof activeElementNode.blur !== 'function') {
1570
1575
  return;
1571
1576
  }
1572
- activeElement.blur();
1577
+ activeElementNode.blur();
1573
1578
  }, [
1574
1579
  autoFocus,
1575
1580
  isOpen,
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,
@@ -28131,18 +28132,22 @@ function Root2({
28131
28132
  noBodyStyles,
28132
28133
  autoFocus
28133
28134
  });
28134
- import_react9.default.useEffect(() => {
28135
+ useSafeLayoutEffect(() => {
28135
28136
  if (!isOpen || !modal || autoFocus || typeof document === "undefined") {
28136
28137
  return;
28137
28138
  }
28138
28139
  const activeElement = document.activeElement;
28139
- if (!(activeElement instanceof HTMLElement)) {
28140
+ if (!activeElement || activeElement === document.body) {
28141
+ return;
28142
+ }
28143
+ const activeElementNode = activeElement;
28144
+ if (drawerRef.current?.contains(activeElementNode) || activeElementNode.closest?.("[data-drawer]")) {
28140
28145
  return;
28141
28146
  }
28142
- if (drawerRef.current?.contains(activeElement) || activeElement.closest("[data-drawer]")) {
28147
+ if (typeof activeElementNode.blur !== "function") {
28143
28148
  return;
28144
28149
  }
28145
- activeElement.blur();
28150
+ activeElementNode.blur();
28146
28151
  }, [autoFocus, isOpen, modal]);
28147
28152
  function getScale() {
28148
28153
  return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
@@ -28880,7 +28885,7 @@ var VISUALLY_HIDDEN_STYLE = {
28880
28885
  whiteSpace: "nowrap",
28881
28886
  border: 0
28882
28887
  };
28883
- var useSafeLayoutEffect = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28888
+ var useSafeLayoutEffect2 = typeof window === "undefined" ? import_react10.default.useEffect : import_react10.default.useLayoutEffect;
28884
28889
  function toReactDrawerProps(options, open, onOpenChange, internalOnDragChange, internalOnReleaseChange) {
28885
28890
  const { id: _id, parentId: _parentId, onDragChange, onReleaseChange, ...drawerOptions } = options;
28886
28891
  const baseProps = {
@@ -28914,7 +28919,7 @@ function VanillaNode({
28914
28919
  dataAttribute
28915
28920
  }) {
28916
28921
  const ref = import_react10.default.useRef(null);
28917
- useSafeLayoutEffect(() => {
28922
+ useSafeLayoutEffect2(() => {
28918
28923
  const element = ref.current;
28919
28924
  if (!element) {
28920
28925
  return;
@@ -29120,6 +29125,7 @@ function openAncestorChain(parentId) {
29120
29125
  openAncestorChain(nextParentId);
29121
29126
  }
29122
29127
  if (!parentRuntime.controller.getSnapshot().state.isOpen) {
29128
+ releaseHiddenFocusBeforeOpen(parentRuntime.options);
29123
29129
  parentRuntime.controller.setOpen(true);
29124
29130
  notifyOpenStateChange(parentRuntime, true);
29125
29131
  renderVanillaDrawer(parentRuntime.id);
@@ -29139,6 +29145,7 @@ function bindTriggerElement(runtime) {
29139
29145
  }
29140
29146
  const triggerElement = runtime.options.triggerElement;
29141
29147
  const handleClick = () => {
29148
+ releaseHiddenFocusBeforeOpen(runtime.options);
29142
29149
  runtime.controller.setOpen(true);
29143
29150
  renderVanillaDrawer(runtime.id);
29144
29151
  };
@@ -29174,6 +29181,16 @@ function notifyOpenStateChange(runtime, open) {
29174
29181
  function canUseDOM3() {
29175
29182
  return typeof window !== "undefined" && typeof document !== "undefined";
29176
29183
  }
29184
+ function releaseHiddenFocusBeforeOpen(options) {
29185
+ if (!canUseDOM3() || options.modal === false || options.autoFocus) {
29186
+ return;
29187
+ }
29188
+ const activeElement = document.activeElement;
29189
+ if (!activeElement || activeElement === document.body || typeof activeElement.blur !== "function") {
29190
+ return;
29191
+ }
29192
+ activeElement.blur();
29193
+ }
29177
29194
  function getRuntimeDrawerElement(runtime) {
29178
29195
  if (!runtime.element) {
29179
29196
  return null;
@@ -29227,6 +29244,9 @@ function renderVanillaDrawer(id) {
29227
29244
  open: snapshot.state.isOpen,
29228
29245
  onOpenChange: (open) => {
29229
29246
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29247
+ if (open) {
29248
+ releaseHiddenFocusBeforeOpen(runtime.options);
29249
+ }
29230
29250
  runtime.controller.setOpen(open);
29231
29251
  if (previousOpen !== open) {
29232
29252
  notifyOpenStateChange(runtime, open);
@@ -29273,6 +29293,9 @@ function buildVanillaController(id) {
29273
29293
  if (!runtime) {
29274
29294
  return createDrawer({ id, open }).getSnapshot();
29275
29295
  }
29296
+ if (open) {
29297
+ releaseHiddenFocusBeforeOpen(runtime.options);
29298
+ }
29276
29299
  const previousOpen = runtime.controller.getSnapshot().state.isOpen;
29277
29300
  const snapshot = runtime.controller.setOpen(open);
29278
29301
  if (previousOpen !== open) {
@@ -29343,6 +29366,9 @@ function createDrawer(options = {}) {
29343
29366
  notifyOpenStateChange(existing, snapshot.state.isOpen);
29344
29367
  }
29345
29368
  }
29369
+ if (nextOptions.open && !previousOpen) {
29370
+ releaseHiddenFocusBeforeOpen(nextOptions);
29371
+ }
29346
29372
  renderVanillaDrawer(id);
29347
29373
  if (nextOptions.parentId && nextOptions.open) {
29348
29374
  openAncestorChain(nextOptions.parentId);